1 package org
.argeo
.cms
.ui
.workbench
.internal
.useradmin
.parts
;
3 import static org
.argeo
.api
.NodeInstance
.WORKGROUP
;
4 import static org
.argeo
.cms
.auth
.UserAdminUtils
.setProperty
;
5 import static org
.argeo
.naming
.LdapAttrs
.businessCategory
;
6 import static org
.argeo
.naming
.LdapAttrs
.description
;
8 import java
.util
.ArrayList
;
9 import java
.util
.Iterator
;
10 import java
.util
.List
;
12 import javax
.jcr
.Node
;
13 import javax
.jcr
.Repository
;
14 import javax
.jcr
.RepositoryException
;
15 import javax
.jcr
.Session
;
16 import javax
.naming
.InvalidNameException
;
17 import javax
.naming
.ldap
.LdapName
;
19 import org
.argeo
.api
.NodeInstance
;
20 import org
.argeo
.cms
.ArgeoNames
;
21 import org
.argeo
.cms
.CmsException
;
22 import org
.argeo
.cms
.auth
.UserAdminUtils
;
23 import org
.argeo
.cms
.jcr
.CmsJcrUtils
;
24 import org
.argeo
.cms
.ui
.util
.CmsUiUtils
;
25 import org
.argeo
.cms
.ui
.workbench
.CmsWorkbenchStyles
;
26 import org
.argeo
.cms
.ui
.workbench
.internal
.useradmin
.SecurityAdminImages
;
27 import org
.argeo
.cms
.ui
.workbench
.internal
.useradmin
.UserAdminWrapper
;
28 import org
.argeo
.cms
.ui
.workbench
.internal
.useradmin
.parts
.UserEditor
.GroupChangeListener
;
29 import org
.argeo
.cms
.ui
.workbench
.internal
.useradmin
.parts
.UserEditor
.MainInfoListener
;
30 import org
.argeo
.cms
.ui
.workbench
.internal
.useradmin
.providers
.CommonNameLP
;
31 import org
.argeo
.cms
.ui
.workbench
.internal
.useradmin
.providers
.MailLP
;
32 import org
.argeo
.cms
.ui
.workbench
.internal
.useradmin
.providers
.RoleIconLP
;
33 import org
.argeo
.cms
.ui
.workbench
.internal
.useradmin
.providers
.UserFilter
;
34 import org
.argeo
.cms
.ui
.workbench
.internal
.useradmin
.providers
.UserTableDefaultDClickListener
;
35 import org
.argeo
.eclipse
.ui
.ColumnDefinition
;
36 import org
.argeo
.eclipse
.ui
.EclipseUiUtils
;
37 import org
.argeo
.eclipse
.ui
.parts
.LdifUsersTable
;
38 import org
.argeo
.jcr
.JcrUtils
;
39 import org
.argeo
.naming
.LdapAttrs
;
40 import org
.argeo
.osgi
.transaction
.WorkTransaction
;
41 import org
.eclipse
.jface
.action
.Action
;
42 import org
.eclipse
.jface
.action
.ToolBarManager
;
43 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
44 import org
.eclipse
.jface
.resource
.ImageDescriptor
;
45 import org
.eclipse
.jface
.viewers
.ISelection
;
46 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
47 import org
.eclipse
.jface
.viewers
.TableViewer
;
48 import org
.eclipse
.jface
.viewers
.ViewerDropAdapter
;
49 import org
.eclipse
.swt
.SWT
;
50 import org
.eclipse
.swt
.dnd
.DND
;
51 import org
.eclipse
.swt
.dnd
.DropTargetEvent
;
52 import org
.eclipse
.swt
.dnd
.TextTransfer
;
53 import org
.eclipse
.swt
.dnd
.Transfer
;
54 import org
.eclipse
.swt
.dnd
.TransferData
;
55 import org
.eclipse
.swt
.events
.DisposeEvent
;
56 import org
.eclipse
.swt
.events
.DisposeListener
;
57 import org
.eclipse
.swt
.events
.ModifyListener
;
58 import org
.eclipse
.swt
.events
.SelectionAdapter
;
59 import org
.eclipse
.swt
.events
.SelectionEvent
;
60 import org
.eclipse
.swt
.graphics
.Cursor
;
61 import org
.eclipse
.swt
.layout
.GridData
;
62 import org
.eclipse
.swt
.layout
.GridLayout
;
63 import org
.eclipse
.swt
.widgets
.Composite
;
64 import org
.eclipse
.swt
.widgets
.Label
;
65 import org
.eclipse
.swt
.widgets
.Link
;
66 import org
.eclipse
.swt
.widgets
.Shell
;
67 import org
.eclipse
.swt
.widgets
.Text
;
68 import org
.eclipse
.swt
.widgets
.ToolBar
;
69 import org
.eclipse
.ui
.forms
.AbstractFormPart
;
70 import org
.eclipse
.ui
.forms
.IManagedForm
;
71 import org
.eclipse
.ui
.forms
.SectionPart
;
72 import org
.eclipse
.ui
.forms
.editor
.FormEditor
;
73 import org
.eclipse
.ui
.forms
.editor
.FormPage
;
74 import org
.eclipse
.ui
.forms
.widgets
.FormToolkit
;
75 import org
.eclipse
.ui
.forms
.widgets
.ScrolledForm
;
76 import org
.eclipse
.ui
.forms
.widgets
.Section
;
77 import org
.osgi
.service
.useradmin
.Group
;
78 import org
.osgi
.service
.useradmin
.Role
;
79 import org
.osgi
.service
.useradmin
.User
;
80 import org
.osgi
.service
.useradmin
.UserAdmin
;
81 import org
.osgi
.service
.useradmin
.UserAdminEvent
;
83 /** Display/edit main properties of a given group */
84 public class GroupMainPage
extends FormPage
implements ArgeoNames
{
85 final static String ID
= "GroupEditor.mainPage";
87 private final UserEditor editor
;
88 private final NodeInstance nodeInstance
;
89 private final UserAdminWrapper userAdminWrapper
;
90 private final Session session
;
92 public GroupMainPage(FormEditor editor
, UserAdminWrapper userAdminWrapper
, Repository repository
,
93 NodeInstance nodeInstance
) {
94 super(editor
, ID
, "Main");
96 session
= repository
.login();
97 } catch (RepositoryException e
) {
98 throw new CmsException("Cannot retrieve session of in MainGroupPage constructor", e
);
100 this.editor
= (UserEditor
) editor
;
101 this.userAdminWrapper
= userAdminWrapper
;
102 this.nodeInstance
= nodeInstance
;
105 protected void createFormContent(final IManagedForm mf
) {
106 ScrolledForm form
= mf
.getForm();
107 Composite body
= form
.getBody();
108 GridLayout mainLayout
= new GridLayout();
109 body
.setLayout(mainLayout
);
110 Group group
= (Group
) editor
.getDisplayedUser();
111 appendOverviewPart(body
, group
);
112 appendMembersPart(body
, group
);
116 public void dispose() {
117 JcrUtils
.logoutQuietly(session
);
121 /** Creates the general section */
122 protected void appendOverviewPart(final Composite parent
, final Group group
) {
123 FormToolkit tk
= getManagedForm().getToolkit();
124 Composite body
= addSection(tk
, parent
);
125 // GridLayout layout = new GridLayout(5, false);
126 GridLayout layout
= new GridLayout(2, false);
127 body
.setLayout(layout
);
129 String cn
= UserAdminUtils
.getProperty(group
, LdapAttrs
.cn
.name());
130 createReadOnlyLT(body
, "Name", cn
);
131 // Text dnTxt = createReadOnlyLT(body, "DN", group.getName());
132 createReadOnlyLT(body
, "Domain", UserAdminUtils
.getDomainName(group
));
135 Label descLbl
= new Label(body
, SWT
.LEAD
);
136 descLbl
.setFont(EclipseUiUtils
.getBoldFont(body
));
137 descLbl
.setText("Description");
138 descLbl
.setLayoutData(new GridData(SWT
.LEAD
, SWT
.CENTER
, false, false, 2, 1));
139 final Text descTxt
= new Text(body
, SWT
.LEAD
| SWT
.MULTI
| SWT
.WRAP
| SWT
.BORDER
);
140 GridData gd
= EclipseUiUtils
.fillAll();
142 gd
.horizontalSpan
= 2;
143 descTxt
.setLayoutData(gd
);
146 Link markAsWorkgroupLk
= new Link(body
, SWT
.NONE
);
147 markAsWorkgroupLk
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, false, false, 2, 1));
149 // create form part (controller)
150 final AbstractFormPart part
= new SectionPart((Section
) body
.getParent()) {
152 private MainInfoListener listener
;
155 public void initialize(IManagedForm form
) {
156 super.initialize(form
);
157 listener
= editor
.new MainInfoListener(parent
.getDisplay(), this);
158 userAdminWrapper
.addListener(listener
);
162 public void dispose() {
163 userAdminWrapper
.removeListener(listener
);
167 public void commit(boolean onSave
) {
168 // group.getProperties().put(LdapAttrs.description.name(), descTxt.getText());
169 setProperty(group
, description
, descTxt
.getText());
170 super.commit(onSave
);
174 public void refresh() {
175 // dnTxt.setText(group.getName());
176 // cnTxt.setText(UserAdminUtils.getProperty(group, LdapAttrs.cn.name()));
177 descTxt
.setText(UserAdminUtils
.getProperty(group
, LdapAttrs
.description
.name()));
178 Node workgroupHome
= CmsJcrUtils
.getGroupHome(session
, cn
);
179 if (workgroupHome
== null)
180 markAsWorkgroupLk
.setText("<a>Mark as workgroup</a>");
182 markAsWorkgroupLk
.setText("Configured as workgroup");
183 parent
.layout(true, true);
188 markAsWorkgroupLk
.addSelectionListener(new SelectionAdapter() {
189 private static final long serialVersionUID
= -6439340898096365078L;
192 public void widgetSelected(SelectionEvent e
) {
194 boolean confirmed
= MessageDialog
.openConfirm(parent
.getShell(), "Mark as workgroup",
195 "Are you sure you want to mark " + cn
+ " as being a workgroup? ");
197 Node workgroupHome
= CmsJcrUtils
.getGroupHome(session
, cn
);
198 if (workgroupHome
!= null)
199 return; // already marked as workgroup, do nothing
202 // improve transaction management
203 userAdminWrapper
.beginTransactionIfNeeded();
204 nodeInstance
.createWorkgroup(new LdapName(group
.getName()));
205 setProperty(group
, businessCategory
, WORKGROUP
);
206 userAdminWrapper
.commitOrNotifyTransactionStateChange();
208 .notifyListeners(new UserAdminEvent(null, UserAdminEvent
.ROLE_CHANGED
, group
));
210 } catch (InvalidNameException e1
) {
211 throw new CmsException("Cannot create Workgroup for " + group
.toString(), e1
);
218 ModifyListener defaultListener
= editor
.new FormPartML(part
);
219 descTxt
.addModifyListener(defaultListener
);
220 getManagedForm().addPart(part
);
223 /** Filtered table with members. Has drag and drop ability */
224 protected void appendMembersPart(Composite parent
, Group group
) {
225 FormToolkit tk
= getManagedForm().getToolkit();
226 Section section
= tk
.createSection(parent
, Section
.TITLE_BAR
);
227 section
.setText("Members");
228 section
.setLayoutData(EclipseUiUtils
.fillAll());
230 Composite body
= new Composite(section
, SWT
.NO_FOCUS
);
231 section
.setClient(body
);
232 body
.setLayoutData(EclipseUiUtils
.fillAll());
234 LdifUsersTable userTableViewerCmp
= createMemberPart(body
, group
);
236 SectionPart part
= new GroupMembersPart(section
, userTableViewerCmp
);
237 getManagedForm().addPart(part
);
238 addRemoveAbitily(part
, userTableViewerCmp
.getTableViewer(), group
);
241 public LdifUsersTable
createMemberPart(Composite parent
, Group group
) {
242 parent
.setLayout(EclipseUiUtils
.noSpaceGridLayout());
244 // Define the displayed columns
245 List
<ColumnDefinition
> columnDefs
= new ArrayList
<ColumnDefinition
>();
246 columnDefs
.add(new ColumnDefinition(new RoleIconLP(), "", 0, 24));
247 columnDefs
.add(new ColumnDefinition(new CommonNameLP(), "Name", 150));
248 columnDefs
.add(new ColumnDefinition(new MailLP(), "Mail", 150));
249 // columnDefs.add(new ColumnDefinition(new UserNameLP(), "Distinguished Name",
252 // Create and configure the table
253 LdifUsersTable userViewerCmp
= new MyUserTableViewer(parent
, SWT
.MULTI
| SWT
.H_SCROLL
| SWT
.V_SCROLL
,
254 userAdminWrapper
.getUserAdmin());
256 userViewerCmp
.setColumnDefinitions(columnDefs
);
257 userViewerCmp
.populate(true, false);
258 userViewerCmp
.setLayoutData(EclipseUiUtils
.fillAll());
261 TableViewer userViewer
= userViewerCmp
.getTableViewer();
262 userViewer
.addDoubleClickListener(new UserTableDefaultDClickListener());
263 int operations
= DND
.DROP_COPY
| DND
.DROP_MOVE
;
264 Transfer
[] tt
= new Transfer
[] { TextTransfer
.getInstance() };
265 userViewer
.addDropSupport(operations
, tt
,
266 new GroupDropListener(userAdminWrapper
, userViewerCmp
, (Group
) editor
.getDisplayedUser()));
268 return userViewerCmp
;
272 private class MyUserTableViewer
extends LdifUsersTable
{
273 private static final long serialVersionUID
= 8467999509931900367L;
275 private final UserFilter userFilter
;
277 public MyUserTableViewer(Composite parent
, int style
, UserAdmin userAdmin
) {
278 super(parent
, style
, true);
279 userFilter
= new UserFilter();
284 protected List
<User
> listFilteredElements(String filter
) {
285 // reload user and set it in the editor
286 Group group
= (Group
) editor
.getDisplayedUser();
287 Role
[] roles
= group
.getMembers();
288 List
<User
> users
= new ArrayList
<User
>();
289 userFilter
.setSearchText(filter
);
290 // userFilter.setShowSystemRole(true);
291 for (Role role
: roles
)
292 // if (role.getType() == Role.GROUP)
293 if (userFilter
.select(null, null, role
))
294 users
.add((User
) role
);
299 private void addRemoveAbitily(SectionPart sectionPart
, TableViewer userViewer
, Group group
) {
300 Section section
= sectionPart
.getSection();
301 ToolBarManager toolBarManager
= new ToolBarManager(SWT
.FLAT
);
302 ToolBar toolbar
= toolBarManager
.createControl(section
);
303 final Cursor handCursor
= new Cursor(section
.getDisplay(), SWT
.CURSOR_HAND
);
304 toolbar
.setCursor(handCursor
);
305 toolbar
.addDisposeListener(new DisposeListener() {
306 private static final long serialVersionUID
= 3882131405820522925L;
308 public void widgetDisposed(DisposeEvent e
) {
309 if ((handCursor
!= null) && (handCursor
.isDisposed() == false)) {
310 handCursor
.dispose();
315 Action action
= new RemoveMembershipAction(userViewer
, group
, "Remove selected items from this group",
316 SecurityAdminImages
.ICON_REMOVE_DESC
);
317 toolBarManager
.add(action
);
318 toolBarManager
.update(true);
319 section
.setTextClient(toolbar
);
322 private class RemoveMembershipAction
extends Action
{
323 private static final long serialVersionUID
= -1337713097184522588L;
325 private final TableViewer userViewer
;
326 private final Group group
;
328 RemoveMembershipAction(TableViewer userViewer
, Group group
, String name
, ImageDescriptor img
) {
330 this.userViewer
= userViewer
;
336 ISelection selection
= userViewer
.getSelection();
337 if (selection
.isEmpty())
340 @SuppressWarnings("unchecked")
341 Iterator
<User
> it
= ((IStructuredSelection
) selection
).iterator();
342 List
<User
> users
= new ArrayList
<User
>();
343 while (it
.hasNext()) {
344 User currUser
= it
.next();
348 userAdminWrapper
.beginTransactionIfNeeded();
349 for (User user
: users
) {
350 group
.removeMember(user
);
352 userAdminWrapper
.commitOrNotifyTransactionStateChange();
353 userAdminWrapper
.notifyListeners(new UserAdminEvent(null, UserAdminEvent
.ROLE_CHANGED
, group
));
358 private class GroupMembersPart
extends SectionPart
{
359 private final LdifUsersTable userViewer
;
360 // private final Group group;
362 private GroupChangeListener listener
;
364 public GroupMembersPart(Section section
, LdifUsersTable userViewer
) {
366 this.userViewer
= userViewer
;
367 // this.group = group;
371 public void initialize(IManagedForm form
) {
372 super.initialize(form
);
373 listener
= editor
.new GroupChangeListener(userViewer
.getDisplay(), GroupMembersPart
.this);
374 userAdminWrapper
.addListener(listener
);
378 public void dispose() {
379 userAdminWrapper
.removeListener(listener
);
384 public void refresh() {
385 userViewer
.refresh();
391 * Defines this table as being a potential target to add group membership
392 * (roles) to this group
394 private class GroupDropListener
extends ViewerDropAdapter
{
395 private static final long serialVersionUID
= 2893468717831451621L;
397 private final UserAdminWrapper userAdminWrapper
;
398 // private final LdifUsersTable myUserViewerCmp;
399 private final Group myGroup
;
401 public GroupDropListener(UserAdminWrapper userAdminWrapper
, LdifUsersTable userTableViewerCmp
, Group group
) {
402 super(userTableViewerCmp
.getTableViewer());
403 this.userAdminWrapper
= userAdminWrapper
;
404 this.myGroup
= group
;
405 // this.myUserViewerCmp = userTableViewerCmp;
409 public boolean validateDrop(Object target
, int operation
, TransferData transferType
) {
410 // Target is always OK in a list only view
411 // TODO check if not a string
412 boolean validDrop
= true;
417 public void drop(DropTargetEvent event
) {
418 // TODO Is there an opportunity to perform the check before?
419 String newUserName
= (String
) event
.data
;
420 UserAdmin myUserAdmin
= userAdminWrapper
.getUserAdmin();
421 Role role
= myUserAdmin
.getRole(newUserName
);
422 if (role
.getType() == Role
.GROUP
) {
423 Group newGroup
= (Group
) role
;
424 Shell shell
= getViewer().getControl().getShell();
426 if (myGroup
== newGroup
) { // Equality
427 MessageDialog
.openError(shell
, "Forbidden addition ", "A group cannot be a member of itself.");
432 String myName
= myGroup
.getName();
433 List
<User
> myMemberships
= editor
.getFlatGroups(myGroup
);
434 if (myMemberships
.contains(newGroup
)) {
435 MessageDialog
.openError(shell
, "Forbidden addition: cycle",
436 "Cannot add " + newUserName
+ " to group " + myName
+ ". This would create a cycle");
441 List
<User
> newGroupMemberships
= editor
.getFlatGroups(newGroup
);
442 if (newGroupMemberships
.contains(myGroup
)) {
443 MessageDialog
.openError(shell
, "Forbidden addition",
444 "Cannot add " + newUserName
+ " to group " + myName
+ ", this membership already exists");
447 userAdminWrapper
.beginTransactionIfNeeded();
448 myGroup
.addMember(newGroup
);
449 userAdminWrapper
.commitOrNotifyTransactionStateChange();
450 userAdminWrapper
.notifyListeners(new UserAdminEvent(null, UserAdminEvent
.ROLE_CHANGED
, myGroup
));
451 } else if (role
.getType() == Role
.USER
) {
452 // TODO check if the group is already member of this group
453 WorkTransaction transaction
= userAdminWrapper
.beginTransactionIfNeeded();
454 User user
= (User
) role
;
455 myGroup
.addMember(user
);
456 if (UserAdminWrapper
.COMMIT_ON_SAVE
)
458 transaction
.commit();
459 } catch (Exception e
) {
460 throw new CmsException("Cannot commit transaction " + "after user group membership update", e
);
462 userAdminWrapper
.notifyListeners(new UserAdminEvent(null, UserAdminEvent
.ROLE_CHANGED
, myGroup
));
468 public boolean performDrop(Object data
) {
469 // myUserViewerCmp.refresh();
475 private Composite
addSection(FormToolkit tk
, Composite parent
) {
476 Section section
= tk
.createSection(parent
, SWT
.NO_FOCUS
);
477 section
.setLayoutData(EclipseUiUtils
.fillWidth());
478 Composite body
= tk
.createComposite(section
, SWT
.WRAP
);
479 body
.setLayoutData(EclipseUiUtils
.fillAll());
480 section
.setClient(body
);
484 /** Creates label and text. */
485 // private Text createLT(Composite parent, String label, String value) {
486 // FormToolkit toolkit = getManagedForm().getToolkit();
487 // Label lbl = toolkit.createLabel(parent, label);
488 // lbl.setLayoutData(new GridData(SWT.LEAD, SWT.CENTER, false, false));
489 // lbl.setFont(EclipseUiUtils.getBoldFont(parent));
490 // Text text = toolkit.createText(parent, value, SWT.BORDER);
491 // text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
492 // CmsUiUtils.style(text, CmsWorkbenchStyles.WORKBENCH_FORM_TEXT);
496 Text
createReadOnlyLT(Composite parent
, String label
, String value
) {
497 FormToolkit toolkit
= getManagedForm().getToolkit();
498 Label lbl
= toolkit
.createLabel(parent
, label
);
499 lbl
.setLayoutData(new GridData(SWT
.LEAD
, SWT
.CENTER
, false, false));
500 lbl
.setFont(EclipseUiUtils
.getBoldFont(parent
));
501 Text text
= toolkit
.createText(parent
, value
, SWT
.NONE
);
502 text
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
503 text
.setEditable(false);
504 CmsUiUtils
.style(text
, CmsWorkbenchStyles
.WORKBENCH_FORM_TEXT
);