]> 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
Working Argeo 2 deployment (with UI)
[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(getShell(),
160 "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 if (EclipseUiUtils.isEmpty(dn))
177 return;
178
179 User newChosen = null;
180 try {
181 newChosen = (User) userAdmin.getRole(dn);
182 } catch (Exception e) {
183 boolean tryAgain = MessageDialog.openQuestion(
184 getShell(), "Unvalid DN",
185 "DN " + dn + " is not valid.\nError message: "
186 + e.getMessage()
187 + "\n\t\tDo you want to try again?");
188 if (tryAgain)
189 groupNameTxt.setFocus();
190 else
191 resetOnFail();
192 }
193
194 if (userAdmin.getRole(dn) == null) {
195 boolean tryAgain = MessageDialog.openQuestion(
196 getShell(), "Unexisting role", "User/group "
197 + dn + " does not exist. "
198 + "Do you want to try again?");
199 if (tryAgain)
200 groupNameTxt.setFocus();
201 else
202 resetOnFail();
203 } else {
204 chosenUser = newChosen;
205 groupNameLbl.setText(UsersUtils
206 .getCommonName(chosenUser));
207 }
208 }
209
210 private void resetOnFail() {
211 String oldDn = chosenUser == null ? "" : chosenUser
212 .getName();
213 groupNameTxt.setText(oldDn);
214 }
215
216 @Override
217 public void focusGained(FocusEvent event) {
218 }
219 });
220
221 // JCR Privileges
222 createBoldLabel(composite, "Privilege type");
223 Combo authorizationCmb = new Combo(composite, SWT.BORDER
224 | SWT.READ_ONLY | SWT.V_SCROLL);
225 authorizationCmb.setItems(AUTH_TYPE_LABELS.values().toArray(
226 new String[0]));
227 authorizationCmb.setLayoutData(EclipseUiUtils.fillWidth(2));
228 createBoldLabel(composite, ""); // empty cell
229 final Label descLbl = new Label(composite, SWT.WRAP);
230 descLbl.setLayoutData(EclipseUiUtils.fillWidth(2));
231
232 authorizationCmb.addSelectionListener(new SelectionAdapter() {
233 private static final long serialVersionUID = 1L;
234
235 @Override
236 public void widgetSelected(SelectionEvent e) {
237 String chosenPrivStr = ((Combo) e.getSource()).getText();
238 if (AUTH_TYPE_LABELS.containsValue(chosenPrivStr)) {
239 loop: for (String key : AUTH_TYPE_LABELS.keySet()) {
240 if (AUTH_TYPE_LABELS.get(key).equals(chosenPrivStr)) {
241 jcrPrivilege = key;
242 break loop;
243 }
244 }
245 }
246
247 if (jcrPrivilege != null) {
248 descLbl.setText(AUTH_TYPE_DESC.get(jcrPrivilege));
249 composite.layout(true, true);
250 }
251 }
252 });
253
254 // Compulsory
255 setControl(composite);
256 }
257
258 public void modifyText(ModifyEvent event) {
259 String message = checkComplete();
260 if (message != null)
261 setMessage(message, WizardPage.ERROR);
262 else {
263 setMessage("Complete", WizardPage.INFORMATION);
264 setPageComplete(true);
265 }
266 }
267
268 /** @return error message or null if complete */
269 protected String checkComplete() {
270 if (chosenUser == null)
271 return "Please choose a relevant group or user";
272 else if (userAdmin.getRole(chosenUser.getName()) == null)
273 return "Please choose a relevant group or user";
274 else if (jcrPrivilege == null)
275 return "Please choose a relevant JCR privilege";
276 return null;
277 }
278 }
279
280 private Label createBoldLabel(Composite parent, String value) {
281 Label label = new Label(parent, SWT.RIGHT);
282 label.setText(" " + value);
283 label.setFont(EclipseUiUtils.getBoldFont(parent));
284 label.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
285 return label;
286 }
287 }