]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms.e4/src/org/argeo/cms/e4/users/AbstractRoleEditor.java
Introduce E4 privileged job
[lgpl/argeo-commons.git] / org.argeo.cms.e4 / src / org / argeo / cms / e4 / users / AbstractRoleEditor.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.cms.e4.users;
17
18 import java.util.ArrayList;
19 import java.util.List;
20
21 import javax.annotation.PostConstruct;
22 import javax.annotation.PreDestroy;
23 import javax.inject.Inject;
24
25 import org.argeo.cms.ui.eclipse.forms.AbstractFormPart;
26 import org.argeo.cms.ui.eclipse.forms.IManagedForm;
27 import org.argeo.cms.ui.eclipse.forms.ManagedForm;
28 import org.argeo.cms.util.UserAdminUtils;
29 import org.argeo.eclipse.ui.EclipseUiUtils;
30 import org.argeo.naming.LdapAttrs;
31 import org.eclipse.core.runtime.IProgressMonitor;
32 import org.eclipse.e4.ui.di.Persist;
33 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
34 import org.eclipse.swt.SWT;
35 import org.eclipse.swt.custom.ScrolledComposite;
36 import org.eclipse.swt.events.ModifyEvent;
37 import org.eclipse.swt.events.ModifyListener;
38 import org.eclipse.swt.layout.GridData;
39 import org.eclipse.swt.widgets.Composite;
40 import org.eclipse.swt.widgets.Control;
41 import org.eclipse.swt.widgets.Display;
42 import org.eclipse.swt.widgets.Label;
43 import org.eclipse.swt.widgets.Text;
44 import org.osgi.service.useradmin.Authorization;
45 import org.osgi.service.useradmin.Role;
46 import org.osgi.service.useradmin.User;
47 import org.osgi.service.useradmin.UserAdmin;
48 import org.osgi.service.useradmin.UserAdminEvent;
49
50 /** Editor for a user, might be a user or a group. */
51 public abstract class AbstractRoleEditor {
52
53 // public final static String USER_EDITOR_ID = WorkbenchUiPlugin.PLUGIN_ID +
54 // ".userEditor";
55 // public final static String GROUP_EDITOR_ID = WorkbenchUiPlugin.PLUGIN_ID +
56 // ".groupEditor";
57
58 /* DEPENDENCY INJECTION */
59 @Inject
60 protected UserAdminWrapper userAdminWrapper;
61
62 @Inject
63 private MPart mPart;
64
65 // @Inject
66 // Composite parent;
67
68 private UserAdmin userAdmin;
69
70 // Context
71 private User user;
72 private String username;
73
74 private NameChangeListener listener;
75
76 private ManagedForm managedForm;
77
78 // public void init(IEditorSite site, IEditorInput input) throws
79 // PartInitException {
80 @PostConstruct
81 public void init(Composite parent) {
82 this.userAdmin = userAdminWrapper.getUserAdmin();
83 username = mPart.getPersistedState().get(LdapAttrs.uid.name());
84 user = (User) userAdmin.getRole(username);
85
86 listener = new NameChangeListener(Display.getCurrent());
87 userAdminWrapper.addListener(listener);
88 updateEditorTitle(null);
89
90 managedForm = new ManagedForm(parent) {
91
92 @Override
93 public void staleStateChanged() {
94 refresh();
95 }
96 };
97 ScrolledComposite scrolled = managedForm.getForm();
98 Composite body = new Composite(scrolled, SWT.NONE);
99 scrolled.setContent(body);
100 createUi(body);
101 managedForm.refresh();
102 }
103
104 abstract void createUi(Composite parent);
105
106 /**
107 * returns the list of all authorizations for the given user or of the current
108 * displayed user if parameter is null
109 */
110 protected List<User> getFlatGroups(User aUser) {
111 Authorization currAuth;
112 if (aUser == null)
113 currAuth = userAdmin.getAuthorization(this.user);
114 else
115 currAuth = userAdmin.getAuthorization(aUser);
116
117 String[] roles = currAuth.getRoles();
118
119 List<User> groups = new ArrayList<User>();
120 for (String roleStr : roles) {
121 User currRole = (User) userAdmin.getRole(roleStr);
122 if (currRole != null && !groups.contains(currRole))
123 groups.add(currRole);
124 }
125 return groups;
126 }
127
128 protected IManagedForm getManagedForm() {
129 return managedForm;
130 }
131
132 /** Exposes the user (or group) that is displayed by the current editor */
133 protected User getDisplayedUser() {
134 return user;
135 }
136
137 private void setDisplayedUser(User user) {
138 this.user = user;
139 }
140
141 void updateEditorTitle(String title) {
142 if (title == null) {
143 String commonName = UserAdminUtils.getProperty(user, LdapAttrs.cn.name());
144 title = "".equals(commonName) ? user.getName() : commonName;
145 }
146 setPartName(title);
147 }
148
149 protected void setPartName(String name) {
150 mPart.setLabel(name);
151 }
152
153 // protected void addPages() {
154 // try {
155 // if (user.getType() == Role.GROUP)
156 // addPage(new GroupMainPage(this, userAdminWrapper, repository, nodeInstance));
157 // else
158 // addPage(new UserMainPage(this, userAdminWrapper));
159 // } catch (Exception e) {
160 // throw new CmsException("Cannot add pages", e);
161 // }
162 // }
163
164 @Persist
165 public void doSave(IProgressMonitor monitor) {
166 userAdminWrapper.beginTransactionIfNeeded();
167 commitPages(true);
168 userAdminWrapper.commitOrNotifyTransactionStateChange();
169 // firePropertyChange(PROP_DIRTY);
170 userAdminWrapper.notifyListeners(new UserAdminEvent(null, UserAdminEvent.ROLE_REMOVED, user));
171 }
172
173 protected void commitPages(boolean b) {
174 managedForm.commit(b);
175 }
176
177 @PreDestroy
178 public void dispose() {
179 userAdminWrapper.removeListener(listener);
180 managedForm.dispose();
181 }
182
183 // CONTROLERS FOR THIS EDITOR AND ITS PAGES
184
185 class NameChangeListener extends UiUserAdminListener {
186 public NameChangeListener(Display display) {
187 super(display);
188 }
189
190 @Override
191 public void roleChangedToUiThread(UserAdminEvent event) {
192 Role changedRole = event.getRole();
193 if (changedRole == null || changedRole.equals(user)) {
194 updateEditorTitle(null);
195 User reloadedUser = (User) userAdminWrapper.getUserAdmin().getRole(user.getName());
196 setDisplayedUser(reloadedUser);
197 }
198 }
199 }
200
201 class MainInfoListener extends UiUserAdminListener {
202 private final AbstractFormPart part;
203
204 public MainInfoListener(Display display, AbstractFormPart part) {
205 super(display);
206 this.part = part;
207 }
208
209 @Override
210 public void roleChangedToUiThread(UserAdminEvent event) {
211 // Rollback
212 if (event.getRole() == null)
213 part.markStale();
214 }
215 }
216
217 class GroupChangeListener extends UiUserAdminListener {
218 private final AbstractFormPart part;
219
220 public GroupChangeListener(Display display, AbstractFormPart part) {
221 super(display);
222 this.part = part;
223 }
224
225 @Override
226 public void roleChangedToUiThread(UserAdminEvent event) {
227 // always mark as stale
228 part.markStale();
229 }
230 }
231
232 /** Registers a listener that will notify this part */
233 class FormPartML implements ModifyListener {
234 private static final long serialVersionUID = 6299808129505381333L;
235 private AbstractFormPart formPart;
236
237 public FormPartML(AbstractFormPart generalPart) {
238 this.formPart = generalPart;
239 }
240
241 public void modifyText(ModifyEvent e) {
242 // Discard event when the control does not have the focus, typically
243 // to avoid all editors being marked as dirty during a Rollback
244 if (((Control) e.widget).isFocusControl())
245 formPart.markDirty();
246 }
247 }
248
249 /* DEPENDENCY INJECTION */
250 public void setUserAdminWrapper(UserAdminWrapper userAdminWrapper) {
251 this.userAdminWrapper = userAdminWrapper;
252 }
253
254 /** Creates label and multiline text. */
255 Text createLMT(Composite parent, String label, String value) {
256 Label lbl = new Label(parent, SWT.NONE);
257 lbl.setText(label);
258 lbl.setLayoutData(new GridData(SWT.LEAD, SWT.CENTER, false, false));
259 Text text = new Text(parent, SWT.NONE);
260 text.setText(value);
261 text.setLayoutData(new GridData(SWT.LEAD, SWT.FILL, true, true));
262 return text;
263 }
264
265 /** Creates label and password. */
266 Text createLP(Composite parent, String label, String value) {
267 Label lbl = new Label(parent, SWT.NONE);
268 lbl.setText(label);
269 lbl.setLayoutData(new GridData(SWT.LEAD, SWT.CENTER, false, false));
270 Text text = new Text(parent, SWT.PASSWORD | SWT.BORDER);
271 text.setText(value);
272 text.setLayoutData(new GridData(SWT.LEAD, SWT.FILL, true, false));
273 return text;
274 }
275
276 /** Creates label and text. */
277 Text createLT(Composite parent, String label, String value) {
278 Label lbl = new Label(parent, SWT.NONE);
279 lbl.setText(label);
280 lbl.setLayoutData(new GridData(SWT.LEAD, SWT.CENTER, false, false));
281 lbl.setFont(EclipseUiUtils.getBoldFont(parent));
282 Text text = new Text(parent, SWT.BORDER);
283 text.setText(value);
284 text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
285 // CmsUtils.style(text, CmsWorkbenchStyles.WORKBENCH_FORM_TEXT);
286 return text;
287 }
288
289 Text createReadOnlyLT(Composite parent, String label, String value) {
290 Label lbl = new Label(parent, SWT.NONE);
291 lbl.setText(label);
292 lbl.setLayoutData(new GridData(SWT.LEAD, SWT.CENTER, false, false));
293 lbl.setFont(EclipseUiUtils.getBoldFont(parent));
294 Text text = new Text(parent, SWT.NONE);
295 text.setText(value);
296 text.setLayoutData(new GridData(SWT.LEAD, SWT.FILL, true, false));
297 text.setEditable(false);
298 // CmsUtils.style(text, CmsWorkbenchStyles.WORKBENCH_FORM_TEXT);
299 return text;
300 }
301
302 }