Factorise People UI
[gpl/argeo-suite.git] / swt / org.argeo.app.ui / src / org / argeo / app / ui / people / UsersPart.java
diff --git a/swt/org.argeo.app.ui/src/org/argeo/app/ui/people/UsersPart.java b/swt/org.argeo.app.ui/src/org/argeo/app/ui/people/UsersPart.java
new file mode 100644 (file)
index 0000000..d82b227
--- /dev/null
@@ -0,0 +1,92 @@
+package org.argeo.app.ui.people;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.argeo.api.acr.Content;
+import org.argeo.api.acr.ContentSession;
+import org.argeo.api.acr.ldap.LdapAttrs;
+import org.argeo.api.acr.ldap.LdapObjs;
+import org.argeo.api.cms.directory.CmsUserManager;
+import org.argeo.api.cms.directory.HierarchyUnit;
+import org.argeo.api.cms.directory.UserDirectory;
+import org.argeo.api.cms.ux.CmsIcon;
+import org.argeo.app.ui.SuiteIcon;
+import org.argeo.cms.acr.ContentUtils;
+import org.argeo.cms.auth.UserAdminUtils;
+import org.argeo.cms.ux.widgets.Column;
+import org.argeo.cms.ux.widgets.DefaultTabularPart;
+import org.osgi.service.useradmin.Role;
+import org.osgi.service.useradmin.User;
+
+public class UsersPart extends DefaultTabularPart<HierarchyUnit, Content> {
+       private ContentSession contentSession;
+       private CmsUserManager cmsUserManager;
+
+       public UsersPart(ContentSession contentSession, CmsUserManager cmsUserManager) {
+               this.contentSession = contentSession;
+               this.cmsUserManager = cmsUserManager;
+               addColumn(new Column<Content>() {
+
+                       @Override
+                       public String getText(Content role) {
+                               if (role.hasContentClass(LdapObjs.inetOrgPerson))
+                                       return UserAdminUtils.getUserDisplayName(role.adapt(User.class));
+                               else if (role.hasContentClass(LdapObjs.organization))
+                                       return role.attr(LdapAttrs.o);
+                               else if (role.hasContentClass(LdapObjs.groupOfNames))
+                                       return role.attr(LdapAttrs.cn);
+                               else
+                                       return null;
+                       }
+
+                       @Override
+                       public CmsIcon getIcon(Content role) {
+                               if (role.hasContentClass(LdapObjs.posixAccount))
+                                       return SuiteIcon.user;
+                               else if (role.hasContentClass(LdapObjs.inetOrgPerson))
+                                       return SuiteIcon.person;
+                               else if (role.hasContentClass(LdapObjs.organization))
+                                       return SuiteIcon.organisationContact;
+                               else if (role.hasContentClass(LdapObjs.groupOfNames))
+                                       return SuiteIcon.group;
+                               else
+                                       return null;
+                       }
+
+                       @Override
+                       public int getWidth() {
+                               return 300;
+                       }
+
+               });
+       }
+
+       @Override
+       protected List<Content> asList(HierarchyUnit hu) {
+               List<Content> roles = new ArrayList<>();
+               UserDirectory ud = (UserDirectory) hu.getDirectory();
+               if (ud.getRealm().isPresent()) {
+                       for (Role r : ud.getHierarchyUnitRoles(ud, null, true)) {
+                               Content content = ContentUtils.roleToContent(cmsUserManager, contentSession, r);
+                               if (content.hasContentClass(LdapObjs.inetOrgPerson, LdapObjs.organization))
+                                       roles.add(content);
+                       }
+
+               } else {
+                       for (HierarchyUnit directChild : hu.getDirectHierarchyUnits(false)) {
+                               if (!(directChild.isType(HierarchyUnit.Type.FUNCTIONAL)
+                                               || directChild.isType(HierarchyUnit.Type.ROLES))) {
+                                       for (Role r : ud.getHierarchyUnitRoles(directChild, null, false)) {
+                                               Content content = ContentUtils.roleToContent(cmsUserManager, contentSession, r);
+                                               if (content.hasContentClass(LdapObjs.inetOrgPerson, LdapObjs.organization,
+                                                               LdapObjs.groupOfNames))
+                                                       roles.add(content);
+                                       }
+                               }
+                       }
+               }
+               return roles;
+       }
+
+}