]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.eclipse.ui.workbench/src/org/argeo/eclipse/ui/workbench/internal/jcr/parts/AddPrivilegeWizard.java
Reactivate experimental page to edit String properties.
[lgpl/argeo-commons.git] / org.argeo.eclipse.ui.workbench / src / org / argeo / eclipse / ui / workbench / internal / jcr / parts / AddPrivilegeWizard.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.eclipse.ui.workbench.internal.jcr.parts;
17
18 import java.util.Collections;
19 import java.util.HashMap;
20 import java.util.Map;
21
22 import javax.jcr.RepositoryException;
23 import javax.jcr.Session;
24 import javax.jcr.security.Privilege;
25
26 import org.argeo.ArgeoException;
27 import org.argeo.eclipse.ui.EclipseUiUtils;
28 import org.argeo.eclipse.ui.workbench.internal.users.UsersUtils;
29 import org.argeo.eclipse.ui.workbench.users.PickUpUserDialog;
30 import org.argeo.jcr.JcrUtils;
31 import org.eclipse.jface.dialogs.MessageDialog;
32 import org.eclipse.jface.window.Window;
33 import org.eclipse.jface.wizard.Wizard;
34 import org.eclipse.jface.wizard.WizardPage;
35 import org.eclipse.swt.SWT;
36 import org.eclipse.swt.events.FocusEvent;
37 import org.eclipse.swt.events.FocusListener;
38 import org.eclipse.swt.events.ModifyEvent;
39 import org.eclipse.swt.events.ModifyListener;
40 import org.eclipse.swt.events.SelectionAdapter;
41 import org.eclipse.swt.events.SelectionEvent;
42 import org.eclipse.swt.layout.GridData;
43 import org.eclipse.swt.layout.GridLayout;
44 import org.eclipse.swt.widgets.Combo;
45 import org.eclipse.swt.widgets.Composite;
46 import org.eclipse.swt.widgets.Label;
47 import org.eclipse.swt.widgets.Link;
48 import org.eclipse.swt.widgets.Text;
49 import org.osgi.service.useradmin.User;
50 import org.osgi.service.useradmin.UserAdmin;
51
52 /** Add JCR privileges to the chosen user group on a given node */
53 public class AddPrivilegeWizard extends Wizard {
54
55 // Context
56 private UserAdmin userAdmin;
57 private Session currentSession;
58 private String targetPath;
59 // Chosen parameters
60 private User chosenUser;
61 private String jcrPrivilege;
62
63 // UI Object
64 private DefinePrivilegePage page;
65
66 // TODO enable external definition of possible values and corresponding
67 // description
68 protected static final Map<String, String> AUTH_TYPE_LABELS;
69 static {
70 Map<String, String> tmpMap = new HashMap<String, String>();
71 tmpMap.put(Privilege.JCR_READ, "jcr:read");
72 tmpMap.put(Privilege.JCR_WRITE, "jcr:write");
73 tmpMap.put(Privilege.JCR_ALL, "jcr:all");
74 AUTH_TYPE_LABELS = Collections.unmodifiableMap(tmpMap);
75 }
76
77 protected static final Map<String, String> AUTH_TYPE_DESC;
78 static {
79 Map<String, String> tmpMap = new HashMap<String, String>();
80 tmpMap.put(Privilege.JCR_READ,
81 "The privilege to retrieve a node and get its properties and their values.");
82 tmpMap.put(Privilege.JCR_WRITE, "An aggregate privilege that "
83 + "contains: jcr:modifyProperties, jcr:addChildNodes, "
84 + "jcr:removeNode, jcr:removeChildNodes");
85 tmpMap.put(Privilege.JCR_ALL, "An aggregate privilege that "
86 + "contains all JCR predefined privileges, "
87 + "plus all implementation-defined privileges. ");
88 AUTH_TYPE_DESC = Collections.unmodifiableMap(tmpMap);
89 }
90
91 public AddPrivilegeWizard(Session currentSession, String path,
92 UserAdmin userAdmin) {
93 super();
94 this.userAdmin = userAdmin;
95 this.currentSession = currentSession;
96 this.targetPath = path;
97 }
98
99 @Override
100 public void addPages() {
101 try {
102 setWindowTitle("Add privilege on " + targetPath);
103 page = new DefinePrivilegePage(userAdmin, targetPath);
104 addPage(page);
105 } catch (Exception e) {
106 throw new ArgeoException("Cannot add page to wizard ", e);
107 }
108 }
109
110 @Override
111 public boolean performFinish() {
112 if (!canFinish())
113 return false;
114 try {
115 JcrUtils.addPrivilege(currentSession, targetPath,
116 chosenUser.getName(), jcrPrivilege);
117 } catch (RepositoryException re) {
118 throw new ArgeoException("Cannot set " + jcrPrivilege + " for "
119 + chosenUser.getName() + " on " + targetPath, re);
120 }
121 return true;
122 }
123
124 private class DefinePrivilegePage extends WizardPage implements
125 ModifyListener {
126 private static final long serialVersionUID = 8084431378762283920L;
127
128 // Context
129 final private UserAdmin userAdmin;
130
131 public DefinePrivilegePage(UserAdmin userAdmin, String path) {
132 super("Main");
133 this.userAdmin = userAdmin;
134 setTitle("Define the privilege to apply to " + path);
135 setMessage("Please choose a user or a group and relevant JCR Privilege.");
136 }
137
138 public void createControl(Composite parent) {
139 final Composite composite = new Composite(parent, SWT.NONE);
140 composite.setLayout(new GridLayout(3, false));
141
142 // specify subject
143 createBoldLabel(composite, "User or group name");
144 final Label groupNameLbl = new Label(composite, SWT.LEAD);
145 groupNameLbl.setLayoutData(EclipseUiUtils.fillWidth());
146
147 Link pickUpLk = new Link(composite, SWT.LEFT);
148 pickUpLk.setText(" <a>Change</a> ");
149
150 createBoldLabel(composite, "User or group DN");
151 final Text groupNameTxt = new Text(composite, SWT.LEAD | SWT.BORDER);
152 groupNameTxt.setLayoutData(EclipseUiUtils.fillWidth(2));
153
154 pickUpLk.addSelectionListener(new SelectionAdapter() {
155 private static final long serialVersionUID = 1L;
156
157 @Override
158 public void widgetSelected(SelectionEvent e) {
159 PickUpUserDialog dialog = new PickUpUserDialog(
160 getShell(), "Choose a group or a user", userAdmin);
161 if (dialog.open() == Window.OK) {
162 chosenUser = dialog.getSelected();
163 groupNameLbl.setText(UsersUtils
164 .getCommonName(chosenUser));
165 groupNameTxt.setText(chosenUser.getName());
166 }
167 }
168 });
169
170 groupNameTxt.addFocusListener(new FocusListener() {
171 private static final long serialVersionUID = 1965498600105667738L;
172
173 @Override
174 public void focusLost(FocusEvent event) {
175 String dn = groupNameTxt.getText();
176 User newChosen = null;
177 try {
178 newChosen = (User) userAdmin.getRole(dn);
179 } catch (Exception e) {
180 boolean tryAgain = MessageDialog.openQuestion(
181 getShell(), "Unvalid DN",
182 "DN " + dn + " is not valid.\nError message: "
183 + e.getMessage()
184 + "\n\t\tDo you want to try again?");
185 if (tryAgain)
186 groupNameTxt.setFocus();
187 else
188 resetOnFail();
189 }
190
191 if (userAdmin.getRole(dn) == null) {
192 boolean tryAgain = MessageDialog.openQuestion(
193 getShell(), "Unexisting role", "User/group "
194 + dn + " does not exist. "
195 + "Do you want to try again?");
196 if (tryAgain)
197 groupNameTxt.setFocus();
198 else
199 resetOnFail();
200 } else {
201 chosenUser = newChosen;
202 groupNameLbl.setText(UsersUtils
203 .getCommonName(chosenUser));
204 }
205 }
206
207 private void resetOnFail() {
208 String oldDn = chosenUser == null ? "" : chosenUser
209 .getName();
210 groupNameTxt.setText(oldDn);
211 }
212
213 @Override
214 public void focusGained(FocusEvent event) {
215 }
216 });
217
218 // JCR Privileges
219 createBoldLabel(composite, "Privilege type");
220 Combo authorizationCmb = new Combo(composite, SWT.BORDER
221 | SWT.READ_ONLY | SWT.V_SCROLL);
222 authorizationCmb.setItems(AUTH_TYPE_LABELS.values().toArray(
223 new String[0]));
224 authorizationCmb.setLayoutData(EclipseUiUtils.fillWidth(2));
225 createBoldLabel(composite, ""); // empty cell
226 final Label descLbl = new Label(composite, SWT.WRAP);
227 descLbl.setLayoutData(EclipseUiUtils.fillWidth(2));
228
229 authorizationCmb.addSelectionListener(new SelectionAdapter() {
230 private static final long serialVersionUID = 1L;
231
232 @Override
233 public void widgetSelected(SelectionEvent e) {
234 String chosenPrivStr = ((Combo) e.getSource()).getText();
235 if (AUTH_TYPE_LABELS.containsValue(chosenPrivStr)) {
236 loop: for (String key : AUTH_TYPE_LABELS.keySet()) {
237 if (AUTH_TYPE_LABELS.get(key).equals(chosenPrivStr)) {
238 jcrPrivilege = key;
239 break loop;
240 }
241 }
242 }
243
244 if (jcrPrivilege != null) {
245 descLbl.setText(AUTH_TYPE_DESC.get(jcrPrivilege));
246 composite.layout(true, true);
247 }
248 }
249 });
250
251 // Compulsory
252 setControl(composite);
253 }
254
255 public void modifyText(ModifyEvent event) {
256 String message = checkComplete();
257 if (message != null)
258 setMessage(message, WizardPage.ERROR);
259 else {
260 setMessage("Complete", WizardPage.INFORMATION);
261 setPageComplete(true);
262 }
263 }
264
265 /** @return error message or null if complete */
266 protected String checkComplete() {
267 if (chosenUser == null)
268 return "Please choose a relevant group or user";
269 else if (userAdmin.getRole(chosenUser.getName()) == null)
270 return "Please choose a relevant group or user";
271 else if (jcrPrivilege == null)
272 return "Please choose a relevant JCR privilege";
273 return null;
274 }
275 }
276
277 private Label createBoldLabel(Composite parent, String value) {
278 Label label = new Label(parent, SWT.RIGHT);
279 label.setText(" " + value);
280 label.setFont(EclipseUiUtils.getBoldFont(parent));
281 label.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
282 return label;
283 }
284 }