View Javadoc

1   /**
2    * This file Copyright (c) 2013-2014 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.field;
35  
36  import info.magnolia.cms.security.Permission;
37  import info.magnolia.i18nsystem.SimpleTranslator;
38  import info.magnolia.jcr.RuntimeRepositoryException;
39  import info.magnolia.jcr.util.NodeUtil;
40  import info.magnolia.ui.vaadin.integration.jcr.AbstractJcrNodeAdapter;
41  import info.magnolia.ui.vaadin.integration.jcr.DefaultProperty;
42  import info.magnolia.ui.vaadin.integration.jcr.JcrNewNodeAdapter;
43  import info.magnolia.ui.vaadin.integration.jcr.JcrNodeAdapter;
44  
45  import javax.jcr.Node;
46  import javax.jcr.RepositoryException;
47  
48  import com.vaadin.data.Item;
49  import com.vaadin.data.Property;
50  import com.vaadin.ui.AbstractOrderedLayout;
51  import com.vaadin.ui.Button;
52  import com.vaadin.ui.Component;
53  import com.vaadin.ui.CustomField;
54  import com.vaadin.ui.Field;
55  import com.vaadin.ui.HorizontalLayout;
56  import com.vaadin.ui.Label;
57  import com.vaadin.ui.NativeSelect;
58  import com.vaadin.ui.TextField;
59  import com.vaadin.ui.VerticalLayout;
60  
61  /**
62   * Field builder for the web access field.
63   *
64   * @param <D> definition type
65   * @see WebAccessFieldDefinition
66   */
67  public class WebAccessFieldFactory<D extends WebAccessFieldDefinition> extends AbstractAccessFieldFactory<D> {
68  
69      private static final String ACL_NODE_NAME = "acl_uri";
70      private static final String PERMISSIONS_PROPERTY_NAME = "permissions";
71      private static final String PATH_PROPERTY_NAME = "path";
72  
73      private final SimpleTranslator i18n;
74  
75      public WebAccessFieldFactory(D definition, Item relatedFieldItem, SimpleTranslator i18n) {
76          super(definition, relatedFieldItem);
77          this.i18n = i18n;
78      }
79  
80      @Override
81      protected Field<Object> createFieldComponent() {
82  
83          final VerticalLayout layout = new VerticalLayout();
84          layout.setSpacing(true);
85  
86          try {
87  
88              final JcrNodeAdapter roleItem = (JcrNodeAdapter) item;
89  
90              final VerticalLayout aclLayout = new VerticalLayout();
91  
92              final Label emptyLabel = new Label(i18n.translate("security.web.field.noAccess"));
93  
94              // Since JcrNewNodeAdapter.getJcrItem() returns the parent node we need to skip this step because we don't want to inspect the parent node
95              if (!(roleItem instanceof JcrNewNodeAdapter)) {
96                  Node roleNode = roleItem.getJcrItem();
97                  if (roleNode.hasNode(ACL_NODE_NAME)) {
98  
99                      final Node aclNode = roleNode.getNode(ACL_NODE_NAME);
100                     AbstractJcrNodeAdapter aclItem = new JcrNodeAdapter(aclNode);
101                     roleItem.addChild(aclItem);
102 
103                     for (Node entryNode : NodeUtil.getNodes(aclNode)) {
104 
105                         AbstractJcrNodeAdapter entryItem = new JcrNodeAdapter(entryNode);
106                         aclItem.addChild(entryItem);
107 
108                         Component ruleRow = createRuleRow(aclLayout, entryItem, emptyLabel);
109                         aclLayout.addComponent(ruleRow);
110                     }
111                 }
112             }
113 
114             if (aclLayout.getComponentCount() == 0) {
115                 aclLayout.addComponent(emptyLabel);
116             }
117 
118             final HorizontalLayout buttons = new HorizontalLayout();
119             final Button addButton = new Button(i18n.translate("security.web.field.addNew"));
120             addButton.addClickListener(new Button.ClickListener() {
121 
122                 @Override
123                 public void buttonClick(Button.ClickEvent event) {
124 
125                     try {
126                         AbstractJcrNodeAdapter aclItem = getOrAddAclItem(roleItem, ACL_NODE_NAME);
127                         JcrNewNodeAdapter newItem = addAclEntryItem(aclItem);
128                         Component ruleRow = createRuleRow(aclLayout, newItem, emptyLabel);
129                         aclLayout.removeComponent(emptyLabel);
130                         aclLayout.addComponent(ruleRow, aclLayout.getComponentCount() - 1);
131 
132                     } catch (RepositoryException e) {
133                         throw new RuntimeRepositoryException(e);
134                     }
135                 }
136             });
137             buttons.addComponent(addButton);
138             aclLayout.addComponent(buttons);
139 
140             layout.addComponent(aclLayout);
141 
142         } catch (RepositoryException e) {
143             throw new RuntimeRepositoryException(e);
144         }
145 
146         return new CustomField<Object>() {
147 
148             @Override
149             protected Component initContent() {
150                 return layout;
151             }
152 
153             @Override
154             public Class<?> getType() {
155                 return Object.class;
156             }
157         };
158     }
159 
160     private Component createRuleRow(final AbstractOrderedLayout parentContainer, final AbstractJcrNodeAdapter ruleItem, final Label emptyLabel) {
161 
162         final HorizontalLayout ruleLayout = new HorizontalLayout();
163         ruleLayout.setSpacing(true);
164         ruleLayout.setWidth("100%");
165 
166         NativeSelect accessRights = new NativeSelect();
167         accessRights.addItem(Permission.ALL);
168         accessRights.setItemCaption(Permission.ALL, i18n.translate("security.web.field.getPost"));
169         accessRights.addItem(Permission.READ);
170         accessRights.setItemCaption(Permission.READ, i18n.translate("security.web.field.get"));
171         accessRights.addItem(Permission.NONE);
172         accessRights.setItemCaption(Permission.NONE, i18n.translate("security.web.field.deny"));
173         accessRights.setNullSelectionAllowed(false);
174         accessRights.setImmediate(true);
175         accessRights.setInvalidAllowed(false);
176         accessRights.setNewItemsAllowed(false);
177         Property permissionsProperty = ruleItem.getItemProperty(PERMISSIONS_PROPERTY_NAME);
178         if (permissionsProperty == null) {
179             permissionsProperty = new DefaultProperty<Long>(Long.class, Permission.ALL);
180             ruleItem.addItemProperty(PERMISSIONS_PROPERTY_NAME, permissionsProperty);
181         }
182         accessRights.setPropertyDataSource(permissionsProperty);
183         ruleLayout.addComponent(accessRights);
184 
185         TextField path = new TextField();
186         path.setWidth("100%");
187         Property pathProperty = ruleItem.getItemProperty(PATH_PROPERTY_NAME);
188         if (pathProperty == null) {
189             pathProperty = new DefaultProperty<String>(String.class, "/*");
190             ruleItem.addItemProperty(PATH_PROPERTY_NAME, pathProperty);
191         }
192         path.setPropertyDataSource(pathProperty);
193         ruleLayout.addComponent(path);
194         ruleLayout.setExpandRatio(path, 1.0f);
195 
196         final Button deleteButton = new Button();
197         deleteButton.setHtmlContentAllowed(true);
198         deleteButton.setCaption("<span class=\"" + "icon-trash" + "\"></span>");
199         deleteButton.addStyleName("inline");
200         deleteButton.setDescription(i18n.translate("security.web.field.delete"));
201         deleteButton.addClickListener(new Button.ClickListener() {
202 
203             @Override
204             public void buttonClick(Button.ClickEvent event) {
205                 parentContainer.removeComponent(ruleLayout);
206                 ruleItem.getParent().removeChild(ruleItem);
207                 if (parentContainer.getComponentCount() == 1) {
208                     parentContainer.addComponent(emptyLabel, 0);
209                 }
210             }
211         });
212         ruleLayout.addComponent(deleteButton);
213 
214         return ruleLayout;
215     }
216 }