1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.security.app.dialog.field;
35
36 import info.magnolia.cms.security.Permission;
37 import info.magnolia.jcr.RuntimeRepositoryException;
38 import info.magnolia.jcr.util.NodeUtil;
39 import info.magnolia.ui.vaadin.integration.jcr.AbstractJcrNodeAdapter;
40 import info.magnolia.ui.vaadin.integration.jcr.DefaultProperty;
41 import info.magnolia.ui.vaadin.integration.jcr.JcrNewNodeAdapter;
42 import info.magnolia.ui.vaadin.integration.jcr.JcrNodeAdapter;
43
44 import javax.jcr.Node;
45 import javax.jcr.RepositoryException;
46
47 import com.vaadin.data.Item;
48 import com.vaadin.data.Property;
49 import com.vaadin.ui.AbstractOrderedLayout;
50 import com.vaadin.ui.Button;
51 import com.vaadin.ui.Component;
52 import com.vaadin.ui.CustomField;
53 import com.vaadin.ui.Field;
54 import com.vaadin.ui.HorizontalLayout;
55 import com.vaadin.ui.Label;
56 import com.vaadin.ui.NativeSelect;
57 import com.vaadin.ui.TextField;
58 import com.vaadin.ui.VerticalLayout;
59
60
61
62
63
64
65
66 public class WebAccessFieldFactory<D extends WebAccessFieldDefinition> extends AbstractAccessFieldFactory<D> {
67
68 private static final String ACL_NODE_NAME = "acl_uri";
69 private static final String PERMISSIONS_PROPERTY_NAME = "permissions";
70 private static final String PATH_PROPERTY_NAME = "path";
71
72 public WebAccessFieldFactory(D definition, Item relatedFieldItem) {
73 super(definition, relatedFieldItem);
74 }
75
76 @Override
77 protected Field<Object> createFieldComponent() {
78
79 final VerticalLayout layout = new VerticalLayout();
80 layout.setSpacing(true);
81
82 try {
83
84 final JcrNodeAdapter roleItem = (JcrNodeAdapter) item;
85 Node roleNode = roleItem.getJcrItem();
86
87 final VerticalLayout aclLayout = new VerticalLayout();
88
89 final Label emptyLabel = new Label("No access.");
90
91 if (roleNode.hasNode(ACL_NODE_NAME)) {
92
93 final Node aclNode = roleNode.getNode(ACL_NODE_NAME);
94 AbstractJcrNodeAdapter aclItem = new JcrNodeAdapter(aclNode);
95 roleItem.addChild(aclItem);
96
97 for (Node entryNode : NodeUtil.getNodes(aclNode)) {
98
99 AbstractJcrNodeAdapter entryItem = new JcrNodeAdapter(entryNode);
100 aclItem.addChild(entryItem);
101
102 Component ruleRow = createRuleRow(aclLayout, entryItem, emptyLabel);
103 aclLayout.addComponent(ruleRow);
104 }
105 }
106
107 if (aclLayout.getComponentCount() == 0) {
108 aclLayout.addComponent(emptyLabel);
109 }
110
111 final HorizontalLayout buttons = new HorizontalLayout();
112 final Button addButton = new Button("Add new");
113 addButton.addClickListener(new Button.ClickListener() {
114
115 @Override
116 public void buttonClick(Button.ClickEvent event) {
117
118 try {
119 AbstractJcrNodeAdapter aclItem = getOrAddAclItem(roleItem, ACL_NODE_NAME);
120 JcrNewNodeAdapter newItem = addAclEntryItem(aclItem);
121 Component ruleRow = createRuleRow(aclLayout, newItem, emptyLabel);
122 aclLayout.removeComponent(emptyLabel);
123 aclLayout.addComponent(ruleRow, aclLayout.getComponentCount() - 1);
124
125 } catch (RepositoryException e) {
126 throw new RuntimeRepositoryException(e);
127 }
128 }
129 });
130 buttons.addComponent(addButton);
131 aclLayout.addComponent(buttons);
132
133 layout.addComponent(aclLayout);
134
135 } catch (RepositoryException e) {
136 throw new RuntimeRepositoryException(e);
137 }
138
139 return new CustomField<Object>() {
140
141 @Override
142 protected Component initContent() {
143 return layout;
144 }
145
146 @Override
147 public Class<?> getType() {
148 return Object.class;
149 }
150 };
151 }
152
153 private Component createRuleRow(final AbstractOrderedLayout parentContainer, final AbstractJcrNodeAdapter ruleItem, final Label emptyLabel) {
154
155 final HorizontalLayout ruleLayout = new HorizontalLayout();
156 ruleLayout.setSpacing(true);
157
158 NativeSelect accessRights = new NativeSelect();
159 accessRights.addItem(Permission.ALL);
160 accessRights.setItemCaption(Permission.ALL, "Get & Post");
161 accessRights.addItem(Permission.READ);
162 accessRights.setItemCaption(Permission.READ, "Get");
163 accessRights.addItem(Permission.NONE);
164 accessRights.setItemCaption(Permission.NONE, "Deny");
165 accessRights.setNullSelectionAllowed(false);
166 accessRights.setImmediate(true);
167 accessRights.setInvalidAllowed(false);
168 accessRights.setNewItemsAllowed(false);
169 Property permissionsProperty = ruleItem.getItemProperty(PERMISSIONS_PROPERTY_NAME);
170 if (permissionsProperty == null) {
171 permissionsProperty = new DefaultProperty<Long>(Long.class, Permission.ALL);
172 ruleItem.addItemProperty(PERMISSIONS_PROPERTY_NAME, permissionsProperty);
173 }
174 accessRights.setPropertyDataSource(permissionsProperty);
175 ruleLayout.addComponent(accessRights);
176
177 TextField path = new TextField();
178 path.setWidth("375px");
179 Property pathProperty = ruleItem.getItemProperty(PATH_PROPERTY_NAME);
180 if (pathProperty == null) {
181 pathProperty = new DefaultProperty<String>(String.class, "/*");
182 ruleItem.addItemProperty(PATH_PROPERTY_NAME, pathProperty);
183 }
184 path.setPropertyDataSource(pathProperty);
185 ruleLayout.addComponent(path);
186
187 final Button deleteButton = new Button();
188 deleteButton.setHtmlContentAllowed(true);
189 deleteButton.setCaption("<span class=\"" + "icon-trash" + "\"></span>");
190 deleteButton.addStyleName("inline");
191 deleteButton.setDescription("Delete");
192 deleteButton.addClickListener(new Button.ClickListener() {
193
194 @Override
195 public void buttonClick(Button.ClickEvent event) {
196 parentContainer.removeComponent(ruleLayout);
197 ruleItem.getParent().removeChild(ruleItem);
198 if (parentContainer.getComponentCount() == 1) {
199 parentContainer.addComponent(emptyLabel, 0);
200 }
201 }
202 });
203 ruleLayout.addComponent(deleteButton);
204
205 return ruleLayout;
206 }
207 }