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.beans.config.ContentRepository;
37  import info.magnolia.cms.core.Content;
38  import info.magnolia.cms.core.NodeData;
39  import info.magnolia.cms.core.HierarchyManager;
40  import info.magnolia.cms.gui.dialog.Dialog;
41  import info.magnolia.cms.gui.dialog.DialogControlImpl;
42  import info.magnolia.cms.security.AccessDeniedException;
43  import info.magnolia.context.MgnlContext;
44  import info.magnolia.module.admininterface.SaveHandler;
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: UserEditDialog.java 45241 2011-05-19 06:34:40Z ochytil $
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      public String getRepository() {
88          String repository = super.getRepository();
89          if (repository == null) {
90              repository = ContentRepository.USERS;
91          }
92          return repository;
93      }
94  
95      /**
96       * @param name
97       * @param request
98       * @param response
99       * @param configNode
100      */
101     public UserEditDialog(String name, HttpServletRequest request, HttpServletResponse response, Content configNode) {
102         super(name, request, response, configNode);
103     }
104 
105     /**
106      * @see info.magnolia.module.admininterface.DialogMVCHandler#configureSaveHandler(info.magnolia.module.admininterface.SaveHandler)
107      */
108     protected void configureSaveHandler(SaveHandler save) {
109         super.configureSaveHandler(save);
110         save.setPath(path);
111     }
112 
113     /**
114      * Is called during showDialog(). Here can you create/ add controls for the dialog.
115      * @param configNode
116      * @param storageNode
117      * @throws javax.jcr.RepositoryException
118      */
119     protected Dialog createDialog(Content configNode, Content storageNode) throws RepositoryException {
120         Dialog dialog = super.createDialog(configNode, storageNode);
121         // don't do anything if command is "save"
122         if (this.getCommand().equalsIgnoreCase(COMMAND_SAVE)) {
123             return dialog;
124         }
125 
126         // replace UUID with Path for groups and roles
127         DialogControlImpl control = dialog.getSub("groups");
128         // it is ok to use system context here as long as it is used only to retrieve UUIDs of nodes we are interested in
129         HierarchyManager groupsHM = MgnlContext.getSystemContext().getHierarchyManager(ContentRepository.USER_GROUPS);
130         // replace uuid with path
131         replaceUUIDsWithNames(control, groupsHM);
132 
133         control = dialog.getSub("roles");
134         // it is ok to use system context here as long as it is used only to retrieve UUIDs of nodes we are interested in
135         HierarchyManager rolesHM = MgnlContext.getSystemContext().getHierarchyManager(ContentRepository.USER_ROLES);
136         // replace uuid with path
137         replaceUUIDsWithNames(control, rolesHM);
138        
139         return dialog;
140     }
141 
142 	private void replaceUUIDsWithNames(DialogControlImpl control, HierarchyManager hm) throws RepositoryException {
143 		List values = new ArrayList<String>();
144         Iterator it = control.getValues().iterator();
145         while(it.hasNext()){
146             String uuid = (String) it.next();
147             if (StringUtils.isEmpty(uuid)) {
148                 continue;
149             }
150             try {
151                 values.add(hm.getContentByUUID(uuid).getHandle());
152             }
153             catch (ItemNotFoundException e) {
154              // ignore - group/role doesn't exist anymore
155             }
156         }
157         control.getValues().clear();
158         control.getValues().addAll(values);
159 	}
160 
161     /**
162      * Write ACL entries under the given user node
163      * @param node under which ACL for all workspaces needs to be created
164      */
165     protected void writeACL(Content node) throws RepositoryException {
166         // this silly method was rewriting some basic ACLs for user on every update while removing all others. Completely pointless and unnecessary.
167         // required permissions are now assigned at user creation time by the MgnlUserManager if necessary and are left untouched by this dialog.
168         // still keeping method as a hook for changing permissions directly on user node if ever needed
169     }
170 
171     protected boolean onPostSave(SaveHandler saveControl) {
172 
173         Content node = this.getStorageNode();
174 
175         HierarchyManager groupsHM = MgnlContext.getHierarchyManager(
176             ContentRepository.USER_GROUPS);
177         HierarchyManager rolesHM = MgnlContext.getHierarchyManager(
178             ContentRepository.USER_ROLES);
179 
180         try {
181             this.writeRolesOrGroups(groupsHM, node, "groups");
182             this.writeRolesOrGroups(rolesHM, node, "roles");
183             this.writeACL(node);
184             node.save();
185             return true;
186         } catch (RepositoryException re) {
187             log.error("Failed to update user, reverting all transient modifications made for this node", re);
188             try {
189                 node.refresh(false);
190             } catch (RepositoryException e) {
191                 log.error("Failed to revert transient modifications", re);
192             }
193         }
194         return false;
195     }
196 
197     private void writeRolesOrGroups(HierarchyManager hm, Content parentNode, String nodeName)
198             throws RepositoryException {
199         try {
200             Content groupOrRoleNode = parentNode.getContent(nodeName);
201             // remove existing roles, leave the node as is
202             Iterator existingNodes = groupOrRoleNode.getNodeDataCollection().iterator();
203             while (existingNodes.hasNext()) {
204                 ((NodeData) existingNodes.next()).delete();
205             }
206             List values = getDialog().getSub(nodeName).getValues();
207             String path = null;
208             for (int index = 0; index < values.size(); index++) {
209                 try {
210                     path = (String) values.get(index);
211                     if (StringUtils.isNotEmpty(path)) {
212                         groupOrRoleNode.createNodeData(Integer.toString(index)).setValue(hm.getContent(path).getUUID());
213                     }
214                 } catch(AccessDeniedException e) {
215                     String user = MgnlContext.getUser().getName();
216                     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())});
217                 }
218             }
219         } catch (PathNotFoundException e) {
220             // this might happen if all groups are deleted via dialog
221         }
222     }
223 }