]> git.argeo.org Git - lgpl/argeo-commons.git/blob - commands/NewGroup.java
Prepare next development cycle
[lgpl/argeo-commons.git] / 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.internal.commands;
17
18 import java.util.Dictionary;
19 import java.util.List;
20
21 import org.argeo.ArgeoException;
22 import org.argeo.eclipse.ui.EclipseUiUtils;
23 import org.argeo.eclipse.ui.dialogs.ErrorFeedback;
24 import org.argeo.jcr.ArgeoNames;
25 import org.argeo.osgi.useradmin.LdifName;
26 import org.argeo.security.ui.admin.SecurityAdminPlugin;
27 import org.argeo.security.ui.admin.internal.UiAdminUtils;
28 import org.argeo.security.ui.admin.internal.UserAdminWrapper;
29 import org.eclipse.core.commands.AbstractHandler;
30 import org.eclipse.core.commands.ExecutionEvent;
31 import org.eclipse.core.commands.ExecutionException;
32 import org.eclipse.jface.wizard.Wizard;
33 import org.eclipse.jface.wizard.WizardDialog;
34 import org.eclipse.jface.wizard.WizardPage;
35 import org.eclipse.swt.SWT;
36 import org.eclipse.swt.events.ModifyEvent;
37 import org.eclipse.swt.events.ModifyListener;
38 import org.eclipse.swt.layout.GridData;
39 import org.eclipse.swt.layout.GridLayout;
40 import org.eclipse.swt.widgets.Combo;
41 import org.eclipse.swt.widgets.Composite;
42 import org.eclipse.swt.widgets.Label;
43 import org.eclipse.swt.widgets.Text;
44 import org.eclipse.ui.handlers.HandlerUtil;
45 import org.osgi.service.useradmin.Group;
46 import org.osgi.service.useradmin.Role;
47 import org.osgi.service.useradmin.UserAdminEvent;
48
49 /** Create a new group. */
50 public class NewGroup extends AbstractHandler {
51 public final static String ID = SecurityAdminPlugin.PLUGIN_ID + ".newGroup";
52
53 /* DEPENDENCY INJECTION */
54 private UserAdminWrapper userAdminWrapper;
55
56 public Object execute(ExecutionEvent event) throws ExecutionException {
57 NewGroupWizard newGroupWizard = new NewGroupWizard();
58 newGroupWizard.setWindowTitle("Group creation");
59 WizardDialog dialog = new WizardDialog(
60 HandlerUtil.getActiveShell(event), newGroupWizard);
61 dialog.open();
62 return null;
63 }
64
65 private class NewGroupWizard extends Wizard {
66
67 // pages
68 private MainGroupInfoWizardPage mainGroupInfo;
69
70 // End user fields
71 private Text dNameTxt, commonNameTxt, descriptionTxt;
72 private Combo baseDnCmb;
73
74 public NewGroupWizard() {
75 }
76
77 @Override
78 public void addPages() {
79 mainGroupInfo = new MainGroupInfoWizardPage();
80 addPage(mainGroupInfo);
81 }
82
83 @SuppressWarnings({ "rawtypes", "unchecked" })
84 @Override
85 public boolean performFinish() {
86 if (!canFinish())
87 return false;
88 String commonName = commonNameTxt.getText();
89 try {
90 userAdminWrapper.beginTransactionIfNeeded();
91 Group group = (Group) userAdminWrapper.getUserAdmin()
92 .createRole(getDn(commonName), Role.GROUP);
93 Dictionary props = group.getProperties();
94 String descStr = descriptionTxt.getText();
95 if (UiAdminUtils.notNull(descStr))
96 props.put(LdifName.description.name(), descStr);
97 userAdminWrapper.notifyListeners(new UserAdminEvent(null,
98 UserAdminEvent.ROLE_CREATED, group));
99 return true;
100 } catch (Exception e) {
101 ErrorFeedback.show("Cannot create new group " + commonName, e);
102 return false;
103 }
104 }
105
106 private class MainGroupInfoWizardPage extends WizardPage implements
107 ModifyListener, ArgeoNames {
108 private static final long serialVersionUID = -3150193365151601807L;
109
110 public MainGroupInfoWizardPage() {
111 super("Main");
112 setTitle("General information");
113 setMessage("Please choose a domain, provide a common name "
114 + "and a free description");
115 }
116
117 @Override
118 public void createControl(Composite parent) {
119 Composite bodyCmp = new Composite(parent, SWT.NONE);
120 bodyCmp.setLayout(new GridLayout(2, false));
121 dNameTxt = EclipseUiUtils.createGridLT(bodyCmp,
122 "Distinguished name"); // Read-only -> no listener
123 dNameTxt.setEnabled(false);
124
125 baseDnCmb = createGridLC(bodyCmp, "Base DN");
126 // Initialise before adding the listener top avoid NPE
127 initialiseDnCmb(baseDnCmb);
128 baseDnCmb.addModifyListener(this);
129 baseDnCmb.addModifyListener(new ModifyListener() {
130 private static final long serialVersionUID = -1435351236582736843L;
131
132 @Override
133 public void modifyText(ModifyEvent event) {
134 String name = commonNameTxt.getText();
135 dNameTxt.setText(getDn(name));
136 }
137 });
138
139 commonNameTxt = EclipseUiUtils.createGridLT(bodyCmp,
140 "Common name", this);
141 commonNameTxt.addModifyListener(new ModifyListener() {
142 private static final long serialVersionUID = -1435351236582736843L;
143
144 @Override
145 public void modifyText(ModifyEvent event) {
146 String name = commonNameTxt.getText();
147 if (name.trim().equals("")) {
148 dNameTxt.setText("");
149 } else {
150 dNameTxt.setText(getDn(name));
151 }
152 }
153 });
154
155 Label descLbl = new Label(bodyCmp, SWT.LEAD);
156 descLbl.setText("Description");
157 descLbl.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false,
158 false));
159 descriptionTxt = new Text(bodyCmp, SWT.LEAD | SWT.MULTI
160 | SWT.WRAP | SWT.BORDER);
161 descriptionTxt.setLayoutData(EclipseUiUtils.fillAll());
162 descriptionTxt.addModifyListener(this);
163
164 setControl(bodyCmp);
165
166 // Initialize buttons
167 setPageComplete(false);
168 getContainer().updateButtons();
169 }
170
171 @Override
172 public void modifyText(ModifyEvent event) {
173 String message = checkComplete();
174 if (message != null) {
175 setMessage(message, WizardPage.ERROR);
176 setPageComplete(false);
177 } else {
178 setMessage("Complete", WizardPage.INFORMATION);
179 setPageComplete(true);
180 }
181 getContainer().updateButtons();
182 }
183
184 /** @return error message or null if complete */
185 protected String checkComplete() {
186 String name = commonNameTxt.getText();
187
188 if (name.trim().equals(""))
189 return "Common name must not be empty";
190 Role role = userAdminWrapper.getUserAdmin()
191 .getRole(getDn(name));
192 if (role != null)
193 return "Group " + name + " already exists";
194 return null;
195 }
196
197 @Override
198 public void setVisible(boolean visible) {
199 super.setVisible(visible);
200 if (visible)
201 if (baseDnCmb.getSelectionIndex() == -1)
202 baseDnCmb.setFocus();
203 else
204 commonNameTxt.setFocus();
205 }
206 }
207
208 private String getDn(String cn) {
209 return "cn=" + cn + ",ou=groups," + baseDnCmb.getText();
210 }
211
212 private void initialiseDnCmb(Combo combo) {
213 List<String> dns = userAdminWrapper.getKnownBaseDns(true);
214 if (dns.isEmpty())
215 throw new ArgeoException(
216 "No writable base dn found. Cannot create user");
217 combo.setItems(dns.toArray(new String[0]));
218 if (dns.size() == 1)
219 combo.select(0);
220 }
221
222 }
223
224 private Combo createGridLC(Composite parent, String label) {
225 Label lbl = new Label(parent, SWT.LEAD);
226 lbl.setText(label);
227 lbl.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
228 Combo combo = new Combo(parent, SWT.LEAD | SWT.BORDER | SWT.READ_ONLY);
229 combo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
230 return combo;
231 }
232
233 /* DEPENDENCY INJECTION */
234 public void setUserAdminWrapper(UserAdminWrapper userAdminWrapper) {
235 this.userAdminWrapper = userAdminWrapper;
236 }
237 }