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 org
.argeo
.ArgeoException
;
19 import org
.argeo
.security
.UserAdminService
;
20 import org
.argeo
.security
.ui
.admin
.editors
.ArgeoUserEditor
;
21 import org
.argeo
.security
.ui
.admin
.views
.RolesView
;
22 import org
.eclipse
.core
.commands
.AbstractHandler
;
23 import org
.eclipse
.core
.commands
.ExecutionEvent
;
24 import org
.eclipse
.core
.commands
.ExecutionException
;
25 import org
.eclipse
.ui
.IEditorReference
;
26 import org
.eclipse
.ui
.IWorkbenchPage
;
27 import org
.eclipse
.ui
.handlers
.HandlerUtil
;
29 /** Add a new role. */
30 public class AddRole
extends AbstractHandler
{
31 public final static String COMMAND_ID
= "org.argeo.security.ui.admin.addRole";
32 private UserAdminService userAdminService
;
33 private String rolePrefix
= "ROLE_";
35 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
36 RolesView rolesView
= (RolesView
) HandlerUtil
37 .getActiveWorkbenchWindow(event
).getActivePage()
38 .findView(RolesView
.ID
);
39 String role
= rolesView
.getNewRole();
40 if (role
.trim().equals(""))
42 if (role
.equals(rolesView
.getAddNewRoleText()))
44 role
= role
.trim().toUpperCase();
45 if (!role
.startsWith(rolePrefix
))
46 role
= rolePrefix
+ role
;
47 if (userAdminService
.listEditableRoles().contains(role
))
48 throw new ArgeoException("Role " + role
+ " already exists");
49 userAdminService
.newRole(role
);
53 IEditorReference
[] refs
= HandlerUtil
.getActiveWorkbenchWindow(event
)
55 .findEditors(null, ArgeoUserEditor
.ID
, IWorkbenchPage
.MATCH_ID
);
56 for (IEditorReference ref
: refs
) {
57 ArgeoUserEditor userEditor
= (ArgeoUserEditor
) ref
.getEditor(false);
58 if (userEditor
!= null) {
65 public void setUserAdminService(UserAdminService userAdminService
) {
66 this.userAdminService
= userAdminService
;