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