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
.cms
.e4
.users
;
18 import java
.util
.ArrayList
;
19 import java
.util
.List
;
21 import javax
.annotation
.PostConstruct
;
22 import javax
.annotation
.PreDestroy
;
23 import javax
.inject
.Inject
;
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
;
50 /** Editor for a user, might be a user or a group. */
51 public abstract class AbstractRoleEditor
{
53 // public final static String USER_EDITOR_ID = WorkbenchUiPlugin.PLUGIN_ID +
55 // public final static String GROUP_EDITOR_ID = WorkbenchUiPlugin.PLUGIN_ID +
58 /* DEPENDENCY INJECTION */
60 protected UserAdminWrapper userAdminWrapper
;
68 private UserAdmin userAdmin
;
72 private String username
;
74 private NameChangeListener listener
;
76 private ManagedForm managedForm
;
78 // public void init(IEditorSite site, IEditorInput input) throws
79 // PartInitException {
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
);
86 listener
= new NameChangeListener(Display
.getCurrent());
87 userAdminWrapper
.addListener(listener
);
88 updateEditorTitle(null);
90 managedForm
= new ManagedForm(parent
) {
93 public void staleStateChanged() {
97 ScrolledComposite scrolled
= managedForm
.getForm();
98 Composite body
= new Composite(scrolled
, SWT
.NONE
);
99 scrolled
.setContent(body
);
101 managedForm
.refresh();
104 abstract void createUi(Composite parent
);
107 * returns the list of all authorizations for the given user or of the current
108 * displayed user if parameter is null
110 protected List
<User
> getFlatGroups(User aUser
) {
111 Authorization currAuth
;
113 currAuth
= userAdmin
.getAuthorization(this.user
);
115 currAuth
= userAdmin
.getAuthorization(aUser
);
117 String
[] roles
= currAuth
.getRoles();
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
);
128 protected IManagedForm
getManagedForm() {
132 /** Exposes the user (or group) that is displayed by the current editor */
133 protected User
getDisplayedUser() {
137 private void setDisplayedUser(User user
) {
141 void updateEditorTitle(String title
) {
143 String commonName
= UserAdminUtils
.getProperty(user
, LdapAttrs
.cn
.name());
144 title
= "".equals(commonName
) ? user
.getName() : commonName
;
149 protected void setPartName(String name
) {
150 mPart
.setLabel(name
);
153 // protected void addPages() {
155 // if (user.getType() == Role.GROUP)
156 // addPage(new GroupMainPage(this, userAdminWrapper, repository, nodeInstance));
158 // addPage(new UserMainPage(this, userAdminWrapper));
159 // } catch (Exception e) {
160 // throw new CmsException("Cannot add pages", e);
165 public void doSave(IProgressMonitor monitor
) {
166 userAdminWrapper
.beginTransactionIfNeeded();
168 userAdminWrapper
.commitOrNotifyTransactionStateChange();
169 // firePropertyChange(PROP_DIRTY);
170 userAdminWrapper
.notifyListeners(new UserAdminEvent(null, UserAdminEvent
.ROLE_REMOVED
, user
));
173 protected void commitPages(boolean b
) {
174 managedForm
.commit(b
);
178 public void dispose() {
179 userAdminWrapper
.removeListener(listener
);
180 managedForm
.dispose();
183 // CONTROLERS FOR THIS EDITOR AND ITS PAGES
185 class NameChangeListener
extends UiUserAdminListener
{
186 public NameChangeListener(Display display
) {
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
);
201 class MainInfoListener
extends UiUserAdminListener
{
202 private final AbstractFormPart part
;
204 public MainInfoListener(Display display
, AbstractFormPart part
) {
210 public void roleChangedToUiThread(UserAdminEvent event
) {
212 if (event
.getRole() == null)
217 class GroupChangeListener
extends UiUserAdminListener
{
218 private final AbstractFormPart part
;
220 public GroupChangeListener(Display display
, AbstractFormPart part
) {
226 public void roleChangedToUiThread(UserAdminEvent event
) {
227 // always mark as stale
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
;
237 public FormPartML(AbstractFormPart generalPart
) {
238 this.formPart
= generalPart
;
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();
249 /* DEPENDENCY INJECTION */
250 public void setUserAdminWrapper(UserAdminWrapper userAdminWrapper
) {
251 this.userAdminWrapper
= userAdminWrapper
;
254 /** Creates label and multiline text. */
255 Text
createLMT(Composite parent
, String label
, String value
) {
256 Label lbl
= new Label(parent
, SWT
.NONE
);
258 lbl
.setLayoutData(new GridData(SWT
.LEAD
, SWT
.CENTER
, false, false));
259 Text text
= new Text(parent
, SWT
.NONE
);
261 text
.setLayoutData(new GridData(SWT
.LEAD
, SWT
.FILL
, true, true));
265 /** Creates label and password. */
266 Text
createLP(Composite parent
, String label
, String value
) {
267 Label lbl
= new Label(parent
, SWT
.NONE
);
269 lbl
.setLayoutData(new GridData(SWT
.LEAD
, SWT
.CENTER
, false, false));
270 Text text
= new Text(parent
, SWT
.PASSWORD
| SWT
.BORDER
);
272 text
.setLayoutData(new GridData(SWT
.LEAD
, SWT
.FILL
, true, false));
276 /** Creates label and text. */
277 Text
createLT(Composite parent
, String label
, String value
) {
278 Label lbl
= new Label(parent
, SWT
.NONE
);
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
);
284 text
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, false));
285 // CmsUtils.style(text, CmsWorkbenchStyles.WORKBENCH_FORM_TEXT);
289 Text
createReadOnlyLT(Composite parent
, String label
, String value
) {
290 Label lbl
= new Label(parent
, SWT
.NONE
);
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
);
296 text
.setLayoutData(new GridData(SWT
.LEAD
, SWT
.FILL
, true, false));
297 text
.setEditable(false);
298 // CmsUtils.style(text, CmsWorkbenchStyles.WORKBENCH_FORM_TEXT);