1 package org
.argeo
.cms
.e4
.users
;
3 import java
.util
.ArrayList
;
6 import javax
.annotation
.PostConstruct
;
7 import javax
.annotation
.PreDestroy
;
8 import javax
.inject
.Inject
;
10 import org
.argeo
.api
.cms
.CmsConstants
;
11 import org
.argeo
.api
.cms
.CmsLog
;
12 import org
.argeo
.cms
.auth
.CurrentUser
;
13 import org
.argeo
.cms
.e4
.users
.providers
.CommonNameLP
;
14 import org
.argeo
.cms
.e4
.users
.providers
.DomainNameLP
;
15 import org
.argeo
.cms
.e4
.users
.providers
.RoleIconLP
;
16 import org
.argeo
.cms
.e4
.users
.providers
.UserDragListener
;
17 import org
.argeo
.cms
.swt
.CmsException
;
18 import org
.argeo
.cms
.swt
.useradmin
.LdifUsersTable
;
19 //import org.argeo.cms.ui.workbench.WorkbenchUiPlugin;
20 //import org.argeo.cms.ui.workbench.internal.useradmin.UiUserAdminListener;
21 //import org.argeo.cms.ui.workbench.internal.useradmin.UserAdminWrapper;
22 //import org.argeo.cms.ui.workbench.internal.useradmin.providers.CommonNameLP;
23 //import org.argeo.cms.ui.workbench.internal.useradmin.providers.DomainNameLP;
24 //import org.argeo.cms.ui.workbench.internal.useradmin.providers.RoleIconLP;
25 //import org.argeo.cms.ui.workbench.internal.useradmin.providers.UserDragListener;
26 //import org.argeo.cms.ui.workbench.internal.useradmin.providers.UserTableDefaultDClickListener;
27 import org
.argeo
.eclipse
.ui
.ColumnDefinition
;
28 import org
.argeo
.eclipse
.ui
.EclipseUiUtils
;
29 import org
.argeo
.util
.naming
.LdapAttrs
;
30 import org
.argeo
.util
.naming
.LdapObjs
;
31 import org
.eclipse
.e4
.ui
.di
.Focus
;
32 import org
.eclipse
.e4
.ui
.workbench
.modeling
.EPartService
;
33 import org
.eclipse
.e4
.ui
.workbench
.modeling
.ESelectionService
;
34 import org
.eclipse
.jface
.viewers
.ISelectionChangedListener
;
35 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
36 import org
.eclipse
.jface
.viewers
.SelectionChangedEvent
;
37 import org
.eclipse
.jface
.viewers
.TableViewer
;
38 import org
.eclipse
.swt
.SWT
;
39 import org
.eclipse
.swt
.dnd
.DND
;
40 import org
.eclipse
.swt
.dnd
.TextTransfer
;
41 import org
.eclipse
.swt
.dnd
.Transfer
;
42 import org
.eclipse
.swt
.events
.SelectionAdapter
;
43 import org
.eclipse
.swt
.events
.SelectionEvent
;
44 import org
.eclipse
.swt
.layout
.GridData
;
45 import org
.eclipse
.swt
.layout
.GridLayout
;
46 import org
.eclipse
.swt
.widgets
.Button
;
47 import org
.eclipse
.swt
.widgets
.Composite
;
48 import org
.eclipse
.swt
.widgets
.Display
;
49 //import org.eclipse.ui.part.ViewPart;
50 import org
.osgi
.framework
.InvalidSyntaxException
;
51 import org
.osgi
.service
.useradmin
.Role
;
52 import org
.osgi
.service
.useradmin
.User
;
53 import org
.osgi
.service
.useradmin
.UserAdminEvent
;
54 import org
.osgi
.service
.useradmin
.UserAdminListener
;
56 /** List all groups with filter */
57 public class GroupsView
{
58 private final static CmsLog log
= CmsLog
.getLog(GroupsView
.class);
59 // public final static String ID = WorkbenchUiPlugin.PLUGIN_ID + ".groupsView";
62 private EPartService partService
;
64 private UserAdminWrapper userAdminWrapper
;
67 private LdifUsersTable groupTableViewerCmp
;
68 private TableViewer userViewer
;
69 private List
<ColumnDefinition
> columnDefs
= new ArrayList
<ColumnDefinition
>();
71 private UserAdminListener listener
;
74 public void createPartControl(Composite parent
, ESelectionService selectionService
) {
75 parent
.setLayout(EclipseUiUtils
.noSpaceGridLayout());
77 // boolean isAdmin = CurrentUser.isInRole(NodeConstants.ROLE_ADMIN);
79 // Define the displayed columns
80 columnDefs
.add(new ColumnDefinition(new RoleIconLP(), "", 19));
81 columnDefs
.add(new ColumnDefinition(new CommonNameLP(), "Name", 150));
82 columnDefs
.add(new ColumnDefinition(new DomainNameLP(), "Domain", 100));
83 // Only show technical DN to admin
85 // columnDefs.add(new ColumnDefinition(new UserNameLP(),
86 // "Distinguished Name", 300));
88 // Create and configure the table
89 groupTableViewerCmp
= new MyUserTableViewer(parent
, SWT
.MULTI
| SWT
.H_SCROLL
| SWT
.V_SCROLL
);
91 groupTableViewerCmp
.setColumnDefinitions(columnDefs
);
93 // groupTableViewerCmp.populateWithStaticFilters(false, false);
95 groupTableViewerCmp
.populate(true, false);
97 groupTableViewerCmp
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
100 userViewer
= groupTableViewerCmp
.getTableViewer();
101 userViewer
.addDoubleClickListener(new UserTableDefaultDClickListener(partService
));
102 // getViewSite().setSelectionProvider(userViewer);
103 userViewer
.addSelectionChangedListener(new ISelectionChangedListener() {
106 public void selectionChanged(SelectionChangedEvent event
) {
107 IStructuredSelection selection
= (IStructuredSelection
) event
.getSelection();
108 selectionService
.setSelection(selection
.toList());
113 groupTableViewerCmp
.refresh();
116 int operations
= DND
.DROP_COPY
| DND
.DROP_MOVE
;
117 Transfer
[] tt
= new Transfer
[] { TextTransfer
.getInstance() };
118 userViewer
.addDragSupport(operations
, tt
, new UserDragListener(userViewer
));
120 // // Register a useradmin listener
121 // listener = new UserAdminListener() {
123 // public void roleChanged(UserAdminEvent event) {
124 // if (userViewer != null && !userViewer.getTable().isDisposed())
128 // userAdminWrapper.addListener(listener);
131 // Register a useradmin listener
132 listener
= new MyUiUAListener(parent
.getDisplay());
133 userAdminWrapper
.addListener(listener
);
136 private class MyUiUAListener
extends UiUserAdminListener
{
137 public MyUiUAListener(Display display
) {
142 public void roleChangedToUiThread(UserAdminEvent event
) {
143 if (userViewer
!= null && !userViewer
.getTable().isDisposed())
148 private class MyUserTableViewer
extends LdifUsersTable
{
149 private static final long serialVersionUID
= 8467999509931900367L;
151 private boolean showSystemRoles
= true;
153 private final String
[] knownProps
= { LdapAttrs
.uid
.name(), LdapAttrs
.cn
.name(), LdapAttrs
.DN
};
155 public MyUserTableViewer(Composite parent
, int style
) {
156 super(parent
, style
);
157 showSystemRoles
= CurrentUser
.isInRole(CmsConstants
.ROLE_ADMIN
);
160 protected void populateStaticFilters(Composite staticFilterCmp
) {
161 staticFilterCmp
.setLayout(new GridLayout());
162 final Button showSystemRoleBtn
= new Button(staticFilterCmp
, SWT
.CHECK
);
163 showSystemRoleBtn
.setText("Show system roles");
164 showSystemRoles
= CurrentUser
.isInRole(CmsConstants
.ROLE_ADMIN
);
165 showSystemRoleBtn
.setSelection(showSystemRoles
);
167 showSystemRoleBtn
.addSelectionListener(new SelectionAdapter() {
168 private static final long serialVersionUID
= -7033424592697691676L;
171 public void widgetSelected(SelectionEvent e
) {
172 showSystemRoles
= showSystemRoleBtn
.getSelection();
180 protected List
<User
> listFilteredElements(String filter
) {
183 StringBuilder builder
= new StringBuilder();
184 StringBuilder tmpBuilder
= new StringBuilder();
185 if (EclipseUiUtils
.notEmpty(filter
))
186 for (String prop
: knownProps
) {
187 tmpBuilder
.append("(");
188 tmpBuilder
.append(prop
);
189 tmpBuilder
.append("=*");
190 tmpBuilder
.append(filter
);
191 tmpBuilder
.append("*)");
193 if (tmpBuilder
.length() > 1) {
194 builder
.append("(&(").append(LdapAttrs
.objectClass
.name()).append("=")
195 .append(LdapObjs
.groupOfNames
.name()).append(")");
197 builder
.append("(!(").append(LdapAttrs
.DN
).append("=*").append(CmsConstants
.TOKENS_BASEDN
)
200 if (!showSystemRoles
)
201 builder
.append("(!(").append(LdapAttrs
.DN
).append("=*").append(CmsConstants
.SYSTEM_ROLES_BASEDN
)
203 builder
.append("(|");
204 builder
.append(tmpBuilder
.toString());
205 builder
.append("))");
207 if (!showSystemRoles
)
208 builder
.append("(&(").append(LdapAttrs
.objectClass
.name()).append("=")
209 .append(LdapObjs
.groupOfNames
.name()).append(")(!(").append(LdapAttrs
.DN
).append("=*")
210 .append(CmsConstants
.SYSTEM_ROLES_BASEDN
).append("))(!(").append(LdapAttrs
.DN
).append("=*")
211 .append(CmsConstants
.TOKENS_BASEDN
).append(")))");
213 builder
.append("(&(").append(LdapAttrs
.objectClass
.name()).append("=")
214 .append(LdapObjs
.groupOfNames
.name()).append(")(!(").append(LdapAttrs
.DN
).append("=*")
215 .append(CmsConstants
.TOKENS_BASEDN
).append(")))");
218 roles
= userAdminWrapper
.getUserAdmin().getRoles(builder
.toString());
219 } catch (InvalidSyntaxException e
) {
220 throw new CmsException("Unable to get roles with filter: " + filter
, e
);
222 List
<User
> users
= new ArrayList
<User
>();
223 for (Role role
: roles
)
224 if (!users
.contains(role
))
225 users
.add((User
) role
);
227 log
.warn("Duplicated role: " + role
);
233 public void refresh() {
234 groupTableViewerCmp
.refresh();
238 public void dispose() {
239 userAdminWrapper
.removeListener(listener
);
243 public void setFocus() {
244 groupTableViewerCmp
.setFocus();
247 /* DEPENDENCY INJECTION */
248 public void setUserAdminWrapper(UserAdminWrapper userAdminWrapper
) {
249 this.userAdminWrapper
= userAdminWrapper
;