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.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
63
64
65
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
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 }