View Javadoc

1   /**
2    * This file Copyright (c) 2003-2011 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.module.admininterface.dialogs;
35  
36  import info.magnolia.cms.core.Content;
37  import info.magnolia.cms.core.HierarchyManager;
38  import info.magnolia.cms.core.NodeData;
39  import info.magnolia.cms.gui.dialog.Dialog;
40  import info.magnolia.cms.gui.dialog.DialogControlImpl;
41  import info.magnolia.cms.security.AccessDeniedException;
42  import info.magnolia.context.MgnlContext;
43  import info.magnolia.module.admininterface.SaveHandler;
44  import info.magnolia.repository.RepositoryConstants;
45  
46  import java.util.ArrayList;
47  import java.util.Iterator;
48  import java.util.List;
49  
50  import javax.jcr.ItemNotFoundException;
51  import javax.jcr.PathNotFoundException;
52  import javax.jcr.RepositoryException;
53  import javax.servlet.http.HttpServletRequest;
54  import javax.servlet.http.HttpServletResponse;
55  
56  import org.apache.commons.lang.StringUtils;
57  import org.slf4j.Logger;
58  import org.slf4j.LoggerFactory;
59  
60  
61  /**
62   * @author Fabrizio Giustina
63   * @version $Id$
64   */
65  public class UserEditDialog extends ConfiguredDialog {
66  
67      /**
68       * Stable serialVersionUID.
69       */
70      private static final long serialVersionUID = 222L;
71  
72      /**
73       * Logger.
74       */
75      protected static Logger log = LoggerFactory.getLogger(UserEditDialog.class);
76  
77      protected static final String NODE_ACLUSERS = "acl_users"; //$NON-NLS-1$
78  
79      protected static final String NODE_ACLROLES = "acl_userroles"; //$NON-NLS-1$
80  
81      protected static final String NODE_ACLCONFIG = "acl_config"; //$NON-NLS-1$
82  
83      /*
84       * (non-Javadoc)
85       * @see info.magnolia.module.admininterface.DialogMVCHandler#getRepository()
86       */
87      @Override
88      public String getRepository() {
89          String repository = super.getRepository();
90          if (repository == null) {
91              repository = RepositoryConstants.USERS;
92          }
93          return repository;
94      }
95  
96      /**
97       * @param name
98       * @param request
99       * @param response
100      * @param configNode
101      */
102     public UserEditDialog(String name, HttpServletRequest request, HttpServletResponse response, Content configNode) {
103         super(name, request, response, configNode);
104     }
105 
106     /**
107      * @see info.magnolia.module.admininterface.DialogMVCHandler#configureSaveHandler(info.magnolia.module.admininterface.SaveHandler)
108      */
109     @Override
110     protected void configureSaveHandler(SaveHandler save) {
111         super.configureSaveHandler(save);
112         save.setPath(path);
113     }
114 
115     /**
116      * Is called during showDialog(). Here can you create/ add controls for the dialog.
117      * @param configNode
118      * @param storageNode
119      * @throws javax.jcr.RepositoryException
120      */
121     @Override
122     protected Dialog createDialog(Content configNode, Content storageNode) throws RepositoryException {
123         Dialog dialog = super.createDialog(configNode, storageNode);
124         // don't do anything if command is "save"
125         if (this.getCommand().equalsIgnoreCase(COMMAND_SAVE)) {
126             return dialog;
127         }
128 
129         // replace UUID with Path for groups and roles
130         DialogControlImpl control = dialog.getSub("groups");
131         // it is ok to use system context here as long as it is used only to retrieve UUIDs of nodes we are interested in
132         HierarchyManager groupsHM = MgnlContext.getSystemContext().getHierarchyManager(RepositoryConstants.USER_GROUPS);
133         // replace uuid with path
134         replaceUUIDsWithNames(control, groupsHM);
135 
136         control = dialog.getSub("roles");
137         // it is ok to use system context here as long as it is used only to retrieve UUIDs of nodes we are interested in
138         HierarchyManager rolesHM = MgnlContext.getSystemContext().getHierarchyManager(RepositoryConstants.USER_ROLES);
139         // replace uuid with path
140         replaceUUIDsWithNames(control, rolesHM);
141 
142         return dialog;
143     }
144 
145     private void replaceUUIDsWithNames(DialogControlImpl control, HierarchyManager hm) throws RepositoryException {
146         List values = new ArrayList<String>();
147         Iterator it = control.getValues().iterator();
148         while(it.hasNext()){
149             String uuid = (String) it.next();
150             if (StringUtils.isEmpty(uuid)) {
151                 continue;
152             }
153             try {
154                 values.add(hm.getContentByUUID(uuid).getHandle());
155             }
156             catch (ItemNotFoundException e) {
157              // ignore - group/role doesn't exist anymore
158             }
159         }
160         control.getValues().clear();
161         control.getValues().addAll(values);
162     }
163 
164     /**
165      * Write ACL entries under the given user node
166      * @param node under which ACL for all workspaces needs to be created
167      */
168     protected void writeACL(Content node) throws RepositoryException {
169         // this silly method was rewriting some basic ACLs for user on every update while removing all others. Completely pointless and unnecessary.
170         // required permissions are now assigned at user creation time by the MgnlUserManager if necessary and are left untouched by this dialog.
171         // still keeping method as a hook for changing permissions directly on user node if ever needed
172     }
173 
174     @Override
175     protected boolean onPostSave(SaveHandler saveControl) {
176 
177         Content node = this.getStorageNode();
178 
179         HierarchyManager groupsHM = MgnlContext.getHierarchyManager(
180             RepositoryConstants.USER_GROUPS);
181         HierarchyManager rolesHM = MgnlContext.getHierarchyManager(
182                 RepositoryConstants.USER_ROLES);
183 
184         try {
185             this.writeRolesOrGroups(groupsHM, node, "groups");
186             this.writeRolesOrGroups(rolesHM, node, "roles");
187             this.writeACL(node);
188             node.save();
189             return true;
190         } catch (RepositoryException re) {
191             log.error("Failed to update user, reverting all transient modifications made for this node", re);
192             try {
193                 node.refresh(false);
194             } catch (RepositoryException e) {
195                 log.error("Failed to revert transient modifications", re);
196             }
197         }
198         return false;
199     }
200 
201     private void writeRolesOrGroups(HierarchyManager hm, Content parentNode, String nodeName)
202             throws RepositoryException {
203         try {
204             Content groupOrRoleNode = parentNode.getContent(nodeName);
205             // remove existing roles, leave the node as is
206             Iterator existingNodes = groupOrRoleNode.getNodeDataCollection().iterator();
207             while (existingNodes.hasNext()) {
208                 ((NodeData) existingNodes.next()).delete();
209             }
210             List values = getDialog().getSub(nodeName).getValues();
211             String path = null;
212             for (int index = 0; index < values.size(); index++) {
213                 try {
214                     path = (String) values.get(index);
215                     if (StringUtils.isNotEmpty(path)) {
216                         groupOrRoleNode.createNodeData(Integer.toString(index)).setValue(hm.getContent(path).getUUID());
217                     }
218                 } catch(AccessDeniedException e) {
219                     String user = MgnlContext.getUser().getName();
220                     log.warn("User {} tried to assign {} {} to {} without having privileges to do so.", new Object[] {user, nodeName.substring(0, nodeName.length() - 1), path, (parentNode.getName() == user ? "self" : parentNode.getName())});
221                 }
222             }
223         } catch (PathNotFoundException e) {
224             // this might happen if all groups are deleted via dialog
225         }
226     }
227 }