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
.security
.ui
.admin
.internal
.commands
;
18 import java
.util
.Dictionary
;
20 import org
.argeo
.eclipse
.ui
.EclipseUiUtils
;
21 import org
.argeo
.eclipse
.ui
.dialogs
.ErrorFeedback
;
22 import org
.argeo
.jcr
.ArgeoNames
;
23 import org
.argeo
.security
.ui
.admin
.SecurityAdminPlugin
;
24 import org
.argeo
.security
.ui
.admin
.internal
.UiAdminUtils
;
25 import org
.argeo
.security
.ui
.admin
.internal
.UserAdminConstants
;
26 import org
.argeo
.security
.ui
.admin
.internal
.UserAdminWrapper
;
27 import org
.eclipse
.core
.commands
.AbstractHandler
;
28 import org
.eclipse
.core
.commands
.ExecutionEvent
;
29 import org
.eclipse
.core
.commands
.ExecutionException
;
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
.ModifyEvent
;
35 import org
.eclipse
.swt
.events
.ModifyListener
;
36 import org
.eclipse
.swt
.layout
.GridData
;
37 import org
.eclipse
.swt
.layout
.GridLayout
;
38 import org
.eclipse
.swt
.widgets
.Composite
;
39 import org
.eclipse
.swt
.widgets
.Label
;
40 import org
.eclipse
.swt
.widgets
.Text
;
41 import org
.eclipse
.ui
.handlers
.HandlerUtil
;
42 import org
.osgi
.service
.useradmin
.Group
;
43 import org
.osgi
.service
.useradmin
.Role
;
44 import org
.osgi
.service
.useradmin
.UserAdmin
;
45 import org
.osgi
.service
.useradmin
.UserAdminEvent
;
47 /** Create a new group. */
48 public class NewGroup
extends AbstractHandler
{
49 public final static String ID
= SecurityAdminPlugin
.PLUGIN_ID
+ ".newGroup";
51 /* DEPENDENCY INJECTION */
52 private UserAdminWrapper userAdminWrapper
;
53 private UserAdmin userAdmin
;
55 // TODO implement a dynamic choice of the base dn
56 private String
getDn(String cn
) {
57 return "cn=" + cn
+ ",dc=example,dc=com";
60 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
61 NewGroupWizard newGroupWizard
= new NewGroupWizard();
62 WizardDialog dialog
= new WizardDialog(
63 HandlerUtil
.getActiveShell(event
), newGroupWizard
);
64 dialog
.setTitle("Create a new group");
69 private class NewGroupWizard
extends Wizard
{
72 private MainGroupInfoWizardPage mainGroupInfo
;
75 private Text dNameTxt
, commonNameTxt
, descriptionTxt
;
77 public NewGroupWizard() {
81 public void addPages() {
82 mainGroupInfo
= new MainGroupInfoWizardPage();
83 addPage(mainGroupInfo
);
85 setWindowTitle("Create a new group");
86 // mainGroupInfo.setMessage(message, WizardPage.WARNING);
89 @SuppressWarnings({ "rawtypes", "unchecked" })
91 public boolean performFinish() {
94 String commonName
= commonNameTxt
.getText();
96 userAdminWrapper
.beginTransactionIfNeeded();
97 Group group
= (Group
) userAdmin
.createRole(getDn(commonName
),
99 Dictionary props
= group
.getProperties();
100 String descStr
= descriptionTxt
.getText();
101 if (UiAdminUtils
.notNull(descStr
))
102 props
.put(UserAdminConstants
.KEY_DESC
, descStr
);
103 userAdminWrapper
.notifyListeners(new UserAdminEvent(null,
104 UserAdminEvent
.ROLE_CREATED
, group
));
106 } catch (Exception e
) {
107 ErrorFeedback
.show("Cannot create new group " + commonName
, e
);
112 private class MainGroupInfoWizardPage
extends WizardPage
implements
113 ModifyListener
, ArgeoNames
{
114 private static final long serialVersionUID
= -3150193365151601807L;
116 public MainGroupInfoWizardPage() {
118 setTitle("General information");
119 setMessage("Please provide a common name and a free description");
123 public void createControl(Composite parent
) {
124 Composite bodyCmp
= new Composite(parent
, SWT
.NONE
);
125 bodyCmp
.setLayout(new GridLayout(2, false));
126 dNameTxt
= EclipseUiUtils
.createGridLT(bodyCmp
,
127 "Distinguished name", this);
128 dNameTxt
.setEnabled(false);
129 commonNameTxt
= EclipseUiUtils
.createGridLT(bodyCmp
,
130 "Common name", this);
131 commonNameTxt
.addModifyListener(new ModifyListener() {
132 private static final long serialVersionUID
= -1435351236582736843L;
135 public void modifyText(ModifyEvent event
) {
136 String name
= commonNameTxt
.getText();
137 if (name
.trim().equals("")) {
138 dNameTxt
.setText("");
140 dNameTxt
.setText(getDn(name
));
145 Label descLbl
= new Label(bodyCmp
, SWT
.LEAD
);
146 descLbl
.setText("Description");
147 descLbl
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.TOP
, false,
149 descriptionTxt
= new Text(bodyCmp
, SWT
.LEAD
| SWT
.MULTI
150 | SWT
.WRAP
| SWT
.BORDER
);
151 descriptionTxt
.setLayoutData(EclipseUiUtils
.fillAll());
152 descriptionTxt
.addModifyListener(this);
156 // Initialize buttons
157 setPageComplete(false);
158 getContainer().updateButtons();
162 public void modifyText(ModifyEvent event
) {
163 String message
= checkComplete();
164 if (message
!= null) {
165 setMessage(message
, WizardPage
.ERROR
);
166 setPageComplete(false);
168 setMessage("Complete", WizardPage
.INFORMATION
);
169 setPageComplete(true);
171 getContainer().updateButtons();
174 /** @return error message or null if complete */
175 protected String
checkComplete() {
176 String name
= commonNameTxt
.getText();
178 if (name
.trim().equals(""))
179 return "Common name must not be empty";
180 Role role
= userAdmin
.getRole(getDn(name
));
182 return "Group " + name
+ " already exists";
187 public void setVisible(boolean visible
) {
188 super.setVisible(visible
);
190 commonNameTxt
.setFocus();
195 /* DEPENDENCY INJECTION */
196 public void setUserAdminWrapper(UserAdminWrapper userAdminWrapper
) {
197 this.userAdminWrapper
= userAdminWrapper
;
198 this.userAdmin
= userAdminWrapper
.getUserAdmin();