View Javadoc

1   /**
2    * This file Copyright (c) 2012 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.ui.app.security.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.Security;
40  import info.magnolia.cms.security.User;
41  import info.magnolia.jcr.util.NodeTypes;
42  import info.magnolia.jcr.util.NodeUtil;
43  import info.magnolia.jcr.util.PropertyUtil;
44  import info.magnolia.ui.admincentral.dialog.FormDialogPresenter;
45  import info.magnolia.ui.admincentral.dialog.action.SaveDialogAction;
46  import info.magnolia.ui.model.action.ActionExecutionException;
47  import info.magnolia.ui.vaadin.integration.jcr.JcrNodeAdapter;
48  
49  import javax.jcr.Node;
50  import javax.jcr.PropertyIterator;
51  import javax.jcr.RepositoryException;
52  
53  import org.apache.commons.codec.binary.Base64;
54  import org.apache.commons.lang.StringUtils;
55  import org.slf4j.Logger;
56  import org.slf4j.LoggerFactory;
57  
58  /**
59   * Save user dialog action.
60   */
61  public class SaveUserDialogAction extends SaveDialogAction {
62  
63      private static final Logger log = LoggerFactory.getLogger(SaveUserDialogAction.class);
64  
65      public SaveUserDialogAction(SaveUserDialogActionDefinition definition, FormDialogPresenter presenter) {
66          super(definition, presenter);
67      }
68  
69      @Override
70      public void execute() throws ActionExecutionException {
71          // First Validate
72          getPresenter().getForm().showValidation(true);
73          if (getPresenter().getForm().isValid()) {
74  
75              final JcrNodeAdapter item = (JcrNodeAdapter) getItem();
76              createOrUpdateUser(item);
77              getPresenter().getCallback().onSuccess(getDefinition().getName());
78  
79          } else {
80              // validation errors are displayed in the UI.
81          }
82      }
83  
84      private void createOrUpdateUser(final JcrNodeAdapter userItem) throws ActionExecutionException {
85          try {
86              final Node userNode = userItem.getNode();
87              final String parentPath = userNode.getParent() != null ? userNode.getParent().getPath() : "/";
88  
89              if ("/".equals(parentPath)) {
90                  throw new ActionExecutionException("Users cannot be created directly under root. Path to new user was [" + userNode.getPath() + "]");
91              }
92  
93              final String userName = userNode.getName();
94              log.debug("User name is [{}]", userName);
95  
96              // on user creation or when a user changes it this will be in clear, on all other cases this will be encoded
97              final String passwordFromForm = userItem.getItemProperty(PROPERTY_PASSWORD).getValue().toString();
98              final String encodedPassword = encodePassword(passwordFromForm);
99  
100             if (userNode.isNew()) {
101                 log.debug("User is new, setting his/her password...");
102                 PropertyUtil.setProperty(userNode, PROPERTY_PASSWORD, encodedPassword);
103             } else {
104                 final User user = Security.getUserManager().getUser(userName);
105                 final String existingEncodedPassword = user.getPassword();
106                 // if user exists compare the existing password with the one coming from the form
107                 // if they're not equal change password
108                 boolean passwordChanged = !existingEncodedPassword.equals(passwordFromForm);
109                 if (passwordChanged) {
110                     log.debug("Updating password for existing user [{}]", userName);
111                     PropertyUtil.setProperty(userNode, PROPERTY_PASSWORD, encodedPassword);
112                 }
113             }
114 
115             final String enabled = userItem.getItemProperty(PROPERTY_ENABLED).getValue().toString();
116             log.debug("Is user enabled? {}", enabled);
117             PropertyUtil.setProperty(userNode, PROPERTY_ENABLED, Boolean.parseBoolean(enabled));
118 
119             final String email = userItem.getItemProperty(PROPERTY_EMAIL).getValue().toString();
120             log.debug("Setting user email as [{}]", email);
121             PropertyUtil.setProperty(userNode, PROPERTY_EMAIL, email);
122 
123             final String fullName = userItem.getItemProperty(PROPERTY_TITLE).getValue().toString();
124             log.debug("Setting user title as [{}]", fullName);
125             PropertyUtil.setProperty(userNode, PROPERTY_TITLE, fullName);
126 
127             final String[] groups = itemPropertyToArray(userItem, NODE_GROUPS);
128             log.debug("Assigning user the following groups [{}]", groups);
129             replacePropertyWithSubnode(userNode, NODE_GROUPS, groups);
130 
131             final String[] roles = itemPropertyToArray(userItem, NODE_ROLES);
132             log.debug("Assigning user the following roles [{}]", roles);
133             replacePropertyWithSubnode(userNode, NODE_ROLES, roles);
134 
135             NodeTypes.LastModified.update(userNode);
136             userNode.getSession().save();
137         } catch (final RepositoryException e) {
138             throw new ActionExecutionException(e);
139         }
140     }
141 
142     private String[] itemPropertyToArray(JcrNodeAdapter item, String propertyName) {
143         String identifiers = item.getItemProperty(propertyName).getValue().toString();
144         identifiers = StringUtils.remove(identifiers, '[');
145         identifiers = StringUtils.remove(identifiers, ']');
146         return StringUtils.split(identifiers, ',');
147     }
148 
149     private void replacePropertyWithSubnode(Node node, String name, String[] ids) throws RepositoryException {
150         try {
151             node.getProperty(name).remove();
152         } catch (RepositoryException ex) {
153             log.warn("Cannot remove [" + name + "] property of the user [" + node.getName() + "]: " + ex.getMessage());
154         }
155         try {
156             // create subnode (or get it, if it already exists)
157             Node subnode = NodeUtil.createPath(node, name, NodeTypes.ContentNode.NAME);
158             // sanity: remove all possible non-jcr properties
159             PropertyIterator pi = subnode.getProperties();
160             while (pi.hasNext()) {
161                 javax.jcr.Property p = pi.nextProperty();
162                 if (!p.getName().startsWith(NodeTypes.JCR_PREFIX)) {
163                     p.remove();
164                 }
165             }
166             // add new groups
167             int i = 0;
168             for (String id : ids) {
169                 PropertyUtil.setProperty(subnode, "" + i, id.trim());
170                 i++;
171             }
172         } catch (RepositoryException ex) {
173             log.error("Error saving assigned " + name + " of the [" + node.getName() + "] user.", ex);
174             throw new RepositoryException("Error saving assigned " + name + " of the [" + node.getName() + "] user.", ex);
175         }
176     }
177 
178     private String encodePassword(final String clearPassword) throws ActionExecutionException {
179         if (StringUtils.isBlank(clearPassword)) {
180             throw new ActionExecutionException("Password cannot be blank");
181         }
182         return new String(Base64.encodeBase64(clearPassword.getBytes()));
183     }
184 
185 }