]> git.argeo.org Git - lgpl/argeo-commons.git/blob - internal/parts/ChangeRightsWizard.java
Prepare next development cycle
[lgpl/argeo-commons.git] / internal / parts / ChangeRightsWizard.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.jcr.internal.parts;
17
18 import javax.jcr.RepositoryException;
19 import javax.jcr.Session;
20 import javax.jcr.security.Privilege;
21
22 import org.argeo.ArgeoException;
23 import org.argeo.eclipse.ui.EclipseUiUtils;
24 import org.argeo.eclipse.ui.workbench.users.PickUpGroupDialog;
25 import org.argeo.jcr.JcrUtils;
26 import org.eclipse.jface.window.Window;
27 import org.eclipse.jface.wizard.Wizard;
28 import org.eclipse.jface.wizard.WizardPage;
29 import org.eclipse.swt.SWT;
30 import org.eclipse.swt.events.ModifyEvent;
31 import org.eclipse.swt.events.ModifyListener;
32 import org.eclipse.swt.events.SelectionAdapter;
33 import org.eclipse.swt.events.SelectionEvent;
34 import org.eclipse.swt.layout.GridData;
35 import org.eclipse.swt.layout.GridLayout;
36 import org.eclipse.swt.widgets.Combo;
37 import org.eclipse.swt.widgets.Composite;
38 import org.eclipse.swt.widgets.Label;
39 import org.eclipse.swt.widgets.Link;
40 import org.eclipse.swt.widgets.Text;
41 import org.osgi.service.useradmin.UserAdmin;
42
43 /** Add Jcr privileges to the chosen user group on a given node */
44 public class ChangeRightsWizard extends Wizard {
45
46 private UserAdmin userAdmin;
47 private Session currentSession;
48 private String path;
49
50 // This page widget
51 private DefinePrivilegePage page;
52
53 // USABLE SHORTCUTS
54 protected final static String[] validAuthType = { Privilege.JCR_READ,
55 Privilege.JCR_WRITE, Privilege.JCR_ALL };
56
57 public ChangeRightsWizard(Session currentSession, String path,
58 UserAdmin userAdmin) {
59 super();
60 this.userAdmin = userAdmin;
61 this.currentSession = currentSession;
62 this.path = path;
63 }
64
65 @Override
66 public void addPages() {
67 try {
68 page = new DefinePrivilegePage(userAdmin, path);
69 addPage(page);
70 } catch (Exception e) {
71 throw new ArgeoException("Cannot add page to wizard ", e);
72 }
73 }
74
75 @Override
76 public boolean performFinish() {
77 if (!canFinish())
78 return false;
79 try {
80 JcrUtils.addPrivilege(currentSession, path, page.getGroupName(),
81 page.getAuthTypeStr());
82 } catch (RepositoryException re) {
83 throw new ArgeoException(
84 "Unexpected error while setting privileges", re);
85 }
86 return true;
87 }
88
89 private class DefinePrivilegePage extends WizardPage implements ModifyListener {
90 private static final long serialVersionUID = 8084431378762283920L;
91
92 // Context
93 final private UserAdmin userAdmin;
94
95 // This page widget
96 private Text groupNameTxt;
97 private Combo authorizationCmb;
98
99 public DefinePrivilegePage(UserAdmin userAdmin, String path) {
100 super("Main");
101 this.userAdmin = userAdmin;
102 setTitle("Define the privilege to apply to " + path);
103 }
104
105 public void createControl(Composite parent) {
106 // specify subject
107 Composite composite = new Composite(parent, SWT.NONE);
108 composite.setLayout(new GridLayout(3, false));
109 Label lbl = new Label(composite, SWT.LEAD);
110 lbl.setText("Group name");
111 lbl.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
112 groupNameTxt = new Text(composite, SWT.LEAD | SWT.BORDER);
113 groupNameTxt.setLayoutData(EclipseUiUtils.fillWidth());
114 if (groupNameTxt != null)
115 groupNameTxt.addModifyListener(this);
116
117 Link pickUpLk = new Link(composite, SWT.LEFT);
118 pickUpLk.setText(" <a>Pick up</a> ");
119 pickUpLk.addSelectionListener(new SelectionAdapter() {
120 private static final long serialVersionUID = 1L;
121
122 @Override
123 public void widgetSelected(SelectionEvent e) {
124 PickUpGroupDialog dialog = new PickUpGroupDialog(
125 getShell(), "Choose a group", userAdmin);
126 if (dialog.open() == Window.OK)
127 groupNameTxt.setText(dialog.getSelected());
128 }
129
130 });
131
132 // Choose rigths
133 new Label(composite, SWT.NONE)
134 .setText("Choose corresponding rights");
135 authorizationCmb = new Combo(composite, SWT.BORDER | SWT.V_SCROLL);
136 authorizationCmb.setItems(validAuthType);
137 authorizationCmb.setLayoutData(EclipseUiUtils.fillWidth(2));
138 authorizationCmb.select(0);
139
140 // Compulsory
141 setControl(composite);
142 }
143
144 protected String getGroupName() {
145 return groupNameTxt.getText();
146 }
147
148 protected String getAuthTypeStr() {
149 return authorizationCmb.getItem(authorizationCmb
150 .getSelectionIndex());
151 }
152
153 public void modifyText(ModifyEvent event) {
154 String message = checkComplete();
155 if (message != null)
156 setMessage(message, WizardPage.ERROR);
157 else {
158 setMessage("Complete", WizardPage.INFORMATION);
159 setPageComplete(true);
160 }
161 }
162
163 /** @return error message or null if complete */
164 protected String checkComplete() {
165 String groupStr = groupNameTxt.getText();
166 if (groupStr == null || "".equals(groupStr))
167 return "Please enter the name of the corresponding group.";
168 return null;
169 }
170 }
171 }