]>
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
2 * Copyright (C) 2007-2012 Argeo GmbH
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.argeo
.eclipse
.ui
.workbench
.internal
.jcr
.parts
;
18 import java
.util
.Collections
;
19 import java
.util
.HashMap
;
22 import javax
.jcr
.RepositoryException
;
23 import javax
.jcr
.Session
;
24 import javax
.jcr
.security
.Privilege
;
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
;
52 /** Add Jcr privileges to the chosen user group on a given node */
53 public class AddPrivilegeWizard
extends Wizard
{
56 private UserAdmin userAdmin
;
57 private Session currentSession
;
58 private String targetPath
;
60 private User chosenUser
;
61 private String jcrPrivilege
;
64 private DefinePrivilegePage page
;
66 // TODO enable external definition of possible values and corresponding
68 protected static final Map
<String
, String
> AUTH_TYPE_LABELS
;
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
);
77 protected static final Map
<String
, String
> AUTH_TYPE_DESC
;
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
);
91 public AddPrivilegeWizard(Session currentSession
, String path
,
92 UserAdmin userAdmin
) {
94 this.userAdmin
= userAdmin
;
95 this.currentSession
= currentSession
;
96 this.targetPath
= path
;
100 public void addPages() {
102 setWindowTitle("Add privilege on " + targetPath
);
103 page
= new DefinePrivilegePage(userAdmin
, targetPath
);
105 } catch (Exception e
) {
106 throw new ArgeoException("Cannot add page to wizard ", e
);
111 public boolean performFinish() {
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
);
124 private class DefinePrivilegePage
extends WizardPage
implements
126 private static final long serialVersionUID
= 8084431378762283920L;
129 final private UserAdmin userAdmin
;
131 public DefinePrivilegePage(UserAdmin userAdmin
, String path
) {
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.");
138 public void createControl(Composite parent
) {
139 final Composite composite
= new Composite(parent
, SWT
.NONE
);
140 composite
.setLayout(new GridLayout(3, false));
143 createBoldLabel(composite
, "User or group name");
144 final Label groupNameLbl
= new Label(composite
, SWT
.LEAD
);
145 groupNameLbl
.setLayoutData(EclipseUiUtils
.fillWidth());
147 Link pickUpLk
= new Link(composite
, SWT
.LEFT
);
148 pickUpLk
.setText(" <a>Change</a> ");
150 createBoldLabel(composite
, "User or group DN");
151 final Text groupNameTxt
= new Text(composite
, SWT
.LEAD
| SWT
.BORDER
);
152 groupNameTxt
.setLayoutData(EclipseUiUtils
.fillWidth(2));
154 pickUpLk
.addSelectionListener(new SelectionAdapter() {
155 private static final long serialVersionUID
= 1L;
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());
170 groupNameTxt
.addFocusListener(new FocusListener() {
171 private static final long serialVersionUID
= 1965498600105667738L;
174 public void focusLost(FocusEvent event
) {
175 String dn
= groupNameTxt
.getText();
176 User newChosen
= null;
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: "
184 + "\n\t\tDo you want to try again?");
186 groupNameTxt
.setFocus();
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?");
197 groupNameTxt
.setFocus();
201 chosenUser
= newChosen
;
202 groupNameLbl
.setText(UsersUtils
203 .getCommonName(chosenUser
));
207 private void resetOnFail() {
208 String oldDn
= chosenUser
== null ?
"" : chosenUser
210 groupNameTxt
.setText(oldDn
);
214 public void focusGained(FocusEvent event
) {
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(
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));
229 authorizationCmb
.addSelectionListener(new SelectionAdapter() {
230 private static final long serialVersionUID
= 1L;
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
)) {
244 if (jcrPrivilege
!= null) {
245 descLbl
.setText(AUTH_TYPE_DESC
.get(jcrPrivilege
));
246 composite
.layout(true, true);
252 setControl(composite
);
255 public void modifyText(ModifyEvent event
) {
256 String message
= checkComplete();
258 setMessage(message
, WizardPage
.ERROR
);
260 setMessage("Complete", WizardPage
.INFORMATION
);
261 setPageComplete(true);
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";
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));