]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.security.ui.admin/src/org/argeo/security/ui/admin/commands/NewGroup.java
First draft of a drag and drop implementation to manage group. Implement create group...
[lgpl/argeo-commons.git] / org.argeo.security.ui.admin / src / org / argeo / security / ui / admin / commands / NewGroup.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.security.ui.admin.commands;
17
18 import java.util.Dictionary;
19
20 import javax.transaction.Status;
21 import javax.transaction.UserTransaction;
22
23 import org.argeo.ArgeoException;
24 import org.argeo.eclipse.ui.EclipseUiUtils;
25 import org.argeo.eclipse.ui.dialogs.ErrorFeedback;
26 import org.argeo.jcr.ArgeoNames;
27 import org.argeo.security.ui.admin.SecurityAdminPlugin;
28 import org.argeo.security.ui.admin.internal.UiAdminUtils;
29 import org.argeo.security.ui.admin.internal.UserAdminConstants;
30 import org.argeo.security.ui.admin.views.GroupsView;
31 import org.eclipse.core.commands.AbstractHandler;
32 import org.eclipse.core.commands.ExecutionEvent;
33 import org.eclipse.core.commands.ExecutionException;
34 import org.eclipse.jface.window.Window;
35 import org.eclipse.jface.wizard.Wizard;
36 import org.eclipse.jface.wizard.WizardDialog;
37 import org.eclipse.jface.wizard.WizardPage;
38 import org.eclipse.swt.SWT;
39 import org.eclipse.swt.events.ModifyEvent;
40 import org.eclipse.swt.events.ModifyListener;
41 import org.eclipse.swt.layout.GridData;
42 import org.eclipse.swt.layout.GridLayout;
43 import org.eclipse.swt.widgets.Composite;
44 import org.eclipse.swt.widgets.Label;
45 import org.eclipse.swt.widgets.Text;
46 import org.eclipse.ui.IWorkbenchPage;
47 import org.eclipse.ui.IWorkbenchPart;
48 import org.eclipse.ui.IWorkbenchWindow;
49 import org.eclipse.ui.handlers.HandlerUtil;
50 import org.osgi.service.useradmin.Group;
51 import org.osgi.service.useradmin.Role;
52 import org.osgi.service.useradmin.UserAdmin;
53
54 /** Create a new group. */
55 public class NewGroup extends AbstractHandler {
56 public final static String ID = SecurityAdminPlugin.PLUGIN_ID + ".newGroup";
57
58 private UserAdmin userAdmin;
59 private UserTransaction userTransaction;
60
61 // TODO implement a dynamic choice of the base dn
62 private String getDn(String cn) {
63 return "cn=" + cn + ",dc=example,dc=com";
64 }
65
66 public Object execute(ExecutionEvent event) throws ExecutionException {
67 NewGroupWizard newGroupWizard = new NewGroupWizard();
68 WizardDialog dialog = new WizardDialog(
69 HandlerUtil.getActiveShell(event), newGroupWizard);
70 dialog.setTitle("Create a new group");
71
72 // Force refresh until the listener are implemented
73 if (Window.OK == dialog.open())
74 forceRefresh(event);
75 return null;
76 }
77
78 private void forceRefresh(ExecutionEvent event) {
79 IWorkbenchWindow iww = HandlerUtil.getActiveWorkbenchWindow(event);
80 if (iww == null)
81 return;
82 IWorkbenchPage activePage = iww.getActivePage();
83 IWorkbenchPart part = activePage.getActivePart();
84 if (part instanceof GroupsView)
85 ((GroupsView) part).refresh();
86 }
87
88 private class NewGroupWizard extends Wizard {
89
90 // pages
91 private MainGroupInfoWizardPage mainGroupInfo;
92
93 // End user fields
94 private Text dNameTxt, commonNameTxt, descriptionTxt;
95
96 public NewGroupWizard() {
97 }
98
99 @Override
100 public void addPages() {
101 mainGroupInfo = new MainGroupInfoWizardPage();
102 addPage(mainGroupInfo);
103
104 setWindowTitle("Create a new group");
105 // mainGroupInfo.setMessage(message, WizardPage.WARNING);
106 }
107
108 @SuppressWarnings({ "rawtypes", "unchecked" })
109 @Override
110 public boolean performFinish() {
111 if (!canFinish())
112 return false;
113 String commonName = commonNameTxt.getText();
114
115 // Begin transaction if needed
116 try {
117 if (userTransaction.getStatus() == Status.STATUS_NO_TRANSACTION)
118 userTransaction.begin();
119 } catch (Exception e) {
120 throw new ArgeoException("Unable to start "
121 + "transaction to create user " + commonName, e);
122 }
123
124 try {
125 Group user = (Group) userAdmin.createRole(getDn(commonName),
126 Role.GROUP);
127 Dictionary props = user.getProperties();
128 String descStr = descriptionTxt.getText();
129 if (UiAdminUtils.notNull(descStr))
130 props.put(UserAdminConstants.KEY_DESC, descStr);
131 return true;
132 } catch (Exception e) {
133 ErrorFeedback.show("Cannot create new group " + commonName, e);
134 return false;
135 }
136 }
137
138 private class MainGroupInfoWizardPage extends WizardPage implements
139 ModifyListener, ArgeoNames {
140 private static final long serialVersionUID = -3150193365151601807L;
141
142 public MainGroupInfoWizardPage() {
143 super("Main");
144 setTitle("General information");
145 setMessage("Please provide a common name and a free description");
146 }
147
148 @Override
149 public void createControl(Composite parent) {
150 Composite bodyCmp = new Composite(parent, SWT.NONE);
151 bodyCmp.setLayout(new GridLayout(2, false));
152 dNameTxt = EclipseUiUtils.createGridLT(bodyCmp,
153 "Distinguished name", this);
154 dNameTxt.setEnabled(false);
155 commonNameTxt = EclipseUiUtils.createGridLT(bodyCmp,
156 "Common name", this);
157 commonNameTxt.addModifyListener(new ModifyListener() {
158 private static final long serialVersionUID = -1435351236582736843L;
159
160 @Override
161 public void modifyText(ModifyEvent event) {
162 String name = commonNameTxt.getText();
163 if (name.trim().equals("")) {
164 dNameTxt.setText("");
165 } else {
166 dNameTxt.setText(getDn(name));
167 }
168 }
169 });
170
171 Label descLbl = new Label(bodyCmp, SWT.LEAD);
172 descLbl.setText("Description");
173 descLbl.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false,
174 false));
175 descriptionTxt = new Text(bodyCmp, SWT.LEAD | SWT.MULTI
176 | SWT.WRAP | SWT.BORDER);
177 descriptionTxt.setLayoutData(EclipseUiUtils.fillAll());
178 descriptionTxt.addModifyListener(this);
179
180 setControl(bodyCmp);
181
182 // Initialize buttons
183 setPageComplete(false);
184 getContainer().updateButtons();
185 }
186
187 @Override
188 public void modifyText(ModifyEvent event) {
189 String message = checkComplete();
190 if (message != null) {
191 setMessage(message, WizardPage.ERROR);
192 setPageComplete(false);
193 } else {
194 setMessage("Complete", WizardPage.INFORMATION);
195 setPageComplete(true);
196 }
197 getContainer().updateButtons();
198 }
199
200 /** @return error message or null if complete */
201 protected String checkComplete() {
202 String name = commonNameTxt.getText();
203
204 if (name.trim().equals(""))
205 return "Common name must not be empty";
206 Role role = userAdmin.getRole(getDn(name));
207 if (role != null)
208 return "Group " + name + " already exists";
209 return null;
210 }
211
212 @Override
213 public void setVisible(boolean visible) {
214 super.setVisible(visible);
215 if (visible)
216 commonNameTxt.setFocus();
217 }
218 }
219 }
220
221 /* DEPENDENCY INJECTION */
222 public void setUserAdmin(UserAdmin userAdmin) {
223 this.userAdmin = userAdmin;
224 }
225
226 public void setUserTransaction(UserTransaction userTransaction) {
227 this.userTransaction = userTransaction;
228 }
229 }