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.ItemType;
39  import info.magnolia.cms.core.Path;
40  import info.magnolia.cms.gui.dialog.Dialog;
41  import info.magnolia.module.admininterface.SaveHandler;
42  import info.magnolia.module.admininterface.config.AclTypeConfiguration;
43  
44  import java.util.Iterator;
45  
46  import javax.jcr.RepositoryException;
47  import javax.jcr.PathNotFoundException;
48  import javax.servlet.http.HttpServletRequest;
49  import javax.servlet.http.HttpServletResponse;
50  
51  import org.apache.commons.lang.StringUtils;
52  import org.slf4j.Logger;
53  import org.slf4j.LoggerFactory;
54  
55  
56  /**
57   * @author Fabrizio Giustina
58   * @version $Id: RolesEditDialog.java 41137 2011-01-06 18:19:25Z gjoseph $
59   */
60  public class RolesEditDialog extends ConfiguredDialog {
61  
62      protected static Logger log = LoggerFactory.getLogger("roles dialog"); //$NON-NLS-1$
63  
64      /**
65       * Stable serialVersionUID.
66       */
67      private static final long serialVersionUID = 222L;
68  
69      /**
70       * @param name
71       * @param request
72       * @param response
73       * @param configNode
74       */
75      public RolesEditDialog(String name, HttpServletRequest request, HttpServletResponse response, Content configNode) {
76          super(name, request, response, configNode);
77      }
78  
79      public String getRepository() {
80          String repository = super.getRepository();
81          if (repository == null) {
82              repository = ContentRepository.USER_ROLES;
83          }
84          return repository;
85      }
86  
87      /*
88       * (non-Javadoc)
89       * @see info.magnolia.module.admininterface.DialogMVCHandler#createDialog(info.magnolia.cms.core.Content,
90       * info.magnolia.cms.core.Content)
91       */
92      protected Dialog createDialog(Content configNode, Content storageNode) throws RepositoryException {
93  
94          Dialog dialog = super.createDialog(configNode, storageNode);
95  
96          dialog.setJavascriptSources(request.getContextPath() + "/.resources/admin-js/dialogs/pages/rolesACLPage.js"); //$NON-NLS-1$
97          dialog.setCssSources(request.getContextPath() + "/.resources/admin-css/dialogs/pages/rolesEditPage.css"); //$NON-NLS-1$
98          return dialog;
99      }
100 
101     /**
102      * @see info.magnolia.module.admininterface.DialogMVCHandler#configureSaveHandler(info.magnolia.module.admininterface.SaveHandler)
103      */
104     protected void configureSaveHandler(SaveHandler save) {
105         super.configureSaveHandler(save);
106         save.setPath(path);
107     }
108 
109     protected boolean onPostSave(SaveHandler saveControl) {
110         Content role = this.getStorageNode();
111         try {
112             saveACLs(role, "uri");
113 
114             // for each repository
115             Iterator repositoryNames = ContentRepository.getAllRepositoryNames();
116             while (repositoryNames.hasNext()) {
117                 saveACLs(role, (String) repositoryNames.next());
118             }
119 
120             role.save();
121             return true;
122         } catch (RepositoryException re) {
123             log.error("Failed to update role, reverting all transient modifications made for this node", re);
124             try {
125                 role.refresh(false);
126             } catch (RepositoryException e) {
127                 log.error("Failed to revert transient modifications", e);
128             }
129         }
130         return false;
131     }
132 
133     protected void saveACLs(Content role, String repository) throws RepositoryException {
134         // ######################
135         // # acl
136         // ######################
137         // remove existing
138         try {
139             role.delete("acl_" + repository); //$NON-NLS-1$
140         }
141         catch (PathNotFoundException re) {
142             // ignore, not existing
143         }
144         // rewrite
145         Content acl = role.createContent("acl_" + repository, ItemType.CONTENTNODE); //$NON-NLS-1$
146         String aclValueStr = form.getParameter("acl" + repository + "List"); //$NON-NLS-1$ //$NON-NLS-2$
147         if (StringUtils.isNotEmpty(aclValueStr)) {
148             String[] aclEntries = aclValueStr.split(";"); //$NON-NLS-1$
149             for (int i = 0; i < aclEntries.length; i++) {
150                 String path = StringUtils.EMPTY;
151                 long accessRight = 0;
152                 int accessType = 0;
153 
154                 String[] aclValuePairs = aclEntries[i].split(","); //$NON-NLS-1$
155                 for (int j = 0; j < aclValuePairs.length; j++) {
156                     String[] aclValuePair = aclValuePairs[j].split(":"); //$NON-NLS-1$
157                     String aclName = aclValuePair[0].trim();
158                     String aclValue = StringUtils.EMPTY;
159                     if (aclValuePair.length > 1) {
160                         aclValue = aclValuePair[1].trim();
161                     }
162 
163                     if (aclName.equals("path")) { //$NON-NLS-1$
164                         path = aclValue;
165                     }
166                     else if (aclName.equals("accessType")) { //$NON-NLS-1$
167                         accessType = Integer.valueOf(aclValue).intValue();
168                     }
169                     else if (aclName.equals("accessRight")) { //$NON-NLS-1$
170                         try {
171                             accessRight = Long.parseLong(aclValue);
172                         }
173                         catch (NumberFormatException e) {
174                             accessRight = 0;
175                         }
176                     }
177                 }
178 
179                 if (StringUtils.isNotEmpty(path)) {
180                     if (repository.equalsIgnoreCase("uri")) { //$NON-NLS-1$
181                         // write ACL as is for URI security
182                         accessType = AclTypeConfiguration.TYPE_THIS;
183                     } else if (path.equals("/")) { //$NON-NLS-1$
184                         accessType = AclTypeConfiguration.TYPE_SUBS;
185                         path = StringUtils.EMPTY;
186                     }
187 
188                     if ((accessType & AclTypeConfiguration.TYPE_THIS) != 0) {
189                         try {
190                             String newLabel = Path.getUniqueLabel(hm, acl.getHandle(), "0"); //$NON-NLS-1$
191                             Content r = acl.createContent(newLabel, ItemType.CONTENTNODE);
192                             r.createNodeData("path").setValue(path); //$NON-NLS-1$
193                             r.createNodeData("permissions").setValue(accessRight); //$NON-NLS-1$
194                         }
195                         catch (Exception e) {
196                             log.error(e.getMessage(), e);
197                         }
198                     }
199 
200                     if ((accessType & AclTypeConfiguration.TYPE_SUBS) != 0) {
201                         try {
202                             String newLabel = Path.getUniqueLabel(hm, acl.getHandle(), "0"); //$NON-NLS-1$
203                             Content r = acl.createContent(newLabel, ItemType.CONTENTNODE);
204                             r.createNodeData("path").setValue(path + "/*"); //$NON-NLS-1$ //$NON-NLS-2$
205                             r.createNodeData("permissions").setValue(accessRight); //$NON-NLS-1$
206                         }
207                         catch (Exception e) {
208                             log.error(e.getMessage(), e);
209                         }
210                     }
211                 }
212             }
213         }
214     }
215 
216 }