]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.security.ui.admin/src/org/argeo/security/ui/admin/editors/UserEditor.java
First draft of User Creation wizard
[lgpl/argeo-commons.git] / org.argeo.security.ui.admin / src / org / argeo / security / ui / admin / editors / 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.editors;
17
18 import java.util.ArrayList;
19 import java.util.List;
20
21 import org.argeo.ArgeoException;
22 import org.argeo.security.ui.admin.SecurityAdminImages;
23 import org.argeo.security.ui.admin.SecurityAdminPlugin;
24 import org.argeo.security.ui.admin.internal.UserAdminConstants;
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
35 /** Editor for a user, might be a user or a group. */
36 public class UserEditor extends FormEditor implements UserAdminConstants {
37 private static final long serialVersionUID = 8357851520380820241L;
38
39 public final static String ID = SecurityAdminPlugin.PLUGIN_ID
40 + ".userEditor";
41
42 /* DEPENDENCY INJECTION */
43 private UserAdmin userAdmin;
44
45 // Context
46 private User user;
47 private String username;
48
49 public void init(IEditorSite site, IEditorInput input)
50 throws PartInitException {
51 super.init(site, input);
52 username = ((UserEditorInput) getEditorInput()).getUsername();
53 user = (User) userAdmin.getRole(username);
54
55 String commonName = getProperty(KEY_CN);
56 // this.setPartProperty("name", commonName != null ? commonName
57 // : "username");
58
59 // if (user.getType() == Role.GROUP) {
60 // this.setPartProperty("icon", "icons/users.gif");
61 // firePartPropertyChanged("icon", "icons/user.gif", "icons/users.gif");
62 // }
63 setPartName(commonName != null ? commonName : "username");
64 setTitleImage(user.getType() == Role.GROUP ? SecurityAdminImages.ICON_GROUP
65 : SecurityAdminImages.ICON_USER);
66 }
67
68 protected List<User> getFlatGroups() {
69 Authorization currAuth = userAdmin.getAuthorization(user);
70 String[] roles = currAuth.getRoles();
71
72 List<User> groups = new ArrayList<User>();
73 for (String roleStr : roles) {
74 User currRole = (User) userAdmin.getRole(roleStr);
75 if (!groups.contains(currRole))
76 groups.add(currRole);
77 }
78 return groups;
79 }
80
81 /** Exposes the user (or group) that is displayed by the current editor */
82 protected User getDisplayedUser() {
83 return user;
84 }
85
86 void updateEditorTitle(String title) {
87 setPartName(title);
88 }
89
90 protected void addPages() {
91 try {
92
93 if (user.getType() == Role.GROUP)
94 addPage(new GroupMainPage(this, userAdmin));
95 else
96 addPage(new UserMainPage(this, userAdmin));
97
98 } catch (Exception e) {
99 throw new ArgeoException("Cannot add pages", e);
100 }
101 }
102
103 protected String getProperty(String key) {
104 Object obj = user.getProperties().get(key);
105 if (obj != null)
106 return (String) obj;
107 else
108 return "";
109 }
110
111 /** The property is directly updated!!! */
112 @SuppressWarnings("unchecked")
113 protected void setProperty(String key, String value) {
114 user.getProperties().put(key, value);
115 }
116
117 @Override
118 public void doSave(IProgressMonitor monitor) {
119 commitPages(true);
120 firePropertyChange(PROP_DIRTY);
121 }
122
123 @Override
124 public void doSaveAs() {
125 }
126
127 @Override
128 public boolean isSaveAsAllowed() {
129 return false;
130 }
131
132 public void refresh() {
133
134 }
135
136 @Override
137 public void dispose() {
138 super.dispose();
139 }
140
141 /* DEPENDENCY INJECTION */
142 public void setUserAdmin(UserAdmin userAdmin) {
143 this.userAdmin = userAdmin;
144 }
145 }