1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
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
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
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
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
107
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
157 Node subnode = NodeUtil.createPath(node, name, NodeTypes.ContentNode.NAME);
158
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
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 }