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
.commands
;
18 import java
.util
.Dictionary
;
20 import javax
.transaction
.Status
;
21 import javax
.transaction
.UserTransaction
;
23 import org
.argeo
.ArgeoException
;
24 import org
.argeo
.eclipse
.ui
.EclipseUiUtils
;
25 import org
.argeo
.eclipse
.ui
.dialogs
.ErrorFeedback
;
26 import org
.argeo
.jcr
.ArgeoNames
;
27 import org
.argeo
.security
.ui
.admin
.SecurityAdminPlugin
;
28 import org
.argeo
.security
.ui
.admin
.internal
.UiAdminUtils
;
29 import org
.argeo
.security
.ui
.admin
.internal
.UserAdminConstants
;
30 import org
.argeo
.security
.ui
.admin
.views
.GroupsView
;
31 import org
.eclipse
.core
.commands
.AbstractHandler
;
32 import org
.eclipse
.core
.commands
.ExecutionEvent
;
33 import org
.eclipse
.core
.commands
.ExecutionException
;
34 import org
.eclipse
.jface
.window
.Window
;
35 import org
.eclipse
.jface
.wizard
.Wizard
;
36 import org
.eclipse
.jface
.wizard
.WizardDialog
;
37 import org
.eclipse
.jface
.wizard
.WizardPage
;
38 import org
.eclipse
.swt
.SWT
;
39 import org
.eclipse
.swt
.events
.ModifyEvent
;
40 import org
.eclipse
.swt
.events
.ModifyListener
;
41 import org
.eclipse
.swt
.layout
.GridData
;
42 import org
.eclipse
.swt
.layout
.GridLayout
;
43 import org
.eclipse
.swt
.widgets
.Composite
;
44 import org
.eclipse
.swt
.widgets
.Label
;
45 import org
.eclipse
.swt
.widgets
.Text
;
46 import org
.eclipse
.ui
.IWorkbenchPage
;
47 import org
.eclipse
.ui
.IWorkbenchPart
;
48 import org
.eclipse
.ui
.IWorkbenchWindow
;
49 import org
.eclipse
.ui
.handlers
.HandlerUtil
;
50 import org
.osgi
.service
.useradmin
.Group
;
51 import org
.osgi
.service
.useradmin
.Role
;
52 import org
.osgi
.service
.useradmin
.UserAdmin
;
54 /** Create a new group. */
55 public class NewGroup
extends AbstractHandler
{
56 public final static String ID
= SecurityAdminPlugin
.PLUGIN_ID
+ ".newGroup";
58 private UserAdmin userAdmin
;
59 private UserTransaction userTransaction
;
61 // TODO implement a dynamic choice of the base dn
62 private String
getDn(String cn
) {
63 return "cn=" + cn
+ ",dc=example,dc=com";
66 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
67 NewGroupWizard newGroupWizard
= new NewGroupWizard();
68 WizardDialog dialog
= new WizardDialog(
69 HandlerUtil
.getActiveShell(event
), newGroupWizard
);
70 dialog
.setTitle("Create a new group");
72 // Force refresh until the listener are implemented
73 if (Window
.OK
== dialog
.open())
78 private void forceRefresh(ExecutionEvent event
) {
79 IWorkbenchWindow iww
= HandlerUtil
.getActiveWorkbenchWindow(event
);
82 IWorkbenchPage activePage
= iww
.getActivePage();
83 IWorkbenchPart part
= activePage
.getActivePart();
84 if (part
instanceof GroupsView
)
85 ((GroupsView
) part
).refresh();
88 private class NewGroupWizard
extends Wizard
{
91 private MainGroupInfoWizardPage mainGroupInfo
;
94 private Text dNameTxt
, commonNameTxt
, descriptionTxt
;
96 public NewGroupWizard() {
100 public void addPages() {
101 mainGroupInfo
= new MainGroupInfoWizardPage();
102 addPage(mainGroupInfo
);
104 setWindowTitle("Create a new group");
105 // mainGroupInfo.setMessage(message, WizardPage.WARNING);
108 @SuppressWarnings({ "rawtypes", "unchecked" })
110 public boolean performFinish() {
113 String commonName
= commonNameTxt
.getText();
115 // Begin transaction if needed
117 if (userTransaction
.getStatus() == Status
.STATUS_NO_TRANSACTION
)
118 userTransaction
.begin();
119 } catch (Exception e
) {
120 throw new ArgeoException("Unable to start "
121 + "transaction to create user " + commonName
, e
);
125 Group user
= (Group
) userAdmin
.createRole(getDn(commonName
),
127 Dictionary props
= user
.getProperties();
128 String descStr
= descriptionTxt
.getText();
129 if (UiAdminUtils
.notNull(descStr
))
130 props
.put(UserAdminConstants
.KEY_DESC
, descStr
);
132 } catch (Exception e
) {
133 ErrorFeedback
.show("Cannot create new group " + commonName
, e
);
138 private class MainGroupInfoWizardPage
extends WizardPage
implements
139 ModifyListener
, ArgeoNames
{
140 private static final long serialVersionUID
= -3150193365151601807L;
142 public MainGroupInfoWizardPage() {
144 setTitle("General information");
145 setMessage("Please provide a common name and a free description");
149 public void createControl(Composite parent
) {
150 Composite bodyCmp
= new Composite(parent
, SWT
.NONE
);
151 bodyCmp
.setLayout(new GridLayout(2, false));
152 dNameTxt
= EclipseUiUtils
.createGridLT(bodyCmp
,
153 "Distinguished name", this);
154 dNameTxt
.setEnabled(false);
155 commonNameTxt
= EclipseUiUtils
.createGridLT(bodyCmp
,
156 "Common name", this);
157 commonNameTxt
.addModifyListener(new ModifyListener() {
158 private static final long serialVersionUID
= -1435351236582736843L;
161 public void modifyText(ModifyEvent event
) {
162 String name
= commonNameTxt
.getText();
163 if (name
.trim().equals("")) {
164 dNameTxt
.setText("");
166 dNameTxt
.setText(getDn(name
));
171 Label descLbl
= new Label(bodyCmp
, SWT
.LEAD
);
172 descLbl
.setText("Description");
173 descLbl
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.TOP
, false,
175 descriptionTxt
= new Text(bodyCmp
, SWT
.LEAD
| SWT
.MULTI
176 | SWT
.WRAP
| SWT
.BORDER
);
177 descriptionTxt
.setLayoutData(EclipseUiUtils
.fillAll());
178 descriptionTxt
.addModifyListener(this);
182 // Initialize buttons
183 setPageComplete(false);
184 getContainer().updateButtons();
188 public void modifyText(ModifyEvent event
) {
189 String message
= checkComplete();
190 if (message
!= null) {
191 setMessage(message
, WizardPage
.ERROR
);
192 setPageComplete(false);
194 setMessage("Complete", WizardPage
.INFORMATION
);
195 setPageComplete(true);
197 getContainer().updateButtons();
200 /** @return error message or null if complete */
201 protected String
checkComplete() {
202 String name
= commonNameTxt
.getText();
204 if (name
.trim().equals(""))
205 return "Common name must not be empty";
206 Role role
= userAdmin
.getRole(getDn(name
));
208 return "Group " + name
+ " already exists";
213 public void setVisible(boolean visible
) {
214 super.setVisible(visible
);
216 commonNameTxt
.setFocus();
221 /* DEPENDENCY INJECTION */
222 public void setUserAdmin(UserAdmin userAdmin
) {
223 this.userAdmin
= userAdmin
;
226 public void setUserTransaction(UserTransaction userTransaction
) {
227 this.userTransaction
= userTransaction
;