View Javadoc

1   /**
2    * This file Copyright (c) 2012-2013 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.security.app.dialog.action;
35  
36  import info.magnolia.jcr.util.NodeUtil;
37  import info.magnolia.security.app.dialog.field.AccessControlList;
38  import info.magnolia.security.app.dialog.field.WorkspaceAccessFieldFactory;
39  import info.magnolia.ui.admincentral.dialog.action.SaveDialogAction;
40  import info.magnolia.ui.admincentral.dialog.action.SaveDialogActionDefinition;
41  import info.magnolia.ui.api.action.ActionExecutionException;
42  import info.magnolia.ui.form.EditorCallback;
43  import info.magnolia.ui.form.EditorValidator;
44  import info.magnolia.ui.vaadin.integration.jcr.JcrNodeAdapter;
45  
46  import javax.jcr.Node;
47  import javax.jcr.RepositoryException;
48  import javax.jcr.Value;
49  
50  import org.apache.commons.lang.StringUtils;
51  
52  import com.vaadin.data.Item;
53  
54  /**
55   * Save role dialog action. Transforms nodes added by {@link info.magnolia.security.app.dialog.field.WorkspaceAccessFieldFactory} to its final representation.
56   */
57  public class SaveRoleDialogAction extends SaveDialogAction {
58  
59      public SaveRoleDialogAction(SaveDialogActionDefinition definition, Item item, EditorValidator validator, EditorCallback callback) {
60          super(definition, item, validator, callback);
61      }
62  
63      @Override
64      public void execute() throws ActionExecutionException {
65          // First Validate
66          validator.showValidation(true);
67          if (validator.isValid()) {
68              final JcrNodeAdapter itemChanged = (JcrNodeAdapter) item;
69  
70              try {
71  
72                  final Node node = itemChanged.applyChanges();
73  
74                  for (Node aclNode : NodeUtil.getNodes(node)) {
75  
76                      // Any node marked as using the intermediary format we read in, remove all its sub nodes and then
77                      // add new sub nodes based on the read in ACL
78                      if (aclNode.hasProperty(WorkspaceAccessFieldFactory.INTERMEDIARY_FORMAT_PROPERTY_NAME)) {
79  
80                          AccessControlList acl = new AccessControlList();
81  
82                          for (Node entryNode : NodeUtil.getNodes(aclNode)) {
83  
84                              if (entryNode.hasProperty(WorkspaceAccessFieldFactory.INTERMEDIARY_FORMAT_PROPERTY_NAME)) {
85                                  String path = entryNode.getProperty(AccessControlList.PATH_PROPERTY_NAME).getString();
86                                  long accessType = (int) entryNode.getProperty(WorkspaceAccessFieldFactory.ACCESS_TYPE_PROPERTY_NAME).getLong();
87                                  long permissions = entryNode.getProperty(AccessControlList.PERMISSIONS_PROPERTY_NAME).getLong();
88  
89                                  if (path.equals("/")) {
90                                  } else if (path.equals("/*")) {
91                                      path = "/";
92                                  } else {
93                                      path = StringUtils.removeEnd(path, "/*");
94                                      path = StringUtils.removeEnd(path, "/");
95                                  }
96  
97                                  if (StringUtils.isNotBlank(path)) {
98                                      acl.addEntry(new AccessControlList.Entry(permissions, accessType, path));
99                                  }
100                             }
101 
102                             entryNode.remove();
103                         }
104 
105                         aclNode.setProperty(WorkspaceAccessFieldFactory.INTERMEDIARY_FORMAT_PROPERTY_NAME, (Value)null);
106                         acl.saveEntries(aclNode);
107                     }
108                 }
109 
110                 node.getSession().save();
111             } catch (final RepositoryException e) {
112                 throw new ActionExecutionException(e);
113             }
114             callback.onSuccess(getDefinition().getName());
115 
116         } else {
117             // validation errors are displayed in the UI.
118         }
119     }
120 
121 }