Refactor Argeo APIs
[gpl/argeo-suite.git] / swt / org.argeo.app.ui / src / org / argeo / app / ui / people / PersonUiProvider.java
index 5ff00a7d124e53a5420d9edfe3f46f58d0151efc..a7be1ef2325175f27ef704e7d4e87cf47d08eec1 100644 (file)
@@ -6,20 +6,23 @@ 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.acr.ldap.LdapAttr;
+import org.argeo.api.acr.ldap.LdapObj;
+import org.argeo.api.app.SuiteRole;
+import org.argeo.api.cms.auth.RoleNameUtils;
+import org.argeo.api.cms.auth.SystemRole;
+import org.argeo.api.cms.directory.CmsGroup;
 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.api.cms.directory.HierarchyUnit;
+import org.argeo.api.cms.directory.HierarchyUnit.Type;
+import org.argeo.app.swt.ux.SuiteSwtUtils;
+import org.argeo.app.ux.SuiteMsg;
+import org.argeo.app.ux.SuiteStyle;
 import org.argeo.cms.CmsMsg;
 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.CmsSystemRole;
 import org.argeo.cms.swt.CmsSwtUtils;
 import org.argeo.cms.swt.Selected;
 import org.argeo.cms.swt.acr.SwtSection;
@@ -53,25 +56,29 @@ public class PersonUiProvider implements SwtUiProvider {
                CmsUser user = context.adapt(CmsUser.class);
 
                Content hierarchyUnitContent = context.getParent().getParent();
+               HierarchyUnit hierarchyUnit = hierarchyUnitContent.adapt(HierarchyUnit.class);
 
                String roleContext = RoleNameUtils.getContext(user.getName());
 
-               if (context.hasContentClass(LdapObjs.person.qName())) {
+               if (context.hasContentClass(LdapObj.person.qName())) {
 
-                       addFormLine(main, SuiteMsg.firstName, context, LdapAttrs.givenName);
-                       addFormLine(main, SuiteMsg.lastName, context, LdapAttrs.sn);
-                       addFormLine(main, SuiteMsg.email, context, LdapAttrs.mail);
+                       addFormLine(main, SuiteMsg.firstName, context, LdapAttr.givenName);
+                       addFormLine(main, SuiteMsg.lastName, context, LdapAttr.sn);
+                       addFormLine(main, SuiteMsg.email, context, LdapAttr.mail);
                }
 
-               if (context.hasContentClass(LdapObjs.posixAccount.qName())) {
-                       if (hierarchyUnitContent.hasContentClass(LdapObjs.organization)) {
+               if (context.hasContentClass(LdapObj.posixAccount.qName())) {
+                       if (hierarchyUnitContent.hasContentClass(LdapObj.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);
+                               addRoleCheckBox(rolesSection, hierarchyUnit, user, SuiteMsg.coworkerRole, SuiteRole.coworker,
+                                               roleContext, roles);
+                               addRoleCheckBox(rolesSection, hierarchyUnit, user, SuiteMsg.publisherRole, SuiteRole.publisher,
+                                               roleContext, roles);
+                               addRoleCheckBox(rolesSection, hierarchyUnit, user, SuiteMsg.userAdminRole, CmsSystemRole.userAdmin,
+                                               roleContext, roles);
                        }
 //                     Composite facetsSection = new Composite(main, SWT.NONE);
 //                     facetsSection.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
@@ -82,18 +89,18 @@ public class PersonUiProvider implements SwtUiProvider {
 //                                     new Label(facetsSection, SWT.NONE).setText(member);
 //                             }
 //                     }
-                       if (CurrentUser.implies(CmsRole.userAdmin, roleContext)) {
+                       if (CurrentUser.implies(CmsSystemRole.userAdmin, roleContext)) {
                                SwtSection changePasswordSection = new SwtSection(main, SWT.BORDER);
                                changePasswordSection.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1));
                                changePasswordSection.setLayout(new GridLayout(2, false));
 //                             SuiteUiUtils.addFormLabel(changePasswordSection, CmsMsg.changePassword)
 //                                             .setLayoutData(new GridData(SWT.LEAD, SWT.CENTER, false, false, 2, 1));
-                               SuiteUiUtils.addFormLabel(changePasswordSection, CmsMsg.newPassword);
-                               Text newPasswordT = SuiteUiUtils.addFormTextField(changePasswordSection, null, null,
+                               SuiteSwtUtils.addFormLabel(changePasswordSection, CmsMsg.newPassword);
+                               Text newPasswordT = SuiteSwtUtils.addFormTextField(changePasswordSection, null, null,
                                                SWT.PASSWORD | SWT.BORDER);
                                newPasswordT.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
-                               SuiteUiUtils.addFormLabel(changePasswordSection, CmsMsg.repeatNewPassword);
-                               Text repeatNewPasswordT = SuiteUiUtils.addFormTextField(changePasswordSection, null, null,
+                               SuiteSwtUtils.addFormLabel(changePasswordSection, CmsMsg.repeatNewPassword);
+                               Text repeatNewPasswordT = SuiteSwtUtils.addFormTextField(changePasswordSection, null, null,
                                                SWT.PASSWORD | SWT.BORDER);
                                repeatNewPasswordT.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
                                Button apply = new Button(changePasswordSection, SWT.FLAT);
@@ -120,7 +127,7 @@ public class PersonUiProvider implements SwtUiProvider {
        }
 
        private void addFormLine(SwtSection parent, Localized msg, Content content, QNamed attr) {
-               SuiteUiUtils.addFormLabel(parent, msg.lead());
+               SuiteSwtUtils.addFormLabel(parent, msg.lead());
                EditableText text = new EditableText(parent, SWT.SINGLE | SWT.FLAT);
                text.setLayoutData(CmsSwtUtils.fillWidth());
                text.setStyle(SuiteStyle.simpleInput);
@@ -148,6 +155,7 @@ public class PersonUiProvider implements SwtUiProvider {
                                        @Override
                                        public void widgetDefaultSelected(SelectionEvent e) {
                                                String editedTxt = text.getText();
+                                               content.put(attr, editedTxt);
                                                text.stopEditing();
                                                text.setText(editedTxt);
                                                text.getParent().layout(new Control[] { text.getControl() });
@@ -158,8 +166,8 @@ public class PersonUiProvider implements SwtUiProvider {
                });
        }
 
-       private void addRoleCheckBox(SwtSection parent, Localized msg, SystemRole systemRole, String roleContext,
-                       List<String> roles) {
+       private void addRoleCheckBox(SwtSection parent, HierarchyUnit hierarchyUnit, CmsUser user, Localized msg,
+                       SystemRole systemRole, String roleContext, List<String> roles) {
                Button radio = new Button(parent, SWT.CHECK);
                radio.setSelection(false);
                roles: for (String dn : roles) {
@@ -169,16 +177,26 @@ public class PersonUiProvider implements SwtUiProvider {
                        }
                }
 
-               if (systemRole.equals(CmsRole.userAdmin)) {
-                       if (!CurrentUser.isUserContext(roleContext) && CurrentUser.implies(CmsRole.userAdmin, roleContext)) {
+               if (systemRole.equals(CmsSystemRole.userAdmin)) {
+                       if (!CurrentUser.isUserContext(roleContext) && CurrentUser.implies(CmsSystemRole.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));
+                       radio.setEnabled(CurrentUser.implies(CmsSystemRole.userAdmin, roleContext));
                }
+
+               radio.addSelectionListener((Selected) (e) -> {
+                       HierarchyUnit rolesHu = hierarchyUnit.getDirectChild(Type.ROLES);
+                       CmsGroup roleGroup = cmsUserManager.getOrCreateSystemRole(rolesHu, systemRole.qName());
+                       if (radio.getSelection())
+                               cmsUserManager.addMember(roleGroup, user);
+                       else
+                               cmsUserManager.removeMember(roleGroup, user);
+               });
+
                new Label(parent, 0).setText(msg.lead());
 
        }