2 * Copyright (C) 2007-2012 Argeo GmbH
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.argeo
.cms
.e4
.users
.handlers
;
18 import java
.util
.Dictionary
;
21 import javax
.inject
.Inject
;
23 import org
.argeo
.cms
.CmsException
;
24 import org
.argeo
.cms
.e4
.users
.UserAdminWrapper
;
25 import org
.argeo
.eclipse
.ui
.EclipseUiUtils
;
26 import org
.argeo
.eclipse
.ui
.dialogs
.ErrorFeedback
;
27 import org
.argeo
.naming
.LdapAttrs
;
28 import org
.argeo
.osgi
.useradmin
.UserAdminConf
;
29 import org
.eclipse
.e4
.core
.di
.annotations
.Execute
;
30 import org
.eclipse
.jface
.wizard
.Wizard
;
31 import org
.eclipse
.jface
.wizard
.WizardDialog
;
32 import org
.eclipse
.jface
.wizard
.WizardPage
;
33 import org
.eclipse
.swt
.SWT
;
34 import org
.eclipse
.swt
.events
.FocusEvent
;
35 import org
.eclipse
.swt
.events
.FocusListener
;
36 import org
.eclipse
.swt
.layout
.GridData
;
37 import org
.eclipse
.swt
.layout
.GridLayout
;
38 import org
.eclipse
.swt
.widgets
.Combo
;
39 import org
.eclipse
.swt
.widgets
.Composite
;
40 import org
.eclipse
.swt
.widgets
.Display
;
41 import org
.eclipse
.swt
.widgets
.Label
;
42 import org
.eclipse
.swt
.widgets
.Text
;
43 import org
.osgi
.service
.useradmin
.Group
;
44 import org
.osgi
.service
.useradmin
.Role
;
45 import org
.osgi
.service
.useradmin
.UserAdminEvent
;
47 /** Create a new group */
48 public class NewGroup
{
49 // public final static String ID = WorkbenchUiPlugin.PLUGIN_ID + ".newGroup";
51 /* DEPENDENCY INJECTION */
53 private UserAdminWrapper userAdminWrapper
;
56 public Object
execute() {
57 NewGroupWizard newGroupWizard
= new NewGroupWizard();
58 newGroupWizard
.setWindowTitle("Group creation");
59 WizardDialog dialog
= new WizardDialog(Display
.getCurrent().getActiveShell(), newGroupWizard
);
64 private class NewGroupWizard
extends Wizard
{
67 private MainGroupInfoWizardPage mainGroupInfo
;
70 private Text dNameTxt
, commonNameTxt
, descriptionTxt
;
71 private Combo baseDnCmb
;
73 public NewGroupWizard() {
77 public void addPages() {
78 mainGroupInfo
= new MainGroupInfoWizardPage();
79 addPage(mainGroupInfo
);
82 @SuppressWarnings({ "rawtypes", "unchecked" })
84 public boolean performFinish() {
87 String commonName
= commonNameTxt
.getText();
89 userAdminWrapper
.beginTransactionIfNeeded();
90 String dn
= getDn(commonName
);
91 Group group
= (Group
) userAdminWrapper
.getUserAdmin().createRole(dn
, Role
.GROUP
);
92 Dictionary props
= group
.getProperties();
93 String descStr
= descriptionTxt
.getText();
94 if (EclipseUiUtils
.notEmpty(descStr
))
95 props
.put(LdapAttrs
.description
.name(), descStr
);
96 userAdminWrapper
.commitOrNotifyTransactionStateChange();
97 userAdminWrapper
.notifyListeners(new UserAdminEvent(null, UserAdminEvent
.ROLE_CREATED
, group
));
99 } catch (Exception e
) {
100 ErrorFeedback
.show("Cannot create new group " + commonName
, e
);
105 private class MainGroupInfoWizardPage
extends WizardPage
implements FocusListener
{
106 private static final long serialVersionUID
= -3150193365151601807L;
108 public MainGroupInfoWizardPage() {
110 setTitle("General information");
111 setMessage("Please choose a domain, provide a common name " + "and a free description");
115 public void createControl(Composite parent
) {
116 Composite bodyCmp
= new Composite(parent
, SWT
.NONE
);
118 bodyCmp
.setLayout(new GridLayout(2, false));
120 dNameTxt
= EclipseUiUtils
.createGridLT(bodyCmp
, "Distinguished name");
121 dNameTxt
.setEnabled(false);
123 baseDnCmb
= createGridLC(bodyCmp
, "Base DN");
124 // Initialise before adding the listener to avoid NPE
125 initialiseDnCmb(baseDnCmb
);
126 baseDnCmb
.addFocusListener(this);
128 commonNameTxt
= EclipseUiUtils
.createGridLT(bodyCmp
, "Common name");
129 commonNameTxt
.addFocusListener(this);
131 Label descLbl
= new Label(bodyCmp
, SWT
.LEAD
);
132 descLbl
.setText("Description");
133 descLbl
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.TOP
, false, false));
134 descriptionTxt
= new Text(bodyCmp
, SWT
.LEAD
| SWT
.MULTI
| SWT
.WRAP
| SWT
.BORDER
);
135 descriptionTxt
.setLayoutData(EclipseUiUtils
.fillAll());
136 descriptionTxt
.addFocusListener(this);
138 // Initialize buttons
139 setPageComplete(false);
140 getContainer().updateButtons();
144 public void focusLost(FocusEvent event
) {
145 String name
= commonNameTxt
.getText();
146 if (EclipseUiUtils
.isEmpty(name
))
147 dNameTxt
.setText("");
149 dNameTxt
.setText(getDn(name
));
151 String message
= checkComplete();
152 if (message
!= null) {
153 setMessage(message
, WizardPage
.ERROR
);
154 setPageComplete(false);
156 setMessage("Complete", WizardPage
.INFORMATION
);
157 setPageComplete(true);
159 getContainer().updateButtons();
163 public void focusGained(FocusEvent event
) {
166 /** @return the error message or null if complete */
167 protected String
checkComplete() {
168 String name
= commonNameTxt
.getText();
170 if (name
.trim().equals(""))
171 return "Common name must not be empty";
172 Role role
= userAdminWrapper
.getUserAdmin().getRole(getDn(name
));
174 return "Group " + name
+ " already exists";
179 public void setVisible(boolean visible
) {
180 super.setVisible(visible
);
182 if (baseDnCmb
.getSelectionIndex() == -1)
183 baseDnCmb
.setFocus();
185 commonNameTxt
.setFocus();
189 private Map
<String
, String
> getDns() {
190 return userAdminWrapper
.getKnownBaseDns(true);
193 private String
getDn(String cn
) {
194 Map
<String
, String
> dns
= getDns();
195 String bdn
= baseDnCmb
.getText();
196 if (EclipseUiUtils
.notEmpty(bdn
)) {
197 Dictionary
<String
, ?
> props
= UserAdminConf
.uriAsProperties(dns
.get(bdn
));
198 String dn
= LdapAttrs
.cn
.name() + "=" + cn
+ "," + UserAdminConf
.groupBase
.getValue(props
) + "," + bdn
;
204 private void initialiseDnCmb(Combo combo
) {
205 Map
<String
, String
> dns
= userAdminWrapper
.getKnownBaseDns(true);
207 throw new CmsException("No writable base dn found. Cannot create group");
208 combo
.setItems(dns
.keySet().toArray(new String
[0]));
214 private Combo
createGridLC(Composite parent
, String label
) {
215 Label lbl
= new Label(parent
, SWT
.LEAD
);
217 lbl
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.CENTER
, false, false));
218 Combo combo
= new Combo(parent
, SWT
.LEAD
| SWT
.BORDER
| SWT
.READ_ONLY
);
219 combo
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
223 /* DEPENDENCY INJECTION */
224 public void setUserAdminWrapper(UserAdminWrapper userAdminWrapper
) {
225 this.userAdminWrapper
= userAdminWrapper
;