]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.security.ui.admin/src/org/argeo/security/ui/admin/internal/commands/NewGroup.java
Re-add org.argeo.cms.util.useradmin
[lgpl/argeo-commons.git] / org.argeo.security.ui.admin / src / org / argeo / security / ui / admin / internal / 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.Map;
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.osgi.useradmin.UserAdminConf;
27 import org.argeo.security.ui.admin.SecurityAdminPlugin;
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.FocusEvent;
37 import org.eclipse.swt.events.FocusListener;
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 // UI 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 String dn = getDn(commonName);
92 Group group = (Group) userAdminWrapper.getUserAdmin()
93 .createRole(dn, Role.GROUP);
94 Dictionary props = group.getProperties();
95 String descStr = descriptionTxt.getText();
96 if (EclipseUiUtils.notEmpty(descStr))
97 props.put(LdifName.description.name(), descStr);
98 userAdminWrapper.notifyListeners(new UserAdminEvent(null,
99 UserAdminEvent.ROLE_CREATED, group));
100 return true;
101 } catch (Exception e) {
102 ErrorFeedback.show("Cannot create new group " + commonName, e);
103 return false;
104 }
105 }
106
107 private class MainGroupInfoWizardPage extends WizardPage implements
108 FocusListener, ArgeoNames {
109 private static final long serialVersionUID = -3150193365151601807L;
110
111 public MainGroupInfoWizardPage() {
112 super("Main");
113 setTitle("General information");
114 setMessage("Please choose a domain, provide a common name "
115 + "and a free description");
116 }
117
118 @Override
119 public void createControl(Composite parent) {
120 Composite bodyCmp = new Composite(parent, SWT.NONE);
121 setControl(bodyCmp);
122 bodyCmp.setLayout(new GridLayout(2, false));
123
124 dNameTxt = EclipseUiUtils.createGridLT(bodyCmp,
125 "Distinguished name");
126 dNameTxt.setEnabled(false);
127
128 baseDnCmb = createGridLC(bodyCmp, "Base DN");
129 // Initialise before adding the listener to avoid NPE
130 initialiseDnCmb(baseDnCmb);
131 baseDnCmb.addFocusListener(this);
132
133 commonNameTxt = EclipseUiUtils.createGridLT(bodyCmp,
134 "Common name");
135 commonNameTxt.addFocusListener(this);
136
137 Label descLbl = new Label(bodyCmp, SWT.LEAD);
138 descLbl.setText("Description");
139 descLbl.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false,
140 false));
141 descriptionTxt = new Text(bodyCmp, SWT.LEAD | SWT.MULTI
142 | SWT.WRAP | SWT.BORDER);
143 descriptionTxt.setLayoutData(EclipseUiUtils.fillAll());
144 descriptionTxt.addFocusListener(this);
145
146 // Initialize buttons
147 setPageComplete(false);
148 getContainer().updateButtons();
149 }
150
151 @Override
152 public void focusLost(FocusEvent event) {
153 String name = commonNameTxt.getText();
154 if (EclipseUiUtils.isEmpty(name))
155 dNameTxt.setText("");
156 else
157 dNameTxt.setText(getDn(name));
158
159 String message = checkComplete();
160 if (message != null) {
161 setMessage(message, WizardPage.ERROR);
162 setPageComplete(false);
163 } else {
164 setMessage("Complete", WizardPage.INFORMATION);
165 setPageComplete(true);
166 }
167 getContainer().updateButtons();
168 }
169
170 @Override
171 public void focusGained(FocusEvent event) {
172 }
173
174 /** @return the error message or null if complete */
175 protected String checkComplete() {
176 String name = commonNameTxt.getText();
177
178 if (name.trim().equals(""))
179 return "Common name must not be empty";
180 Role role = userAdminWrapper.getUserAdmin()
181 .getRole(getDn(name));
182 if (role != null)
183 return "Group " + name + " already exists";
184 return null;
185 }
186
187 @Override
188 public void setVisible(boolean visible) {
189 super.setVisible(visible);
190 if (visible)
191 if (baseDnCmb.getSelectionIndex() == -1)
192 baseDnCmb.setFocus();
193 else
194 commonNameTxt.setFocus();
195 }
196 }
197
198 private Map<String, String> getDns() {
199 return userAdminWrapper.getKnownBaseDns(true);
200 }
201
202 private String getDn(String cn) {
203 Map<String, String> dns = getDns();
204 String bdn = baseDnCmb.getText();
205 if (EclipseUiUtils.notEmpty(bdn)) {
206 Dictionary<String, ?> props = UserAdminConf.uriAsProperties(dns
207 .get(bdn));
208 String dn = LdifName.cn.name() + "=" + cn + ","
209 + UserAdminConf.groupBase.getValue(props) + "," + bdn;
210 return dn;
211 }
212 return null;
213 }
214
215 private void initialiseDnCmb(Combo combo) {
216 Map<String, String> dns = userAdminWrapper.getKnownBaseDns(true);
217 if (dns.isEmpty())
218 throw new ArgeoException(
219 "No writable base dn found. Cannot create group");
220 combo.setItems(dns.keySet().toArray(new String[0]));
221 if (dns.size() == 1)
222 combo.select(0);
223 }
224 }
225
226 private Combo createGridLC(Composite parent, String label) {
227 Label lbl = new Label(parent, SWT.LEAD);
228 lbl.setText(label);
229 lbl.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
230 Combo combo = new Combo(parent, SWT.LEAD | SWT.BORDER | SWT.READ_ONLY);
231 combo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
232 return combo;
233 }
234
235 /* DEPENDENCY INJECTION */
236 public void setUserAdminWrapper(UserAdminWrapper userAdminWrapper) {
237 this.userAdminWrapper = userAdminWrapper;
238 }
239 }