]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms.ui.workbench/src/org/argeo/cms/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
.cms
.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
.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
;
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
, "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
);
88 public AddPrivilegeWizard(Session currentSession
, String path
, UserAdmin userAdmin
) {
90 this.userAdmin
= userAdmin
;
91 this.currentSession
= currentSession
;
92 this.targetPath
= path
;
96 public void addPages() {
98 setWindowTitle("Add privilege on " + targetPath
);
99 page
= new DefinePrivilegePage(userAdmin
, targetPath
);
101 } catch (Exception e
) {
102 throw new EclipseUiException("Cannot add page to wizard ", e
);
107 public boolean performFinish() {
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
);
119 private class DefinePrivilegePage
extends WizardPage
implements ModifyListener
{
120 private static final long serialVersionUID
= 8084431378762283920L;
123 final private UserAdmin userAdmin
;
125 public DefinePrivilegePage(UserAdmin userAdmin
, String path
) {
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.");
132 public void createControl(Composite parent
) {
133 final Composite composite
= new Composite(parent
, SWT
.NONE
);
134 composite
.setLayout(new GridLayout(3, false));
137 createBoldLabel(composite
, "User or group name");
138 final Label groupNameLbl
= new Label(composite
, SWT
.LEAD
);
139 groupNameLbl
.setLayoutData(EclipseUiUtils
.fillWidth());
141 Link pickUpLk
= new Link(composite
, SWT
.LEFT
);
142 pickUpLk
.setText(" <a>Change</a> ");
144 createBoldLabel(composite
, "User or group DN");
145 final Text groupNameTxt
= new Text(composite
, SWT
.LEAD
| SWT
.BORDER
);
146 groupNameTxt
.setLayoutData(EclipseUiUtils
.fillWidth(2));
148 pickUpLk
.addSelectionListener(new SelectionAdapter() {
149 private static final long serialVersionUID
= 1L;
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());
162 groupNameTxt
.addFocusListener(new FocusListener() {
163 private static final long serialVersionUID
= 1965498600105667738L;
166 public void focusLost(FocusEvent event
) {
167 String dn
= groupNameTxt
.getText();
168 if (EclipseUiUtils
.isEmpty(dn
))
171 User newChosen
= null;
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?");
179 groupNameTxt
.setFocus();
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?");
188 groupNameTxt
.setFocus();
192 chosenUser
= newChosen
;
193 groupNameLbl
.setText(UserAdminUtils
.getCommonName(chosenUser
));
197 private void resetOnFail() {
198 String oldDn
= chosenUser
== null ?
"" : chosenUser
.getName();
199 groupNameTxt
.setText(oldDn
);
203 public void focusGained(FocusEvent event
) {
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));
216 authorizationCmb
.addSelectionListener(new SelectionAdapter() {
217 private static final long serialVersionUID
= 1L;
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
)) {
231 if (jcrPrivilege
!= null) {
232 descLbl
.setText(AUTH_TYPE_DESC
.get(jcrPrivilege
));
233 composite
.layout(true, true);
239 setControl(composite
);
242 public void modifyText(ModifyEvent event
) {
243 String message
= checkComplete();
245 setMessage(message
, WizardPage
.ERROR
);
247 setMessage("Complete", WizardPage
.INFORMATION
);
248 setPageComplete(true);
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";
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));