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.cms.workflow.EditContentFunction;
033import org.ametys.core.user.User;
034import org.ametys.plugins.core.user.UserHelper;
035import org.ametys.plugins.repository.AmetysRepositoryException;
036import org.ametys.runtime.i18n.I18nizableText;
037import org.ametys.web.WebConstants;
038import org.ametys.web.content.FOContentCreationHelper;
039import org.ametys.web.repository.page.ZoneItem;
040import org.ametys.web.usermanagement.UserManagementException;
041
042import com.google.common.collect.Multimap;
043import com.opensymphony.workflow.WorkflowException;
044
045/**
046 * This component overrides {@link org.ametys.web.usermanagement.UserSignupManager} to add the creation of a user content on signup.
047 */
048public class UserSignupManager extends org.ametys.web.usermanagement.UserSignupManager
049{
050    /** The default workflow action id for initializing user content */
051    protected int _initWorkflowActionId;
052    /** The default workflow name user content */
053    protected String _workflowName;
054    
055    private Context _context;
056    private ContentTypeExtensionPoint _contentTypeEP;
057    private FOContentCreationHelper _foHelper;
058    private UserHelper _userHelper;
059
060    @Override
061    public void contextualize(Context context) throws ContextException
062    {
063        super.contextualize(context);
064        _context = context;
065    }
066    
067    @Override
068    public void service(ServiceManager serviceManager) throws ServiceException
069    {
070        super.service(serviceManager);
071        _contentTypeEP = (ContentTypeExtensionPoint) serviceManager.lookup(ContentTypeExtensionPoint.ROLE);
072        _foHelper = (FOContentCreationHelper) serviceManager.lookup(FOContentCreationHelper.ROLE);
073        _userHelper = (UserHelper) serviceManager.lookup(UserHelper.ROLE);
074    }
075    
076    @Override
077    public void configure(Configuration configuration) throws ConfigurationException
078    {
079        super.configure(configuration);
080        
081        _initWorkflowActionId = Integer.valueOf(configuration.getChild("user-init-action-id").getValue());
082        _workflowName = configuration.getChild("user-workflow-name").getValue();
083    }
084    
085    /**
086     * Get the request
087     * @return the request
088     */
089    protected Request _getRequest()
090    {
091        return ContextHelper.getRequest(_context);
092    }
093    
094    @Override
095    public void validationBeforeSignup(Multimap<String, I18nizableText> errors)
096    {
097        Request request = _getRequest();
098        
099        ZoneItem zoneItem = (ZoneItem) request.getAttribute(WebConstants.REQUEST_ATTR_ZONEITEM);
100        if (zoneItem != null)
101        {
102            String userContentTypeId = zoneItem.getServiceParameters().getValue("user-content-type");
103            if (StringUtils.isNotBlank(userContentTypeId))
104            {
105                ContentType userContentType = _contentTypeEP.getExtension(userContentTypeId);
106                if (userContentType != null)
107                {
108                    Map<String, Object> userValues = _foHelper.getAndValidateFormValues(request, userContentType, "signup", errors);
109                    if (errors.isEmpty())
110                    {
111                        request.setAttribute(UserSignupManager.class.getName() + "$userValues", userValues);
112                    }
113                }
114            }
115        }
116    }
117    
118    @Override
119    public void additionalSignupOperations(User createdUser, Multimap<String, I18nizableText> errors) throws UserManagementException
120    {
121        super.additionalSignupOperations(createdUser, errors);
122        
123        Request request = _getRequest();
124        
125        String siteName = (String) request.getAttribute(WebConstants.REQUEST_ATTR_SITE_NAME);
126        String language = (String) request.getAttribute(WebConstants.REQUEST_ATTR_SITEMAP_NAME);
127        ZoneItem zoneItem = (ZoneItem) request.getAttribute(WebConstants.REQUEST_ATTR_ZONEITEM);
128        
129        if (zoneItem != null)
130        {
131            String userContentTypeId = zoneItem.getServiceParameters().getValue("user-content-type");
132            if (StringUtils.isNotBlank(userContentTypeId))
133            {
134                ContentType userContentType = _contentTypeEP.getExtension(userContentTypeId);
135                if (userContentType != null)
136                {
137                    // Create the user content type linked to this user
138                    @SuppressWarnings("unchecked")
139                    Map<String, Object> userValues = (Map<String, Object>) request.getAttribute(UserSignupManager.class.getName() + "$userValues");
140                    
141                    // Add link to created user
142                    userValues.put(EditContentFunction.FORM_ELEMENTS_PREFIX + "user", _userHelper.user2json(createdUser.getIdentity()));
143                        
144                    if (errors.isEmpty())
145                    {
146                        try
147                        {
148                            _foHelper.createAndEditContent(_initWorkflowActionId, userContentType.getId(), siteName, createdUser.getFullName(), createdUser.getFullName(), language, userValues, _workflowName, null);
149                        }
150                        catch (AmetysRepositoryException | WorkflowException e)
151                        {
152                            getLogger().error("Unable to create the user content for user '{}' after sign up", createdUser, e);
153                            errors.put("global", new I18nizableText("PLUGINS_USER_DIRECTORY_SIGNUP_SERVICE_CREATE_CONTENT_ERROR"));
154                        }
155                    }
156                }
157            }
158        }
159    }
160    
161}