]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.security.ui.admin/src/org/argeo/security/ui/admin/internal/parts/UserEditor.java
Move most of the code to the internal packages
[lgpl/argeo-commons.git] / org.argeo.security.ui.admin / src / org / argeo / security / ui / admin / internal / parts / UserEditor.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.internal.parts;
17
18 import java.util.ArrayList;
19 import java.util.List;
20
21 import org.argeo.ArgeoException;
22 import org.argeo.security.ui.admin.SecurityAdminPlugin;
23 import org.argeo.security.ui.admin.internal.UserAdminConstants;
24 import org.argeo.security.ui.admin.internal.UserAdminWrapper;
25 import org.eclipse.core.runtime.IProgressMonitor;
26 import org.eclipse.ui.IEditorInput;
27 import org.eclipse.ui.IEditorSite;
28 import org.eclipse.ui.PartInitException;
29 import org.eclipse.ui.forms.editor.FormEditor;
30 import org.osgi.service.useradmin.Authorization;
31 import org.osgi.service.useradmin.Role;
32 import org.osgi.service.useradmin.User;
33 import org.osgi.service.useradmin.UserAdmin;
34 import org.osgi.service.useradmin.UserAdminEvent;
35
36 /** Editor for a user, might be a user or a group. */
37 public class UserEditor extends FormEditor implements UserAdminConstants {
38 private static final long serialVersionUID = 8357851520380820241L;
39
40 public final static String ID = SecurityAdminPlugin.PLUGIN_ID
41 + ".userEditor";
42
43 /* DEPENDENCY INJECTION */
44 private UserAdminWrapper userAdminWrapper;
45 private UserAdmin userAdmin;
46
47 // Context
48 private User user;
49 private String username;
50
51 public void init(IEditorSite site, IEditorInput input)
52 throws PartInitException {
53 super.init(site, input);
54 username = ((UserEditorInput) getEditorInput()).getUsername();
55 user = (User) userAdmin.getRole(username);
56
57 String commonName = getProperty(KEY_CN);
58
59 setPartName(commonName != null ? commonName : "username");
60
61 // TODO: following has been disabled because it causes NPE after a
62 // login/logout on RAP
63 // Image titleIcon = user.getType() == Role.GROUP ?
64 // SecurityAdminImages.ICON_GROUP
65 // : SecurityAdminImages.ICON_USER;
66 // setTitleImage(titleIcon);
67 }
68
69 /**
70 * returns the list of all authorisation for the given user or of the
71 * current displayed user if parameter is null
72 */
73 protected List<User> getFlatGroups(User aUser) {
74 Authorization currAuth;
75 if (aUser == null)
76 currAuth = userAdmin.getAuthorization(this.user);
77 else
78 currAuth = userAdmin.getAuthorization(aUser);
79
80 String[] roles = currAuth.getRoles();
81
82 List<User> groups = new ArrayList<User>();
83 for (String roleStr : roles) {
84 User currRole = (User) userAdmin.getRole(roleStr);
85 if (!groups.contains(currRole))
86 groups.add(currRole);
87 }
88 return groups;
89 }
90
91 /** Exposes the user (or group) that is displayed by the current editor */
92 protected User getDisplayedUser() {
93 return user;
94 }
95
96 void updateEditorTitle(String title) {
97 setPartName(title);
98 }
99
100 protected void addPages() {
101 try {
102 if (user.getType() == Role.GROUP)
103 addPage(new GroupMainPage(this, userAdminWrapper));
104 else
105 addPage(new UserMainPage(this, userAdminWrapper));
106 } catch (Exception e) {
107 throw new ArgeoException("Cannot add pages", e);
108 }
109 }
110
111 protected String getProperty(String key) {
112 Object obj = user.getProperties().get(key);
113 if (obj != null)
114 return (String) obj;
115 else
116 return "";
117 }
118
119 /**
120 * Updates the property in the working copy. The transaction must be
121 * explicitly committed to persist the update.
122 */
123 @SuppressWarnings("unchecked")
124 protected void setProperty(String key, String value) {
125 user.getProperties().put(key, value);
126 }
127
128 @Override
129 public void doSave(IProgressMonitor monitor) {
130 userAdminWrapper.beginTransactionIfNeeded();
131 commitPages(true);
132 firePropertyChange(PROP_DIRTY);
133 userAdminWrapper.notifyListeners(new UserAdminEvent(null,
134 UserAdminEvent.ROLE_REMOVED, user));
135 }
136
137 @Override
138 public void doSaveAs() {
139 }
140
141 @Override
142 public boolean isSaveAsAllowed() {
143 return false;
144 }
145
146 public void refresh() {
147
148 }
149
150 @Override
151 public void dispose() {
152 super.dispose();
153 }
154
155 /* DEPENDENCY INJECTION */
156 public void setUserAdminWrapper(UserAdminWrapper userAdminWrapper) {
157 this.userAdminWrapper = userAdminWrapper;
158 this.userAdmin = userAdminWrapper.getUserAdmin();
159 }
160 }