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 static org
.argeo
.cms
.util
.UserAdminUtils
.setProperty
;
19 import static org
.argeo
.naming
.LdapAttrs
.businessCategory
;
20 import static org
.argeo
.naming
.LdapAttrs
.description
;
21 import static org
.argeo
.node
.NodeInstance
.WORKGROUP
;
23 import java
.util
.ArrayList
;
24 import java
.util
.Iterator
;
25 import java
.util
.List
;
27 import javax
.inject
.Inject
;
28 import javax
.jcr
.Node
;
29 import javax
.jcr
.Repository
;
30 import javax
.jcr
.RepositoryException
;
31 import javax
.jcr
.Session
;
32 import javax
.naming
.InvalidNameException
;
33 import javax
.naming
.ldap
.LdapName
;
34 import javax
.transaction
.UserTransaction
;
36 import org
.argeo
.cms
.ArgeoNames
;
37 import org
.argeo
.cms
.CmsException
;
38 import org
.argeo
.cms
.e4
.users
.providers
.CommonNameLP
;
39 import org
.argeo
.cms
.e4
.users
.providers
.MailLP
;
40 import org
.argeo
.cms
.e4
.users
.providers
.RoleIconLP
;
41 import org
.argeo
.cms
.e4
.users
.providers
.UserFilter
;
42 import org
.argeo
.cms
.ui
.eclipse
.forms
.AbstractFormPart
;
43 import org
.argeo
.cms
.ui
.eclipse
.forms
.IManagedForm
;
44 import org
.argeo
.cms
.util
.UserAdminUtils
;
45 import org
.argeo
.eclipse
.ui
.ColumnDefinition
;
46 import org
.argeo
.eclipse
.ui
.EclipseUiUtils
;
47 import org
.argeo
.eclipse
.ui
.parts
.LdifUsersTable
;
48 import org
.argeo
.jcr
.JcrUtils
;
49 import org
.argeo
.naming
.LdapAttrs
;
50 import org
.argeo
.node
.NodeInstance
;
51 import org
.argeo
.node
.NodeUtils
;
52 import org
.eclipse
.e4
.ui
.workbench
.modeling
.EPartService
;
53 import org
.eclipse
.jface
.action
.Action
;
54 import org
.eclipse
.jface
.action
.ToolBarManager
;
55 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
56 import org
.eclipse
.jface
.resource
.ImageDescriptor
;
57 import org
.eclipse
.jface
.viewers
.ISelection
;
58 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
59 import org
.eclipse
.jface
.viewers
.TableViewer
;
60 import org
.eclipse
.jface
.viewers
.ViewerDropAdapter
;
61 import org
.eclipse
.swt
.SWT
;
62 import org
.eclipse
.swt
.dnd
.DND
;
63 import org
.eclipse
.swt
.dnd
.DropTargetEvent
;
64 import org
.eclipse
.swt
.dnd
.TextTransfer
;
65 import org
.eclipse
.swt
.dnd
.Transfer
;
66 import org
.eclipse
.swt
.dnd
.TransferData
;
67 import org
.eclipse
.swt
.events
.DisposeEvent
;
68 import org
.eclipse
.swt
.events
.DisposeListener
;
69 import org
.eclipse
.swt
.events
.ModifyListener
;
70 import org
.eclipse
.swt
.events
.SelectionAdapter
;
71 import org
.eclipse
.swt
.events
.SelectionEvent
;
72 import org
.eclipse
.swt
.graphics
.Cursor
;
73 import org
.eclipse
.swt
.layout
.GridData
;
74 import org
.eclipse
.swt
.layout
.GridLayout
;
75 import org
.eclipse
.swt
.widgets
.Composite
;
76 import org
.eclipse
.swt
.widgets
.Label
;
77 import org
.eclipse
.swt
.widgets
.Link
;
78 import org
.eclipse
.swt
.widgets
.Shell
;
79 import org
.eclipse
.swt
.widgets
.Text
;
80 import org
.eclipse
.swt
.widgets
.ToolBar
;
81 import org
.osgi
.service
.useradmin
.Group
;
82 import org
.osgi
.service
.useradmin
.Role
;
83 //import org.eclipse.ui.forms.AbstractFormPart;
84 //import org.eclipse.ui.forms.IManagedForm;
85 //import org.eclipse.ui.forms.SectionPart;
86 //import org.eclipse.ui.forms.editor.FormEditor;
87 //import org.eclipse.ui.forms.editor.FormPage;
88 //import org.eclipse.ui.forms.widgets.FormToolkit;
89 //import org.eclipse.ui.forms.widgets.ScrolledForm;
90 //import org.eclipse.ui.forms.widgets.Section;
91 import org
.osgi
.service
.useradmin
.User
;
92 import org
.osgi
.service
.useradmin
.UserAdmin
;
93 import org
.osgi
.service
.useradmin
.UserAdminEvent
;
95 /** Display/edit main properties of a given group */
96 public class GroupEditor
extends AbstractRoleEditor
implements ArgeoNames
{
97 // final static String ID = "GroupEditor.mainPage";
100 private EPartService partService
;
102 // private final UserEditor editor;
104 private Repository repository
;
106 private NodeInstance nodeInstance
;
107 // private final UserAdminWrapper userAdminWrapper;
108 private Session session
;
110 // public GroupMainPage(FormEditor editor, UserAdminWrapper userAdminWrapper,
111 // Repository repository,
112 // NodeInstance nodeInstance) {
113 // super(editor, ID, "Main");
115 // session = repository.login();
116 // } catch (RepositoryException e) {
117 // throw new CmsException("Cannot retrieve session of in MainGroupPage
120 // this.editor = (UserEditor) editor;
121 // this.userAdminWrapper = userAdminWrapper;
122 // this.nodeInstance = nodeInstance;
125 // protected void createFormContent(final IManagedForm mf) {
126 // ScrolledForm form = mf.getForm();
127 // Composite body = form.getBody();
128 // GridLayout mainLayout = new GridLayout();
129 // body.setLayout(mainLayout);
130 // Group group = (Group) editor.getDisplayedUser();
131 // appendOverviewPart(body, group);
132 // appendMembersPart(body, group);
136 protected void createUi(Composite parent
) {
138 session
= repository
.login();
139 } catch (RepositoryException e
) {
140 throw new CmsException("Cannot retrieve session", e
);
142 // ScrolledForm form = mf.getForm();
143 // Composite body = form.getBody();
144 // Composite body = new Composite(parent, SWT.NONE);
145 Composite body
= parent
;
146 GridLayout mainLayout
= new GridLayout();
147 body
.setLayout(mainLayout
);
148 Group group
= (Group
) getDisplayedUser();
149 appendOverviewPart(body
, group
);
150 appendMembersPart(body
, group
);
154 public void dispose() {
155 JcrUtils
.logoutQuietly(session
);
159 /** Creates the general section */
160 protected void appendOverviewPart(final Composite parent
, final Group group
) {
161 Composite body
= new Composite(parent
, SWT
.BORDER
);
162 // GridLayout layout = new GridLayout(5, false);
163 GridLayout layout
= new GridLayout(2, false);
164 body
.setLayout(layout
);
166 String cn
= UserAdminUtils
.getProperty(group
, LdapAttrs
.cn
.name());
167 createReadOnlyLT(body
, "Name", cn
);
168 // Text dnTxt = createReadOnlyLT(body, "DN", group.getName());
169 createReadOnlyLT(body
, "Domain", UserAdminUtils
.getDomainName(group
));
172 Label descLbl
= new Label(body
, SWT
.LEAD
);
173 descLbl
.setFont(EclipseUiUtils
.getBoldFont(body
));
174 descLbl
.setText("Description");
175 descLbl
.setLayoutData(new GridData(SWT
.LEAD
, SWT
.CENTER
, true, false, 2, 1));
176 final Text descTxt
= new Text(body
, SWT
.LEAD
| SWT
.MULTI
| SWT
.WRAP
| SWT
.BORDER
);
177 GridData gd
= EclipseUiUtils
.fillWidth();
179 gd
.horizontalSpan
= 2;
180 descTxt
.setLayoutData(gd
);
183 Link markAsWorkgroupLk
= new Link(body
, SWT
.NONE
);
184 markAsWorkgroupLk
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, false, false, 2, 1));
186 // create form part (controller)
187 final AbstractFormPart part
= new AbstractFormPart() {
189 private MainInfoListener listener
;
192 public void initialize(IManagedForm form
) {
193 super.initialize(form
);
194 listener
= new MainInfoListener(parent
.getDisplay(), this);
195 userAdminWrapper
.addListener(listener
);
199 public void dispose() {
200 userAdminWrapper
.removeListener(listener
);
204 public void commit(boolean onSave
) {
205 // group.getProperties().put(LdapAttrs.description.name(), descTxt.getText());
206 setProperty(group
, description
, descTxt
.getText());
207 super.commit(onSave
);
211 public void refresh() {
212 // dnTxt.setText(group.getName());
213 // cnTxt.setText(UserAdminUtils.getProperty(group, LdapAttrs.cn.name()));
214 descTxt
.setText(UserAdminUtils
.getProperty(group
, LdapAttrs
.description
.name()));
215 Node workgroupHome
= NodeUtils
.getGroupHome(session
, cn
);
216 if (workgroupHome
== null)
217 markAsWorkgroupLk
.setText("<a>Mark as workgroup</a>");
219 markAsWorkgroupLk
.setText("Configured as workgroup");
220 parent
.layout(true, true);
225 markAsWorkgroupLk
.addSelectionListener(new SelectionAdapter() {
226 private static final long serialVersionUID
= -6439340898096365078L;
229 public void widgetSelected(SelectionEvent e
) {
231 boolean confirmed
= MessageDialog
.openConfirm(parent
.getShell(), "Mark as workgroup",
232 "Are you sure you want to mark " + cn
+ " as being a workgroup? ");
234 Node workgroupHome
= NodeUtils
.getGroupHome(session
, cn
);
235 if (workgroupHome
!= null)
236 return; // already marked as workgroup, do nothing
239 // improve transaction management
240 userAdminWrapper
.beginTransactionIfNeeded();
241 nodeInstance
.createWorkgroup(new LdapName(group
.getName()));
242 setProperty(group
, businessCategory
, WORKGROUP
);
243 userAdminWrapper
.commitOrNotifyTransactionStateChange();
245 .notifyListeners(new UserAdminEvent(null, UserAdminEvent
.ROLE_CHANGED
, group
));
247 } catch (InvalidNameException e1
) {
248 throw new CmsException("Cannot create Workgroup for " + group
.toString(), e1
);
255 ModifyListener defaultListener
= new FormPartML(part
);
256 descTxt
.addModifyListener(defaultListener
);
257 getManagedForm().addPart(part
);
260 /** Filtered table with members. Has drag and drop ability */
261 protected void appendMembersPart(Composite parent
, Group group
) {
262 // Section section = tk.createSection(parent, Section.TITLE_BAR);
263 // section.setText("Members");
264 // section.setLayoutData(EclipseUiUtils.fillAll());
266 Composite body
= new Composite(parent
, SWT
.NO_FOCUS
);
267 body
.setLayout(EclipseUiUtils
.noSpaceGridLayout());
268 // section.setClient(body);
269 body
.setLayoutData(EclipseUiUtils
.fillAll());
271 LdifUsersTable userTableViewerCmp
= createMemberPart(body
, group
);
273 AbstractFormPart part
= new GroupMembersPart(userTableViewerCmp
);
274 getManagedForm().addPart(part
);
275 addRemoveAbitily(body
, userTableViewerCmp
.getTableViewer(), group
);
278 private LdifUsersTable
createMemberPart(Composite parent
, Group group
) {
280 // Define the displayed columns
281 List
<ColumnDefinition
> columnDefs
= new ArrayList
<ColumnDefinition
>();
282 columnDefs
.add(new ColumnDefinition(new RoleIconLP(), "", 0, 24));
283 columnDefs
.add(new ColumnDefinition(new CommonNameLP(), "Name", 150));
284 columnDefs
.add(new ColumnDefinition(new MailLP(), "Mail", 150));
285 // columnDefs.add(new ColumnDefinition(new UserNameLP(), "Distinguished Name",
288 // Create and configure the table
289 LdifUsersTable userViewerCmp
= new MyUserTableViewer(parent
, SWT
.MULTI
| SWT
.H_SCROLL
| SWT
.V_SCROLL
,
290 userAdminWrapper
.getUserAdmin());
292 userViewerCmp
.setColumnDefinitions(columnDefs
);
293 userViewerCmp
.populate(true, false);
294 userViewerCmp
.setLayoutData(EclipseUiUtils
.fillAll());
297 TableViewer userViewer
= userViewerCmp
.getTableViewer();
298 userViewer
.addDoubleClickListener(new UserTableDefaultDClickListener(partService
));
299 int operations
= DND
.DROP_COPY
| DND
.DROP_MOVE
;
300 Transfer
[] tt
= new Transfer
[] { TextTransfer
.getInstance() };
301 userViewer
.addDropSupport(operations
, tt
,
302 new GroupDropListener(userAdminWrapper
, userViewerCmp
, (Group
) getDisplayedUser()));
304 return userViewerCmp
;
308 private class MyUserTableViewer
extends LdifUsersTable
{
309 private static final long serialVersionUID
= 8467999509931900367L;
311 private final UserFilter userFilter
;
313 public MyUserTableViewer(Composite parent
, int style
, UserAdmin userAdmin
) {
314 super(parent
, style
, true);
315 userFilter
= new UserFilter();
320 protected List
<User
> listFilteredElements(String filter
) {
321 // reload user and set it in the editor
322 Group group
= (Group
) getDisplayedUser();
323 Role
[] roles
= group
.getMembers();
324 List
<User
> users
= new ArrayList
<User
>();
325 userFilter
.setSearchText(filter
);
326 // userFilter.setShowSystemRole(true);
327 for (Role role
: roles
)
328 // if (role.getType() == Role.GROUP)
329 if (userFilter
.select(null, null, role
))
330 users
.add((User
) role
);
335 private void addRemoveAbitily(Composite parent
, TableViewer userViewer
, Group group
) {
336 // Section section = sectionPart.getSection();
337 ToolBarManager toolBarManager
= new ToolBarManager(SWT
.FLAT
);
338 ToolBar toolbar
= toolBarManager
.createControl(parent
);
339 final Cursor handCursor
= new Cursor(parent
.getDisplay(), SWT
.CURSOR_HAND
);
340 toolbar
.setCursor(handCursor
);
341 toolbar
.addDisposeListener(new DisposeListener() {
342 private static final long serialVersionUID
= 3882131405820522925L;
344 public void widgetDisposed(DisposeEvent e
) {
345 if ((handCursor
!= null) && (handCursor
.isDisposed() == false)) {
346 handCursor
.dispose();
351 Action action
= new RemoveMembershipAction(userViewer
, group
, "Remove selected items from this group",
352 SecurityAdminImages
.ICON_REMOVE_DESC
);
353 toolBarManager
.add(action
);
354 toolBarManager
.update(true);
355 // section.setTextClient(toolbar);
358 private class RemoveMembershipAction
extends Action
{
359 private static final long serialVersionUID
= -1337713097184522588L;
361 private final TableViewer userViewer
;
362 private final Group group
;
364 RemoveMembershipAction(TableViewer userViewer
, Group group
, String name
, ImageDescriptor img
) {
366 this.userViewer
= userViewer
;
372 ISelection selection
= userViewer
.getSelection();
373 if (selection
.isEmpty())
376 @SuppressWarnings("unchecked")
377 Iterator
<User
> it
= ((IStructuredSelection
) selection
).iterator();
378 List
<User
> users
= new ArrayList
<User
>();
379 while (it
.hasNext()) {
380 User currUser
= it
.next();
384 userAdminWrapper
.beginTransactionIfNeeded();
385 for (User user
: users
) {
386 group
.removeMember(user
);
388 userAdminWrapper
.commitOrNotifyTransactionStateChange();
389 userAdminWrapper
.notifyListeners(new UserAdminEvent(null, UserAdminEvent
.ROLE_CHANGED
, group
));
394 private class GroupMembersPart
extends AbstractFormPart
{
395 private final LdifUsersTable userViewer
;
396 // private final Group group;
398 private GroupChangeListener listener
;
400 public GroupMembersPart(LdifUsersTable userViewer
) {
402 this.userViewer
= userViewer
;
403 // this.group = group;
407 public void initialize(IManagedForm form
) {
408 super.initialize(form
);
409 listener
= new GroupChangeListener(userViewer
.getDisplay(), GroupMembersPart
.this);
410 userAdminWrapper
.addListener(listener
);
414 public void dispose() {
415 userAdminWrapper
.removeListener(listener
);
420 public void refresh() {
421 userViewer
.refresh();
427 * Defines this table as being a potential target to add group membership
428 * (roles) to this group
430 private class GroupDropListener
extends ViewerDropAdapter
{
431 private static final long serialVersionUID
= 2893468717831451621L;
433 private final UserAdminWrapper userAdminWrapper
;
434 // private final LdifUsersTable myUserViewerCmp;
435 private final Group myGroup
;
437 public GroupDropListener(UserAdminWrapper userAdminWrapper
, LdifUsersTable userTableViewerCmp
, Group group
) {
438 super(userTableViewerCmp
.getTableViewer());
439 this.userAdminWrapper
= userAdminWrapper
;
440 this.myGroup
= group
;
441 // this.myUserViewerCmp = userTableViewerCmp;
445 public boolean validateDrop(Object target
, int operation
, TransferData transferType
) {
446 // Target is always OK in a list only view
447 // TODO check if not a string
448 boolean validDrop
= true;
453 public void drop(DropTargetEvent event
) {
454 // TODO Is there an opportunity to perform the check before?
455 String newUserName
= (String
) event
.data
;
456 UserAdmin myUserAdmin
= userAdminWrapper
.getUserAdmin();
457 Role role
= myUserAdmin
.getRole(newUserName
);
458 if (role
.getType() == Role
.GROUP
) {
459 Group newGroup
= (Group
) role
;
460 Shell shell
= getViewer().getControl().getShell();
462 if (myGroup
== newGroup
) { // Equality
463 MessageDialog
.openError(shell
, "Forbidden addition ", "A group cannot be a member of itself.");
468 String myName
= myGroup
.getName();
469 List
<User
> myMemberships
= getFlatGroups(myGroup
);
470 if (myMemberships
.contains(newGroup
)) {
471 MessageDialog
.openError(shell
, "Forbidden addition: cycle",
472 "Cannot add " + newUserName
+ " to group " + myName
+ ". This would create a cycle");
477 List
<User
> newGroupMemberships
= getFlatGroups(newGroup
);
478 if (newGroupMemberships
.contains(myGroup
)) {
479 MessageDialog
.openError(shell
, "Forbidden addition",
480 "Cannot add " + newUserName
+ " to group " + myName
+ ", this membership already exists");
483 userAdminWrapper
.beginTransactionIfNeeded();
484 myGroup
.addMember(newGroup
);
485 userAdminWrapper
.commitOrNotifyTransactionStateChange();
486 userAdminWrapper
.notifyListeners(new UserAdminEvent(null, UserAdminEvent
.ROLE_CHANGED
, myGroup
));
487 } else if (role
.getType() == Role
.USER
) {
488 // TODO check if the group is already member of this group
489 UserTransaction transaction
= userAdminWrapper
.beginTransactionIfNeeded();
490 User user
= (User
) role
;
491 myGroup
.addMember(user
);
492 if (UserAdminWrapper
.COMMIT_ON_SAVE
)
494 transaction
.commit();
495 } catch (Exception e
) {
496 throw new CmsException("Cannot commit transaction " + "after user group membership update", e
);
498 userAdminWrapper
.notifyListeners(new UserAdminEvent(null, UserAdminEvent
.ROLE_CHANGED
, myGroup
));
504 public boolean performDrop(Object data
) {
505 // myUserViewerCmp.refresh();
511 // private Composite addSection(FormToolkit tk, Composite parent) {
512 // Section section = tk.createSection(parent, SWT.NO_FOCUS);
513 // section.setLayoutData(EclipseUiUtils.fillWidth());
514 // Composite body = tk.createComposite(section, SWT.WRAP);
515 // body.setLayoutData(EclipseUiUtils.fillAll());
516 // section.setClient(body);
520 /** Creates label and text. */
521 // private Text createLT(Composite parent, String label, String value) {
522 // FormToolkit toolkit = getManagedForm().getToolkit();
523 // Label lbl = toolkit.createLabel(parent, label);
524 // lbl.setLayoutData(new GridData(SWT.LEAD, SWT.CENTER, false, false));
525 // lbl.setFont(EclipseUiUtils.getBoldFont(parent));
526 // Text text = toolkit.createText(parent, value, SWT.BORDER);
527 // text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
528 // CmsUtils.style(text, CmsWorkbenchStyles.WORKBENCH_FORM_TEXT);
532 // Text createReadOnlyLT(Composite parent, String label, String value) {
533 // FormToolkit toolkit = getManagedForm().getToolkit();
534 // Label lbl = toolkit.createLabel(parent, label);
535 // lbl.setLayoutData(new GridData(SWT.LEAD, SWT.CENTER, false, false));
536 // lbl.setFont(EclipseUiUtils.getBoldFont(parent));
537 // Text text = toolkit.createText(parent, value, SWT.NONE);
538 // text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
539 // text.setEditable(false);
540 // CmsUtils.style(text, CmsWorkbenchStyles.WORKBENCH_FORM_TEXT);