View Javadoc

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