]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms.e4/src/org/argeo/cms/e4/users/handlers/NewGroup.java
Move DocBook support and Example to Argeo Connect
[lgpl/argeo-commons.git] / org.argeo.cms.e4 / src / org / argeo / cms / e4 / users / handlers / 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.cms.e4.users.handlers;
17
18 import java.util.Dictionary;
19 import java.util.Map;
20
21 import javax.inject.Inject;
22
23 import org.argeo.cms.ArgeoNames;
24 import org.argeo.cms.CmsException;
25 import org.argeo.cms.e4.users.UserAdminWrapper;
26 import org.argeo.eclipse.ui.EclipseUiUtils;
27 import org.argeo.eclipse.ui.dialogs.ErrorFeedback;
28 import org.argeo.naming.LdapAttrs;
29 import org.argeo.osgi.useradmin.UserAdminConf;
30 import org.eclipse.e4.core.di.annotations.Execute;
31 import org.eclipse.jface.wizard.Wizard;
32 import org.eclipse.jface.wizard.WizardDialog;
33 import org.eclipse.jface.wizard.WizardPage;
34 import org.eclipse.swt.SWT;
35 import org.eclipse.swt.events.FocusEvent;
36 import org.eclipse.swt.events.FocusListener;
37 import org.eclipse.swt.layout.GridData;
38 import org.eclipse.swt.layout.GridLayout;
39 import org.eclipse.swt.widgets.Combo;
40 import org.eclipse.swt.widgets.Composite;
41 import org.eclipse.swt.widgets.Display;
42 import org.eclipse.swt.widgets.Label;
43 import org.eclipse.swt.widgets.Text;
44 import org.osgi.service.useradmin.Group;
45 import org.osgi.service.useradmin.Role;
46 import org.osgi.service.useradmin.UserAdminEvent;
47
48 /** Create a new group */
49 public class NewGroup {
50 // public final static String ID = WorkbenchUiPlugin.PLUGIN_ID + ".newGroup";
51
52 /* DEPENDENCY INJECTION */
53 @Inject
54 private UserAdminWrapper userAdminWrapper;
55
56 @Execute
57 public Object execute() {
58 NewGroupWizard newGroupWizard = new NewGroupWizard();
59 newGroupWizard.setWindowTitle("Group creation");
60 WizardDialog dialog = new WizardDialog(Display.getCurrent().getActiveShell(), 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().createRole(dn, Role.GROUP);
93 Dictionary props = group.getProperties();
94 String descStr = descriptionTxt.getText();
95 if (EclipseUiUtils.notEmpty(descStr))
96 props.put(LdapAttrs.description.name(), descStr);
97 userAdminWrapper.commitOrNotifyTransactionStateChange();
98 userAdminWrapper.notifyListeners(new UserAdminEvent(null, 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 FocusListener, ArgeoNames {
107 private static final long serialVersionUID = -3150193365151601807L;
108
109 public MainGroupInfoWizardPage() {
110 super("Main");
111 setTitle("General information");
112 setMessage("Please choose a domain, provide a common name " + "and a free description");
113 }
114
115 @Override
116 public void createControl(Composite parent) {
117 Composite bodyCmp = new Composite(parent, SWT.NONE);
118 setControl(bodyCmp);
119 bodyCmp.setLayout(new GridLayout(2, false));
120
121 dNameTxt = EclipseUiUtils.createGridLT(bodyCmp, "Distinguished name");
122 dNameTxt.setEnabled(false);
123
124 baseDnCmb = createGridLC(bodyCmp, "Base DN");
125 // Initialise before adding the listener to avoid NPE
126 initialiseDnCmb(baseDnCmb);
127 baseDnCmb.addFocusListener(this);
128
129 commonNameTxt = EclipseUiUtils.createGridLT(bodyCmp, "Common name");
130 commonNameTxt.addFocusListener(this);
131
132 Label descLbl = new Label(bodyCmp, SWT.LEAD);
133 descLbl.setText("Description");
134 descLbl.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, false));
135 descriptionTxt = new Text(bodyCmp, SWT.LEAD | SWT.MULTI | SWT.WRAP | SWT.BORDER);
136 descriptionTxt.setLayoutData(EclipseUiUtils.fillAll());
137 descriptionTxt.addFocusListener(this);
138
139 // Initialize buttons
140 setPageComplete(false);
141 getContainer().updateButtons();
142 }
143
144 @Override
145 public void focusLost(FocusEvent event) {
146 String name = commonNameTxt.getText();
147 if (EclipseUiUtils.isEmpty(name))
148 dNameTxt.setText("");
149 else
150 dNameTxt.setText(getDn(name));
151
152 String message = checkComplete();
153 if (message != null) {
154 setMessage(message, WizardPage.ERROR);
155 setPageComplete(false);
156 } else {
157 setMessage("Complete", WizardPage.INFORMATION);
158 setPageComplete(true);
159 }
160 getContainer().updateButtons();
161 }
162
163 @Override
164 public void focusGained(FocusEvent event) {
165 }
166
167 /** @return the error message or null if complete */
168 protected String checkComplete() {
169 String name = commonNameTxt.getText();
170
171 if (name.trim().equals(""))
172 return "Common name must not be empty";
173 Role role = userAdminWrapper.getUserAdmin().getRole(getDn(name));
174 if (role != null)
175 return "Group " + name + " already exists";
176 return null;
177 }
178
179 @Override
180 public void setVisible(boolean visible) {
181 super.setVisible(visible);
182 if (visible)
183 if (baseDnCmb.getSelectionIndex() == -1)
184 baseDnCmb.setFocus();
185 else
186 commonNameTxt.setFocus();
187 }
188 }
189
190 private Map<String, String> getDns() {
191 return userAdminWrapper.getKnownBaseDns(true);
192 }
193
194 private String getDn(String cn) {
195 Map<String, String> dns = getDns();
196 String bdn = baseDnCmb.getText();
197 if (EclipseUiUtils.notEmpty(bdn)) {
198 Dictionary<String, ?> props = UserAdminConf.uriAsProperties(dns.get(bdn));
199 String dn = LdapAttrs.cn.name() + "=" + cn + "," + UserAdminConf.groupBase.getValue(props) + "," + bdn;
200 return dn;
201 }
202 return null;
203 }
204
205 private void initialiseDnCmb(Combo combo) {
206 Map<String, String> dns = userAdminWrapper.getKnownBaseDns(true);
207 if (dns.isEmpty())
208 throw new CmsException("No writable base dn found. Cannot create group");
209 combo.setItems(dns.keySet().toArray(new String[0]));
210 if (dns.size() == 1)
211 combo.select(0);
212 }
213 }
214
215 private Combo createGridLC(Composite parent, String label) {
216 Label lbl = new Label(parent, SWT.LEAD);
217 lbl.setText(label);
218 lbl.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
219 Combo combo = new Combo(parent, SWT.LEAD | SWT.BORDER | SWT.READ_ONLY);
220 combo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
221 return combo;
222 }
223
224 /* DEPENDENCY INJECTION */
225 public void setUserAdminWrapper(UserAdminWrapper userAdminWrapper) {
226 this.userAdminWrapper = userAdminWrapper;
227 }
228 }