]>
git.argeo.org Git - gpl/argeo-slc.git/blob - legacy/argeo-commons/org.argeo.cms.ui.workbench/src/org/argeo/cms/ui/workbench/internal/jcr/parts/AddPrivilegeWizard.java
1 package org
.argeo
.cms
.ui
.workbench
.internal
.jcr
.parts
;
3 import java
.util
.Collections
;
4 import java
.util
.HashMap
;
7 import javax
.jcr
.RepositoryException
;
8 import javax
.jcr
.Session
;
9 import javax
.jcr
.security
.Privilege
;
11 import org
.argeo
.cms
.ui
.useradmin
.PickUpUserDialog
;
12 import org
.argeo
.cms
.auth
.UserAdminUtils
;
13 import org
.argeo
.eclipse
.ui
.EclipseUiException
;
14 import org
.argeo
.eclipse
.ui
.EclipseUiUtils
;
15 import org
.argeo
.jcr
.JcrUtils
;
16 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
17 import org
.eclipse
.jface
.window
.Window
;
18 import org
.eclipse
.jface
.wizard
.Wizard
;
19 import org
.eclipse
.jface
.wizard
.WizardPage
;
20 import org
.eclipse
.swt
.SWT
;
21 import org
.eclipse
.swt
.events
.FocusEvent
;
22 import org
.eclipse
.swt
.events
.FocusListener
;
23 import org
.eclipse
.swt
.events
.ModifyEvent
;
24 import org
.eclipse
.swt
.events
.ModifyListener
;
25 import org
.eclipse
.swt
.events
.SelectionAdapter
;
26 import org
.eclipse
.swt
.events
.SelectionEvent
;
27 import org
.eclipse
.swt
.layout
.GridData
;
28 import org
.eclipse
.swt
.layout
.GridLayout
;
29 import org
.eclipse
.swt
.widgets
.Combo
;
30 import org
.eclipse
.swt
.widgets
.Composite
;
31 import org
.eclipse
.swt
.widgets
.Label
;
32 import org
.eclipse
.swt
.widgets
.Link
;
33 import org
.eclipse
.swt
.widgets
.Text
;
34 import org
.osgi
.service
.useradmin
.User
;
35 import org
.osgi
.service
.useradmin
.UserAdmin
;
37 /** Add JCR privileges to the chosen user group on a given node */
38 public class AddPrivilegeWizard
extends Wizard
{
41 private UserAdmin userAdmin
;
42 private Session currentSession
;
43 private String targetPath
;
45 private String chosenDn
;
46 private User chosenUser
;
47 private String jcrPrivilege
;
50 private DefinePrivilegePage page
;
52 // TODO enable external definition of possible values and corresponding
54 protected static final Map
<String
, String
> AUTH_TYPE_LABELS
;
56 Map
<String
, String
> tmpMap
= new HashMap
<String
, String
>();
57 tmpMap
.put(Privilege
.JCR_READ
, "jcr:read");
58 tmpMap
.put(Privilege
.JCR_WRITE
, "jcr:write");
59 tmpMap
.put(Privilege
.JCR_ALL
, "jcr:all");
60 AUTH_TYPE_LABELS
= Collections
.unmodifiableMap(tmpMap
);
63 protected static final Map
<String
, String
> AUTH_TYPE_DESC
;
65 Map
<String
, String
> tmpMap
= new HashMap
<String
, String
>();
66 tmpMap
.put(Privilege
.JCR_READ
, "The privilege to retrieve a node and get its properties and their values.");
67 tmpMap
.put(Privilege
.JCR_WRITE
, "An aggregate privilege that "
68 + "contains: jcr:modifyProperties, jcr:addChildNodes, " + "jcr:removeNode, jcr:removeChildNodes");
69 tmpMap
.put(Privilege
.JCR_ALL
, "An aggregate privilege that " + "contains all JCR predefined privileges, "
70 + "plus all implementation-defined privileges. ");
71 AUTH_TYPE_DESC
= Collections
.unmodifiableMap(tmpMap
);
74 public AddPrivilegeWizard(Session currentSession
, String path
, UserAdmin userAdmin
) {
76 this.userAdmin
= userAdmin
;
77 this.currentSession
= currentSession
;
78 this.targetPath
= path
;
82 public void addPages() {
84 setWindowTitle("Add privilege on " + targetPath
);
85 page
= new DefinePrivilegePage(userAdmin
, targetPath
);
87 } catch (Exception e
) {
88 throw new EclipseUiException("Cannot add page to wizard ", e
);
93 public boolean performFinish() {
97 String username
= chosenUser
.getName();
98 if (EclipseUiUtils
.notEmpty(chosenDn
) && chosenDn
.equalsIgnoreCase(username
))
99 // Enable forcing the username case. TODO clean once this issue
100 // has been generally addressed
102 JcrUtils
.addPrivilege(currentSession
, targetPath
, username
, jcrPrivilege
);
103 } catch (RepositoryException re
) {
104 throw new EclipseUiException(
105 "Cannot set " + jcrPrivilege
+ " for " + chosenUser
.getName() + " on " + targetPath
, re
);
110 private class DefinePrivilegePage
extends WizardPage
implements ModifyListener
{
111 private static final long serialVersionUID
= 8084431378762283920L;
114 final private UserAdmin userAdmin
;
116 public DefinePrivilegePage(UserAdmin userAdmin
, String path
) {
118 this.userAdmin
= userAdmin
;
119 setTitle("Define the privilege to apply to " + path
);
120 setMessage("Please choose a user or a group and relevant JCR Privilege.");
123 public void createControl(Composite parent
) {
124 final Composite composite
= new Composite(parent
, SWT
.NONE
);
125 composite
.setLayout(new GridLayout(3, false));
128 createBoldLabel(composite
, "User or group name");
129 final Label userNameLbl
= new Label(composite
, SWT
.LEAD
);
130 userNameLbl
.setLayoutData(EclipseUiUtils
.fillWidth());
132 Link pickUpLk
= new Link(composite
, SWT
.LEFT
);
133 pickUpLk
.setText(" <a>Change</a> ");
135 createBoldLabel(composite
, "User or group DN");
136 final Text userNameTxt
= new Text(composite
, SWT
.LEAD
| SWT
.BORDER
);
137 userNameTxt
.setLayoutData(EclipseUiUtils
.fillWidth(2));
139 pickUpLk
.addSelectionListener(new SelectionAdapter() {
140 private static final long serialVersionUID
= 1L;
143 public void widgetSelected(SelectionEvent e
) {
144 PickUpUserDialog dialog
= new PickUpUserDialog(getShell(), "Choose a group or a user", userAdmin
);
145 if (dialog
.open() == Window
.OK
) {
146 chosenUser
= dialog
.getSelected();
147 userNameLbl
.setText(UserAdminUtils
.getCommonName(chosenUser
));
148 userNameTxt
.setText(chosenUser
.getName());
153 userNameTxt
.addFocusListener(new FocusListener() {
154 private static final long serialVersionUID
= 1965498600105667738L;
157 public void focusLost(FocusEvent event
) {
158 String dn
= userNameTxt
.getText();
159 if (EclipseUiUtils
.isEmpty(dn
))
162 User newChosen
= null;
164 newChosen
= (User
) userAdmin
.getRole(dn
);
165 } catch (Exception e
) {
166 boolean tryAgain
= MessageDialog
.openQuestion(getShell(), "Unvalid DN",
167 "DN " + dn
+ " is not valid.\nError message: " + e
.getMessage()
168 + "\n\t\tDo you want to try again?");
170 userNameTxt
.setFocus();
175 if (userAdmin
.getRole(dn
) == null) {
176 boolean tryAgain
= MessageDialog
.openQuestion(getShell(), "Unexisting role",
177 "User/group " + dn
+ " does not exist. " + "Do you want to try again?");
179 userNameTxt
.setFocus();
183 chosenUser
= newChosen
;
185 userNameLbl
.setText(UserAdminUtils
.getCommonName(chosenUser
));
189 private void resetOnFail() {
190 String oldDn
= chosenUser
== null ?
"" : chosenUser
.getName();
191 userNameTxt
.setText(oldDn
);
195 public void focusGained(FocusEvent event
) {
200 createBoldLabel(composite
, "Privilege type");
201 Combo authorizationCmb
= new Combo(composite
, SWT
.BORDER
| SWT
.READ_ONLY
| SWT
.V_SCROLL
);
202 authorizationCmb
.setItems(AUTH_TYPE_LABELS
.values().toArray(new String
[0]));
203 authorizationCmb
.setLayoutData(EclipseUiUtils
.fillWidth(2));
204 createBoldLabel(composite
, ""); // empty cell
205 final Label descLbl
= new Label(composite
, SWT
.WRAP
);
206 descLbl
.setLayoutData(EclipseUiUtils
.fillWidth(2));
208 authorizationCmb
.addSelectionListener(new SelectionAdapter() {
209 private static final long serialVersionUID
= 1L;
212 public void widgetSelected(SelectionEvent e
) {
213 String chosenPrivStr
= ((Combo
) e
.getSource()).getText();
214 if (AUTH_TYPE_LABELS
.containsValue(chosenPrivStr
)) {
215 loop
: for (String key
: AUTH_TYPE_LABELS
.keySet()) {
216 if (AUTH_TYPE_LABELS
.get(key
).equals(chosenPrivStr
)) {
223 if (jcrPrivilege
!= null) {
224 descLbl
.setText(AUTH_TYPE_DESC
.get(jcrPrivilege
));
225 composite
.layout(true, true);
231 setControl(composite
);
234 public void modifyText(ModifyEvent event
) {
235 String message
= checkComplete();
237 setMessage(message
, WizardPage
.ERROR
);
239 setMessage("Complete", WizardPage
.INFORMATION
);
240 setPageComplete(true);
244 /** @return error message or null if complete */
245 protected String
checkComplete() {
246 if (chosenUser
== null)
247 return "Please choose a relevant group or user";
248 else if (userAdmin
.getRole(chosenUser
.getName()) == null)
249 return "Please choose a relevant group or user";
250 else if (jcrPrivilege
== null)
251 return "Please choose a relevant JCR privilege";
256 private Label
createBoldLabel(Composite parent
, String value
) {
257 Label label
= new Label(parent
, SWT
.RIGHT
);
258 label
.setText(" " + value
);
259 label
.setFont(EclipseUiUtils
.getBoldFont(parent
));
260 label
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.CENTER
, false, false));