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
.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
;
48 /** Create a new group */
49 public class NewGroup
{
50 // public final static String ID = WorkbenchUiPlugin.PLUGIN_ID + ".newGroup";
52 /* DEPENDENCY INJECTION */
54 private UserAdminWrapper userAdminWrapper
;
57 public Object
execute() {
58 NewGroupWizard newGroupWizard
= new NewGroupWizard();
59 newGroupWizard
.setWindowTitle("Group creation");
60 WizardDialog dialog
= new WizardDialog(Display
.getCurrent().getActiveShell(), newGroupWizard
);
65 private class NewGroupWizard
extends Wizard
{
68 private MainGroupInfoWizardPage mainGroupInfo
;
71 private Text dNameTxt
, commonNameTxt
, descriptionTxt
;
72 private Combo baseDnCmb
;
74 public NewGroupWizard() {
78 public void addPages() {
79 mainGroupInfo
= new MainGroupInfoWizardPage();
80 addPage(mainGroupInfo
);
83 @SuppressWarnings({ "rawtypes", "unchecked" })
85 public boolean performFinish() {
88 String commonName
= commonNameTxt
.getText();
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
));
100 } catch (Exception e
) {
101 ErrorFeedback
.show("Cannot create new group " + commonName
, e
);
106 private class MainGroupInfoWizardPage
extends WizardPage
implements FocusListener
, ArgeoNames
{
107 private static final long serialVersionUID
= -3150193365151601807L;
109 public MainGroupInfoWizardPage() {
111 setTitle("General information");
112 setMessage("Please choose a domain, provide a common name " + "and a free description");
116 public void createControl(Composite parent
) {
117 Composite bodyCmp
= new Composite(parent
, SWT
.NONE
);
119 bodyCmp
.setLayout(new GridLayout(2, false));
121 dNameTxt
= EclipseUiUtils
.createGridLT(bodyCmp
, "Distinguished name");
122 dNameTxt
.setEnabled(false);
124 baseDnCmb
= createGridLC(bodyCmp
, "Base DN");
125 // Initialise before adding the listener to avoid NPE
126 initialiseDnCmb(baseDnCmb
);
127 baseDnCmb
.addFocusListener(this);
129 commonNameTxt
= EclipseUiUtils
.createGridLT(bodyCmp
, "Common name");
130 commonNameTxt
.addFocusListener(this);
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);
139 // Initialize buttons
140 setPageComplete(false);
141 getContainer().updateButtons();
145 public void focusLost(FocusEvent event
) {
146 String name
= commonNameTxt
.getText();
147 if (EclipseUiUtils
.isEmpty(name
))
148 dNameTxt
.setText("");
150 dNameTxt
.setText(getDn(name
));
152 String message
= checkComplete();
153 if (message
!= null) {
154 setMessage(message
, WizardPage
.ERROR
);
155 setPageComplete(false);
157 setMessage("Complete", WizardPage
.INFORMATION
);
158 setPageComplete(true);
160 getContainer().updateButtons();
164 public void focusGained(FocusEvent event
) {
167 /** @return the error message or null if complete */
168 protected String
checkComplete() {
169 String name
= commonNameTxt
.getText();
171 if (name
.trim().equals(""))
172 return "Common name must not be empty";
173 Role role
= userAdminWrapper
.getUserAdmin().getRole(getDn(name
));
175 return "Group " + name
+ " already exists";
180 public void setVisible(boolean visible
) {
181 super.setVisible(visible
);
183 if (baseDnCmb
.getSelectionIndex() == -1)
184 baseDnCmb
.setFocus();
186 commonNameTxt
.setFocus();
190 private Map
<String
, String
> getDns() {
191 return userAdminWrapper
.getKnownBaseDns(true);
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
;
205 private void initialiseDnCmb(Combo combo
) {
206 Map
<String
, String
> dns
= userAdminWrapper
.getKnownBaseDns(true);
208 throw new CmsException("No writable base dn found. Cannot create group");
209 combo
.setItems(dns
.keySet().toArray(new String
[0]));
215 private Combo
createGridLC(Composite parent
, String label
) {
216 Label lbl
= new Label(parent
, SWT
.LEAD
);
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));
224 /* DEPENDENCY INJECTION */
225 public void setUserAdminWrapper(UserAdminWrapper userAdminWrapper
) {
226 this.userAdminWrapper
= userAdminWrapper
;