]> git.argeo.org Git - lgpl/argeo-commons.git/blob - jcr/parts/AddPrivilegeWizard.java
Prepare next development cycle
[lgpl/argeo-commons.git] / 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.cms.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.cms.ui.workbench.useradmin.PickUpUserDialog;
27 import org.argeo.cms.util.UserAdminUtils;
28 import org.argeo.eclipse.ui.EclipseUiException;
29 import org.argeo.eclipse.ui.EclipseUiUtils;
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, "The privilege to retrieve a node and get its properties and their values.");
81 tmpMap.put(Privilege.JCR_WRITE, "An aggregate privilege that "
82 + "contains: jcr:modifyProperties, jcr:addChildNodes, " + "jcr:removeNode, jcr:removeChildNodes");
83 tmpMap.put(Privilege.JCR_ALL, "An aggregate privilege that " + "contains all JCR predefined privileges, "
84 + "plus all implementation-defined privileges. ");
85 AUTH_TYPE_DESC = Collections.unmodifiableMap(tmpMap);
86 }
87
88 public AddPrivilegeWizard(Session currentSession, String path, UserAdmin userAdmin) {
89 super();
90 this.userAdmin = userAdmin;
91 this.currentSession = currentSession;
92 this.targetPath = path;
93 }
94
95 @Override
96 public void addPages() {
97 try {
98 setWindowTitle("Add privilege on " + targetPath);
99 page = new DefinePrivilegePage(userAdmin, targetPath);
100 addPage(page);
101 } catch (Exception e) {
102 throw new EclipseUiException("Cannot add page to wizard ", e);
103 }
104 }
105
106 @Override
107 public boolean performFinish() {
108 if (!canFinish())
109 return false;
110 try {
111 JcrUtils.addPrivilege(currentSession, targetPath, chosenUser.getName(), jcrPrivilege);
112 } catch (RepositoryException re) {
113 throw new EclipseUiException(
114 "Cannot set " + jcrPrivilege + " for " + chosenUser.getName() + " on " + targetPath, re);
115 }
116 return true;
117 }
118
119 private class DefinePrivilegePage extends WizardPage implements ModifyListener {
120 private static final long serialVersionUID = 8084431378762283920L;
121
122 // Context
123 final private UserAdmin userAdmin;
124
125 public DefinePrivilegePage(UserAdmin userAdmin, String path) {
126 super("Main");
127 this.userAdmin = userAdmin;
128 setTitle("Define the privilege to apply to " + path);
129 setMessage("Please choose a user or a group and relevant JCR Privilege.");
130 }
131
132 public void createControl(Composite parent) {
133 final Composite composite = new Composite(parent, SWT.NONE);
134 composite.setLayout(new GridLayout(3, false));
135
136 // specify subject
137 createBoldLabel(composite, "User or group name");
138 final Label groupNameLbl = new Label(composite, SWT.LEAD);
139 groupNameLbl.setLayoutData(EclipseUiUtils.fillWidth());
140
141 Link pickUpLk = new Link(composite, SWT.LEFT);
142 pickUpLk.setText(" <a>Change</a> ");
143
144 createBoldLabel(composite, "User or group DN");
145 final Text groupNameTxt = new Text(composite, SWT.LEAD | SWT.BORDER);
146 groupNameTxt.setLayoutData(EclipseUiUtils.fillWidth(2));
147
148 pickUpLk.addSelectionListener(new SelectionAdapter() {
149 private static final long serialVersionUID = 1L;
150
151 @Override
152 public void widgetSelected(SelectionEvent e) {
153 PickUpUserDialog dialog = new PickUpUserDialog(getShell(), "Choose a group or a user", userAdmin);
154 if (dialog.open() == Window.OK) {
155 chosenUser = dialog.getSelected();
156 groupNameLbl.setText(UserAdminUtils.getCommonName(chosenUser));
157 groupNameTxt.setText(chosenUser.getName());
158 }
159 }
160 });
161
162 groupNameTxt.addFocusListener(new FocusListener() {
163 private static final long serialVersionUID = 1965498600105667738L;
164
165 @Override
166 public void focusLost(FocusEvent event) {
167 String dn = groupNameTxt.getText();
168 if (EclipseUiUtils.isEmpty(dn))
169 return;
170
171 User newChosen = null;
172 try {
173 newChosen = (User) userAdmin.getRole(dn);
174 } catch (Exception e) {
175 boolean tryAgain = MessageDialog.openQuestion(getShell(), "Unvalid DN",
176 "DN " + dn + " is not valid.\nError message: " + e.getMessage()
177 + "\n\t\tDo you want to try again?");
178 if (tryAgain)
179 groupNameTxt.setFocus();
180 else
181 resetOnFail();
182 }
183
184 if (userAdmin.getRole(dn) == null) {
185 boolean tryAgain = MessageDialog.openQuestion(getShell(), "Unexisting role",
186 "User/group " + dn + " does not exist. " + "Do you want to try again?");
187 if (tryAgain)
188 groupNameTxt.setFocus();
189 else
190 resetOnFail();
191 } else {
192 chosenUser = newChosen;
193 groupNameLbl.setText(UserAdminUtils.getCommonName(chosenUser));
194 }
195 }
196
197 private void resetOnFail() {
198 String oldDn = chosenUser == null ? "" : chosenUser.getName();
199 groupNameTxt.setText(oldDn);
200 }
201
202 @Override
203 public void focusGained(FocusEvent event) {
204 }
205 });
206
207 // JCR Privileges
208 createBoldLabel(composite, "Privilege type");
209 Combo authorizationCmb = new Combo(composite, SWT.BORDER | SWT.READ_ONLY | SWT.V_SCROLL);
210 authorizationCmb.setItems(AUTH_TYPE_LABELS.values().toArray(new String[0]));
211 authorizationCmb.setLayoutData(EclipseUiUtils.fillWidth(2));
212 createBoldLabel(composite, ""); // empty cell
213 final Label descLbl = new Label(composite, SWT.WRAP);
214 descLbl.setLayoutData(EclipseUiUtils.fillWidth(2));
215
216 authorizationCmb.addSelectionListener(new SelectionAdapter() {
217 private static final long serialVersionUID = 1L;
218
219 @Override
220 public void widgetSelected(SelectionEvent e) {
221 String chosenPrivStr = ((Combo) e.getSource()).getText();
222 if (AUTH_TYPE_LABELS.containsValue(chosenPrivStr)) {
223 loop: for (String key : AUTH_TYPE_LABELS.keySet()) {
224 if (AUTH_TYPE_LABELS.get(key).equals(chosenPrivStr)) {
225 jcrPrivilege = key;
226 break loop;
227 }
228 }
229 }
230
231 if (jcrPrivilege != null) {
232 descLbl.setText(AUTH_TYPE_DESC.get(jcrPrivilege));
233 composite.layout(true, true);
234 }
235 }
236 });
237
238 // Compulsory
239 setControl(composite);
240 }
241
242 public void modifyText(ModifyEvent event) {
243 String message = checkComplete();
244 if (message != null)
245 setMessage(message, WizardPage.ERROR);
246 else {
247 setMessage("Complete", WizardPage.INFORMATION);
248 setPageComplete(true);
249 }
250 }
251
252 /** @return error message or null if complete */
253 protected String checkComplete() {
254 if (chosenUser == null)
255 return "Please choose a relevant group or user";
256 else if (userAdmin.getRole(chosenUser.getName()) == null)
257 return "Please choose a relevant group or user";
258 else if (jcrPrivilege == null)
259 return "Please choose a relevant JCR privilege";
260 return null;
261 }
262 }
263
264 private Label createBoldLabel(Composite parent, String value) {
265 Label label = new Label(parent, SWT.RIGHT);
266 label.setText(" " + value);
267 label.setFont(EclipseUiUtils.getBoldFont(parent));
268 label.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
269 return label;
270 }
271 }