Factorise People UI
[gpl/argeo-suite.git] / swt / org.argeo.app.ui / src / org / argeo / app / ui / people / PersonUiProvider.java
index e296315ea9abf9b51e3dd2af6a3808e1aec82879..5ff00a7d124e53a5420d9edfe3f46f58d0151efc 100644 (file)
@@ -5,19 +5,21 @@ import java.util.List;
 import java.util.Map;
 
 import org.argeo.api.acr.Content;
+import org.argeo.api.acr.QNamed;
 import org.argeo.api.acr.ldap.LdapAttrs;
 import org.argeo.api.acr.ldap.LdapObjs;
+import org.argeo.api.cms.directory.CmsUser;
+import org.argeo.api.cms.directory.CmsUserManager;
 import org.argeo.app.api.SuiteRole;
 import org.argeo.app.ui.SuiteMsg;
 import org.argeo.app.ui.SuiteStyle;
 import org.argeo.app.ui.SuiteUiUtils;
 import org.argeo.cms.CmsMsg;
-import org.argeo.cms.CmsUserManager;
+import org.argeo.cms.CurrentUser;
 import org.argeo.cms.Localized;
+import org.argeo.cms.RoleNameUtils;
+import org.argeo.cms.SystemRole;
 import org.argeo.cms.auth.CmsRole;
-import org.argeo.cms.auth.CurrentUser;
-import org.argeo.cms.auth.RoleNameUtils;
-import org.argeo.cms.auth.SystemRole;
 import org.argeo.cms.swt.CmsSwtUtils;
 import org.argeo.cms.swt.Selected;
 import org.argeo.cms.swt.acr.SwtSection;
@@ -34,8 +36,8 @@ import org.eclipse.swt.layout.GridLayout;
 import org.eclipse.swt.widgets.Button;
 import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.Control;
+import org.eclipse.swt.widgets.Label;
 import org.eclipse.swt.widgets.Text;
-import org.osgi.service.useradmin.User;
 
 /** Edit a suite user. */
 public class PersonUiProvider implements SwtUiProvider {
@@ -48,7 +50,9 @@ public class PersonUiProvider implements SwtUiProvider {
 
                main.setLayout(new GridLayout(2, false));
 
-               User user = context.adapt(User.class);
+               CmsUser user = context.adapt(CmsUser.class);
+
+               Content hierarchyUnitContent = context.getParent().getParent();
 
                String roleContext = RoleNameUtils.getContext(user.getName());
 
@@ -60,15 +64,15 @@ public class PersonUiProvider implements SwtUiProvider {
                }
 
                if (context.hasContentClass(LdapObjs.posixAccount.qName())) {
-
-                       SwtSection rolesSection = new SwtSection(main, SWT.NONE);
-                       rolesSection.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1));
-                       rolesSection.setLayout(new GridLayout());
-                       List<String> roles = Arrays.asList(cmsUserManager.getUserRoles(user.getName()));
-                       addRoleCheckBox(rolesSection, SuiteMsg.coworkerRole, SuiteRole.coworker, roleContext, roles);
-                       addRoleCheckBox(rolesSection, SuiteMsg.publisherRole, SuiteRole.publisher, roleContext, roles);
-                       addRoleCheckBox(rolesSection, SuiteMsg.userAdminRole, CmsRole.userAdmin, roleContext, roles);
-
+                       if (hierarchyUnitContent.hasContentClass(LdapObjs.organization)) {
+                               SwtSection rolesSection = new SwtSection(main, SWT.NONE);
+                               rolesSection.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1));
+                               rolesSection.setLayout(new GridLayout(2, false));
+                               List<String> roles = Arrays.asList(cmsUserManager.getUserRoles(user.getName()));
+                               addRoleCheckBox(rolesSection, SuiteMsg.coworkerRole, SuiteRole.coworker, roleContext, roles);
+                               addRoleCheckBox(rolesSection, SuiteMsg.publisherRole, SuiteRole.publisher, roleContext, roles);
+                               addRoleCheckBox(rolesSection, SuiteMsg.userAdminRole, CmsRole.userAdmin, roleContext, roles);
+                       }
 //                     Composite facetsSection = new Composite(main, SWT.NONE);
 //                     facetsSection.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
 //                     facetsSection.setLayout(new GridLayout());
@@ -115,17 +119,19 @@ public class PersonUiProvider implements SwtUiProvider {
                return main;
        }
 
-       private void addFormLine(SwtSection parent, Localized msg, Content context, LdapAttrs attr) {
+       private void addFormLine(SwtSection parent, Localized msg, Content content, QNamed attr) {
                SuiteUiUtils.addFormLabel(parent, msg.lead());
                EditableText text = new EditableText(parent, SWT.SINGLE | SWT.FLAT);
                text.setLayoutData(CmsSwtUtils.fillWidth());
                text.setStyle(SuiteStyle.simpleInput);
-               String txt = context.attr(attr.qName());
+               String txt = content.attr(attr);
                if (txt == null) // FIXME understand why email is not found in IPA
                        txt = "";
                text.setText(txt);
                text.setMouseListener(new MouseAdapter() {
 
+                       private static final long serialVersionUID = 1L;
+
                        @Override
                        public void mouseDoubleClick(MouseEvent e) {
                                String currentTxt = text.getText();
@@ -133,6 +139,8 @@ public class PersonUiProvider implements SwtUiProvider {
                                text.setText(currentTxt);
                                ((Text) text.getControl()).addSelectionListener(new SelectionListener() {
 
+                                       private static final long serialVersionUID = 1L;
+
                                        @Override
                                        public void widgetSelected(SelectionEvent e) {
                                        }
@@ -153,7 +161,6 @@ public class PersonUiProvider implements SwtUiProvider {
        private void addRoleCheckBox(SwtSection parent, Localized msg, SystemRole systemRole, String roleContext,
                        List<String> roles) {
                Button radio = new Button(parent, SWT.CHECK);
-               radio.setText(msg.lead());
                radio.setSelection(false);
                roles: for (String dn : roles) {
                        if (systemRole.implied(dn, roleContext)) {
@@ -161,10 +168,18 @@ public class PersonUiProvider implements SwtUiProvider {
                                break roles;
                        }
                }
-               if (CurrentUser.implies(CmsRole.userAdmin, roleContext))
-                       radio.setEnabled(true);
-               else
-                       radio.setEnabled(false);
+
+               if (systemRole.equals(CmsRole.userAdmin)) {
+                       if (!CurrentUser.isUserContext(roleContext) && CurrentUser.implies(CmsRole.userAdmin, roleContext)) {
+                               // a user admin cannot modify the user admins of their own context
+                               radio.setEnabled(true);
+                       } else {
+                               radio.setEnabled(false);
+                       }
+               } else {
+                       radio.setEnabled(CurrentUser.implies(CmsRole.userAdmin, roleContext));
+               }
+               new Label(parent, 0).setText(msg.lead());
 
        }