001/*
002 *  Copyright 2018 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.plugins.userdirectory.signup;
017
018import java.util.Map;
019
020import org.apache.avalon.framework.configuration.Configuration;
021import org.apache.avalon.framework.configuration.ConfigurationException;
022import org.apache.avalon.framework.context.Context;
023import org.apache.avalon.framework.context.ContextException;
024import org.apache.avalon.framework.service.ServiceException;
025import org.apache.avalon.framework.service.ServiceManager;
026import org.apache.cocoon.components.ContextHelper;
027import org.apache.cocoon.environment.Request;
028import org.apache.commons.lang.StringUtils;
029
030import org.ametys.cms.contenttype.ContentType;
031import org.ametys.cms.contenttype.ContentTypeExtensionPoint;
032import org.ametys.core.user.User;
033import org.ametys.plugins.repository.AmetysRepositoryException;
034import org.ametys.runtime.i18n.I18nizableText;
035import org.ametys.web.WebConstants;
036import org.ametys.web.content.FOContentCreationHelper;
037import org.ametys.web.repository.page.ZoneItem;
038import org.ametys.web.usermanagement.UserManagementException;
039
040import com.google.common.collect.Multimap;
041import com.opensymphony.workflow.WorkflowException;
042
043/**
044 * This component overrides {@link org.ametys.web.usermanagement.UserSignupManager} to add the creation of a user content on signup.
045 */
046public class UserSignupManager extends org.ametys.web.usermanagement.UserSignupManager
047{
048    /** The default workflow action id for initializing user content */
049    protected int _initWorkflowActionId;
050    /** The default workflow name user content */
051    protected String _workflowName;
052    
053    private Context _context;
054    private ContentTypeExtensionPoint _contentTypeEP;
055    private FOContentCreationHelper _foHelper;
056
057    @Override
058    public void contextualize(Context context) throws ContextException
059    {
060        super.contextualize(context);
061        _context = context;
062    }
063    
064    @Override
065    public void service(ServiceManager serviceManager) throws ServiceException
066    {
067        super.service(serviceManager);
068        _contentTypeEP = (ContentTypeExtensionPoint) serviceManager.lookup(ContentTypeExtensionPoint.ROLE);
069        _foHelper = (FOContentCreationHelper) serviceManager.lookup(FOContentCreationHelper.ROLE);
070    }
071    
072    @Override
073    public void configure(Configuration configuration) throws ConfigurationException
074    {
075        super.configure(configuration);
076        
077        _initWorkflowActionId = Integer.valueOf(configuration.getChild("user-init-action-id").getValue());
078        _workflowName = configuration.getChild("user-workflow-name").getValue();
079    }
080    
081    /**
082     * Get the request
083     * @return the request
084     */
085    protected Request _getRequest()
086    {
087        return ContextHelper.getRequest(_context);
088    }
089    
090    @Override
091    public void validationBeforeSignup(Multimap<String, I18nizableText> errors)
092    {
093        Request request = _getRequest();
094        
095        ZoneItem zoneItem = (ZoneItem) request.getAttribute(WebConstants.REQUEST_ATTR_ZONEITEM);
096        if (zoneItem != null)
097        {
098            String userContentTypeId = zoneItem.getServiceParameters().getValue("user-content-type");
099            if (StringUtils.isNotBlank(userContentTypeId))
100            {
101                ContentType userContentType = _contentTypeEP.getExtension(userContentTypeId);
102                if (userContentType != null)
103                {
104                    Map<String, Object> userValues = _foHelper.getAndValidateFormValues(request, userContentType, "signup", errors);
105                    if (errors.isEmpty())
106                    {
107                        request.setAttribute(UserSignupManager.class.getName() + "$userValues", userValues);
108                    }
109                }
110            }
111        }
112    }
113    
114    @Override
115    public void additionalSignupOperations(User createdUser, Multimap<String, I18nizableText> errors) throws UserManagementException
116    {
117        super.additionalSignupOperations(createdUser, errors);
118        
119        Request request = _getRequest();
120        
121        String siteName = (String) request.getAttribute(WebConstants.REQUEST_ATTR_SITE_NAME);
122        String language = (String) request.getAttribute(WebConstants.REQUEST_ATTR_SITEMAP_NAME);
123        ZoneItem zoneItem = (ZoneItem) request.getAttribute(WebConstants.REQUEST_ATTR_ZONEITEM);
124        
125        if (zoneItem != null)
126        {
127            String userContentTypeId = zoneItem.getServiceParameters().getValue("user-content-type");
128            if (StringUtils.isNotBlank(userContentTypeId))
129            {
130                ContentType userContentType = _contentTypeEP.getExtension(userContentTypeId);
131                if (userContentType != null)
132                {
133                    // Create the user content type linked to this user
134                    @SuppressWarnings("unchecked")
135                    Map<String, Object> userValues = (Map<String, Object>) request.getAttribute(UserSignupManager.class.getName() + "$userValues");
136                    
137                    // Add link to created user
138                    userValues.put("user", createdUser.getIdentity());
139                        
140                    if (errors.isEmpty())
141                    {
142                        try
143                        {
144                            _foHelper.createAndEditContent(_initWorkflowActionId, userContentType.getId(), siteName, createdUser.getFullName(), createdUser.getFullName(), language, userValues, _workflowName, null);
145                        }
146                        catch (AmetysRepositoryException | WorkflowException e)
147                        {
148                            getLogger().error("Unable to create the user content for user '{}' after sign up", createdUser, e);
149                            errors.put("global", new I18nizableText("PLUGINS_USER_DIRECTORY_SIGNUP_SERVICE_CREATE_CONTENT_ERROR"));
150                        }
151                    }
152                }
153            }
154        }
155    }
156    
157}