View Javadoc

1   /**
2    * This file Copyright (c) 2012-2013 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.security.app.dialog.action;
35  
36  import static info.magnolia.cms.security.MgnlUserManager.*;
37  import static info.magnolia.cms.security.SecurityConstants.*;
38  
39  import info.magnolia.cms.security.SecuritySupport;
40  import info.magnolia.cms.security.User;
41  import info.magnolia.cms.security.UserManager;
42  import info.magnolia.jcr.util.NodeTypes;
43  import info.magnolia.jcr.util.NodeUtil;
44  import info.magnolia.jcr.util.PropertyUtil;
45  import info.magnolia.security.app.util.UsersWorkspaceUtil;
46  import info.magnolia.ui.admincentral.dialog.action.SaveDialogAction;
47  import info.magnolia.ui.admincentral.dialog.action.SaveDialogActionDefinition;
48  import info.magnolia.ui.api.ModelConstants;
49  import info.magnolia.ui.api.action.ActionExecutionException;
50  import info.magnolia.ui.form.EditorCallback;
51  import info.magnolia.ui.form.EditorValidator;
52  import info.magnolia.ui.vaadin.integration.jcr.JcrNewNodeAdapter;
53  import info.magnolia.ui.vaadin.integration.jcr.JcrNodeAdapter;
54  
55  import java.util.Collection;
56  
57  import javax.jcr.Node;
58  import javax.jcr.PropertyIterator;
59  import javax.jcr.RepositoryException;
60  
61  import org.apache.commons.lang.StringUtils;
62  import org.slf4j.Logger;
63  import org.slf4j.LoggerFactory;
64  
65  import com.vaadin.data.Item;
66  
67  /**
68   * Save user dialog action.
69   */
70  public class SaveUserDialogAction extends SaveDialogAction {
71  
72      private static final Logger log = LoggerFactory.getLogger(SaveUserDialogAction.class);
73  
74      private SecuritySupport securitySupport;
75  
76      public SaveUserDialogAction(SaveDialogActionDefinition definition, Item item, EditorValidator validator, EditorCallback callback, SecuritySupport securitySupport) {
77          super(definition, item, validator, callback);
78          this.securitySupport = securitySupport;
79      }
80  
81      @Override
82      public void execute() throws ActionExecutionException {
83          // First Validate
84          validator.showValidation(true);
85          if (validator.isValid()) {
86  
87              final JcrNodeAdapter nodeAdapter = (JcrNodeAdapter) item;
88              createOrUpdateUser(nodeAdapter);
89              callback.onSuccess(getDefinition().getName());
90  
91          } else {
92              // validation errors are displayed in the UI.
93          }
94      }
95  
96      private void createOrUpdateUser(final JcrNodeAdapter userItem) throws ActionExecutionException {
97          try {
98  
99              UserManager userManager = securitySupport.getUserManager();
100 
101             String newUserName = (String) userItem.getItemProperty(ModelConstants.JCR_NAME).getValue();
102             String newPassword = (String) userItem.getItemProperty(PROPERTY_PASSWORD).getValue();
103 
104             User user;
105             Node userNode;
106             if (userItem instanceof JcrNewNodeAdapter) {
107 
108                 // JcrNewNodeAdapter returns the parent JCR item here
109                 Node parentNode = userItem.getJcrItem();
110                 String parentPath = parentNode.getPath();
111 
112                 if ("/".equals(parentPath)) {
113                     throw new ActionExecutionException("Users cannot be created directly under root");
114                 }
115 
116                 user = userManager.createUser(parentPath, newUserName, newPassword);
117                 userNode = parentNode.getNode(user.getName());
118             } else {
119                 userNode = userItem.getJcrItem();
120                 String existingUserName = userNode.getName();
121                 user = userManager.getUser(existingUserName);
122 
123                 if (!StringUtils.equals(existingUserName, newUserName)) {
124                     String pathBefore = userNode.getPath();
125                     NodeUtil.renameNode(userNode, newUserName);
126                     userNode.setProperty("name", newUserName);
127                     UsersWorkspaceUtil.updateAcls(userNode, pathBefore);
128                 }
129 
130                 String existingPasswordHash = user.getProperty(PROPERTY_PASSWORD);
131                 if (!StringUtils.equals(newPassword, existingPasswordHash)) {
132                     userManager.setProperty(user, PROPERTY_PASSWORD, newPassword);
133                 }
134             }
135 
136             String enabled = userItem.getItemProperty(PROPERTY_ENABLED).toString();
137             userManager.setProperty(user, PROPERTY_ENABLED, enabled);
138 
139             String title = userItem.getItemProperty(PROPERTY_TITLE).toString();
140             userManager.setProperty(user, PROPERTY_TITLE, title);
141 
142             String email = userItem.getItemProperty(PROPERTY_EMAIL).toString();
143             userManager.setProperty(user, PROPERTY_EMAIL, email);
144 
145             String language = userItem.getItemProperty(PROPERTY_LANGUAGE).toString();
146             userManager.setProperty(user, PROPERTY_LANGUAGE, language);
147 
148             final Collection<String> groups = (Collection<String>) userItem.getItemProperty(NODE_GROUPS).getValue();
149             log.debug("Assigning user the following groups [{}]", groups);
150             storeCollectionAsNodeWithProperties(userNode, NODE_GROUPS, groups);
151 
152             final Collection<String> roles = (Collection<String>) userItem.getItemProperty(NODE_ROLES).getValue();
153             log.debug("Assigning user the following roles [{}]", roles);
154             storeCollectionAsNodeWithProperties(userNode, NODE_ROLES, roles);
155 
156             userNode.getSession().save();
157 
158         } catch (final RepositoryException e) {
159             throw new ActionExecutionException(e);
160         }
161     }
162 
163     private void storeCollectionAsNodeWithProperties(Node parentNode, String name, Collection<String> values) throws RepositoryException {
164         try {
165             // create sub node (or get it, if it already exists)
166             Node node = NodeUtil.createPath(parentNode, name, NodeTypes.ContentNode.NAME);
167 
168             // remove all previous properties
169             PropertyIterator pi = node.getProperties();
170             while (pi.hasNext()) {
171                 javax.jcr.Property p = pi.nextProperty();
172                 if (!p.getName().startsWith(NodeTypes.JCR_PREFIX)) {
173                     p.remove();
174                 }
175             }
176 
177             int i = 0;
178             for (String value : values) {
179                 PropertyUtil.setProperty(node, String.valueOf(i), value.trim());
180                 i++;
181             }
182         } catch (RepositoryException ex) {
183             throw new RepositoryException("Error saving assigned " + name + " of the [" + parentNode.getName() + "] user.", ex);
184         }
185     }
186 }