]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.security.ui.admin/src/org/argeo/security/ui/admin/commands/AddRole.java
New project conventions (builds)
[lgpl/argeo-commons.git] / org.argeo.security.ui.admin / src / org / argeo / security / ui / admin / commands / AddRole.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16 package org.argeo.security.ui.admin.commands;
17
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;
28
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_";
34
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(""))
41 return null;
42 if (role.equals(rolesView.getAddNewRoleText()))
43 return null;
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);
50 rolesView.refresh();
51
52 // refresh editors
53 IEditorReference[] refs = HandlerUtil.getActiveWorkbenchWindow(event)
54 .getActivePage()
55 .findEditors(null, ArgeoUserEditor.ID, IWorkbenchPage.MATCH_ID);
56 for (IEditorReference ref : refs) {
57 ArgeoUserEditor userEditor = (ArgeoUserEditor) ref.getEditor(false);
58 if (userEditor != null) {
59 userEditor.refresh();
60 }
61 }
62 return null;
63 }
64
65 public void setUserAdminService(UserAdminService userAdminService) {
66 this.userAdminService = userAdminService;
67 }
68
69 }