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
.UserTransaction
;
22 import org
.argeo
.eclipse
.ui
.EclipseUiUtils
;
23 import org
.argeo
.eclipse
.ui
.dialogs
.ErrorFeedback
;
24 import org
.argeo
.jcr
.ArgeoNames
;
25 import org
.argeo
.security
.ui
.admin
.SecurityAdminPlugin
;
26 import org
.argeo
.security
.ui
.admin
.internal
.UiAdminUtils
;
27 import org
.argeo
.security
.ui
.admin
.internal
.UserAdminConstants
;
28 import org
.argeo
.security
.ui
.admin
.views
.GroupsView
;
29 import org
.eclipse
.core
.commands
.AbstractHandler
;
30 import org
.eclipse
.core
.commands
.ExecutionEvent
;
31 import org
.eclipse
.core
.commands
.ExecutionException
;
32 import org
.eclipse
.jface
.window
.Window
;
33 import org
.eclipse
.jface
.wizard
.Wizard
;
34 import org
.eclipse
.jface
.wizard
.WizardDialog
;
35 import org
.eclipse
.jface
.wizard
.WizardPage
;
36 import org
.eclipse
.swt
.SWT
;
37 import org
.eclipse
.swt
.events
.ModifyEvent
;
38 import org
.eclipse
.swt
.events
.ModifyListener
;
39 import org
.eclipse
.swt
.layout
.GridData
;
40 import org
.eclipse
.swt
.layout
.GridLayout
;
41 import org
.eclipse
.swt
.widgets
.Composite
;
42 import org
.eclipse
.swt
.widgets
.Label
;
43 import org
.eclipse
.swt
.widgets
.Text
;
44 import org
.eclipse
.ui
.IWorkbenchPage
;
45 import org
.eclipse
.ui
.IWorkbenchPart
;
46 import org
.eclipse
.ui
.IWorkbenchWindow
;
47 import org
.eclipse
.ui
.handlers
.HandlerUtil
;
48 import org
.osgi
.service
.useradmin
.Group
;
49 import org
.osgi
.service
.useradmin
.Role
;
50 import org
.osgi
.service
.useradmin
.UserAdmin
;
52 /** Create a new group. */
53 public class NewGroup
extends AbstractHandler
{
54 public final static String ID
= SecurityAdminPlugin
.PLUGIN_ID
+ ".newGroup";
56 private UserAdmin userAdmin
;
57 private UserTransaction userTransaction
;
59 // TODO implement a dynamic choice of the base dn
60 private String
getDn(String cn
) {
61 return "cn=" + cn
+ ",dc=example,dc=com";
64 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
65 NewGroupWizard newGroupWizard
= new NewGroupWizard();
66 WizardDialog dialog
= new WizardDialog(
67 HandlerUtil
.getActiveShell(event
), newGroupWizard
);
68 dialog
.setTitle("Create a new group");
70 // Force refresh until the listener are implemented
71 if (Window
.OK
== dialog
.open())
76 private void forceRefresh(ExecutionEvent event
) {
77 IWorkbenchWindow iww
= HandlerUtil
.getActiveWorkbenchWindow(event
);
80 IWorkbenchPage activePage
= iww
.getActivePage();
81 IWorkbenchPart part
= activePage
.getActivePart();
82 if (part
instanceof GroupsView
)
83 ((GroupsView
) part
).refresh();
86 private class NewGroupWizard
extends Wizard
{
89 private MainGroupInfoWizardPage mainGroupInfo
;
92 private Text dNameTxt
, commonNameTxt
, descriptionTxt
;
94 public NewGroupWizard() {
98 public void addPages() {
99 mainGroupInfo
= new MainGroupInfoWizardPage();
100 addPage(mainGroupInfo
);
102 setWindowTitle("Create a new group");
103 // mainGroupInfo.setMessage(message, WizardPage.WARNING);
106 @SuppressWarnings({ "rawtypes", "unchecked" })
108 public boolean performFinish() {
111 String commonName
= commonNameTxt
.getText();
113 UiAdminUtils
.beginTransactionIfNeeded(userTransaction
);
114 Group user
= (Group
) userAdmin
.createRole(getDn(commonName
),
116 Dictionary props
= user
.getProperties();
117 String descStr
= descriptionTxt
.getText();
118 if (UiAdminUtils
.notNull(descStr
))
119 props
.put(UserAdminConstants
.KEY_DESC
, descStr
);
121 } catch (Exception e
) {
122 ErrorFeedback
.show("Cannot create new group " + commonName
, e
);
127 private class MainGroupInfoWizardPage
extends WizardPage
implements
128 ModifyListener
, ArgeoNames
{
129 private static final long serialVersionUID
= -3150193365151601807L;
131 public MainGroupInfoWizardPage() {
133 setTitle("General information");
134 setMessage("Please provide a common name and a free description");
138 public void createControl(Composite parent
) {
139 Composite bodyCmp
= new Composite(parent
, SWT
.NONE
);
140 bodyCmp
.setLayout(new GridLayout(2, false));
141 dNameTxt
= EclipseUiUtils
.createGridLT(bodyCmp
,
142 "Distinguished name", this);
143 dNameTxt
.setEnabled(false);
144 commonNameTxt
= EclipseUiUtils
.createGridLT(bodyCmp
,
145 "Common name", this);
146 commonNameTxt
.addModifyListener(new ModifyListener() {
147 private static final long serialVersionUID
= -1435351236582736843L;
150 public void modifyText(ModifyEvent event
) {
151 String name
= commonNameTxt
.getText();
152 if (name
.trim().equals("")) {
153 dNameTxt
.setText("");
155 dNameTxt
.setText(getDn(name
));
160 Label descLbl
= new Label(bodyCmp
, SWT
.LEAD
);
161 descLbl
.setText("Description");
162 descLbl
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.TOP
, false,
164 descriptionTxt
= new Text(bodyCmp
, SWT
.LEAD
| SWT
.MULTI
165 | SWT
.WRAP
| SWT
.BORDER
);
166 descriptionTxt
.setLayoutData(EclipseUiUtils
.fillAll());
167 descriptionTxt
.addModifyListener(this);
171 // Initialize buttons
172 setPageComplete(false);
173 getContainer().updateButtons();
177 public void modifyText(ModifyEvent event
) {
178 String message
= checkComplete();
179 if (message
!= null) {
180 setMessage(message
, WizardPage
.ERROR
);
181 setPageComplete(false);
183 setMessage("Complete", WizardPage
.INFORMATION
);
184 setPageComplete(true);
186 getContainer().updateButtons();
189 /** @return error message or null if complete */
190 protected String
checkComplete() {
191 String name
= commonNameTxt
.getText();
193 if (name
.trim().equals(""))
194 return "Common name must not be empty";
195 Role role
= userAdmin
.getRole(getDn(name
));
197 return "Group " + name
+ " already exists";
202 public void setVisible(boolean visible
) {
203 super.setVisible(visible
);
205 commonNameTxt
.setFocus();
210 /* DEPENDENCY INJECTION */
211 public void setUserAdmin(UserAdmin userAdmin
) {
212 this.userAdmin
= userAdmin
;
215 public void setUserTransaction(UserTransaction userTransaction
) {
216 this.userTransaction
= userTransaction
;