Clean user management
authorBruno Sinou <bsinou@argeo.org>
Wed, 26 Oct 2016 15:29:30 +0000 (15:29 +0000)
committerBruno Sinou <bsinou@argeo.org>
Wed, 26 Oct 2016 15:29:30 +0000 (15:29 +0000)
git-svn-id: https://svn.argeo.org/commons/trunk@9290 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

org.argeo.cms.ui.workbench/src/org/argeo/cms/ui/workbench/internal/jcr/parts/AddPrivilegeWizard.java
org.argeo.cms.ui.workbench/src/org/argeo/cms/ui/workbench/internal/useradmin/UsersUtils.java [deleted file]
org.argeo.cms.ui.workbench/src/org/argeo/cms/ui/workbench/useradmin/UserLP.java

index bbd23ca7e8f3c3d18683c8588bc4ff5bbe429161..1b39812a89f84932345497249623b68a6c828d08 100644 (file)
@@ -23,8 +23,8 @@ import javax.jcr.RepositoryException;
 import javax.jcr.Session;
 import javax.jcr.security.Privilege;
 
-import org.argeo.cms.ui.workbench.internal.useradmin.UsersUtils;
 import org.argeo.cms.ui.workbench.useradmin.PickUpUserDialog;
+import org.argeo.cms.util.UserAdminUtils;
 import org.argeo.eclipse.ui.EclipseUiException;
 import org.argeo.eclipse.ui.EclipseUiUtils;
 import org.argeo.jcr.JcrUtils;
@@ -77,19 +77,15 @@ public class AddPrivilegeWizard extends Wizard {
        protected static final Map<String, String> AUTH_TYPE_DESC;
        static {
                Map<String, String> tmpMap = new HashMap<String, String>();
-               tmpMap.put(Privilege.JCR_READ,
-                               "The privilege to retrieve a node and get its properties and their values.");
+               tmpMap.put(Privilege.JCR_READ, "The privilege to retrieve a node and get its properties and their values.");
                tmpMap.put(Privilege.JCR_WRITE, "An aggregate privilege that "
-                               + "contains: jcr:modifyProperties, jcr:addChildNodes, "
-                               + "jcr:removeNode, jcr:removeChildNodes");
-               tmpMap.put(Privilege.JCR_ALL, "An aggregate privilege that "
-                               + "contains all JCR predefined privileges, "
+                               + "contains: jcr:modifyProperties, jcr:addChildNodes, " + "jcr:removeNode, jcr:removeChildNodes");
+               tmpMap.put(Privilege.JCR_ALL, "An aggregate privilege that " + "contains all JCR predefined privileges, "
                                + "plus all implementation-defined privileges. ");
                AUTH_TYPE_DESC = Collections.unmodifiableMap(tmpMap);
        }
 
-       public AddPrivilegeWizard(Session currentSession, String path,
-                       UserAdmin userAdmin) {
+       public AddPrivilegeWizard(Session currentSession, String path, UserAdmin userAdmin) {
                super();
                this.userAdmin = userAdmin;
                this.currentSession = currentSession;
@@ -112,17 +108,15 @@ public class AddPrivilegeWizard extends Wizard {
                if (!canFinish())
                        return false;
                try {
-                       JcrUtils.addPrivilege(currentSession, targetPath,
-                                       chosenUser.getName(), jcrPrivilege);
+                       JcrUtils.addPrivilege(currentSession, targetPath, chosenUser.getName(), jcrPrivilege);
                } catch (RepositoryException re) {
-                       throw new EclipseUiException("Cannot set " + jcrPrivilege + " for "
-                                       + chosenUser.getName() + " on " + targetPath, re);
+                       throw new EclipseUiException(
+                                       "Cannot set " + jcrPrivilege + " for " + chosenUser.getName() + " on " + targetPath, re);
                }
                return true;
        }
 
-       private class DefinePrivilegePage extends WizardPage implements
-                       ModifyListener {
+       private class DefinePrivilegePage extends WizardPage implements ModifyListener {
                private static final long serialVersionUID = 8084431378762283920L;
 
                // Context
@@ -156,12 +150,10 @@ public class AddPrivilegeWizard extends Wizard {
 
                                @Override
                                public void widgetSelected(SelectionEvent e) {
-                                       PickUpUserDialog dialog = new PickUpUserDialog(getShell(),
-                                                       "Choose a group or a user", userAdmin);
+                                       PickUpUserDialog dialog = new PickUpUserDialog(getShell(), "Choose a group or a user", userAdmin);
                                        if (dialog.open() == Window.OK) {
                                                chosenUser = dialog.getSelected();
-                                               groupNameLbl.setText(UsersUtils
-                                                               .getCommonName(chosenUser));
+                                               groupNameLbl.setText(UserAdminUtils.getCommonName(chosenUser));
                                                groupNameTxt.setText(chosenUser.getName());
                                        }
                                }
@@ -180,10 +172,8 @@ public class AddPrivilegeWizard extends Wizard {
                                        try {
                                                newChosen = (User) userAdmin.getRole(dn);
                                        } catch (Exception e) {
-                                               boolean tryAgain = MessageDialog.openQuestion(
-                                                               getShell(), "Unvalid DN",
-                                                               "DN " + dn + " is not valid.\nError message: "
-                                                                               + e.getMessage()
+                                               boolean tryAgain = MessageDialog.openQuestion(getShell(), "Unvalid DN",
+                                                               "DN " + dn + " is not valid.\nError message: " + e.getMessage()
                                                                                + "\n\t\tDo you want to try again?");
                                                if (tryAgain)
                                                        groupNameTxt.setFocus();
@@ -192,24 +182,20 @@ public class AddPrivilegeWizard extends Wizard {
                                        }
 
                                        if (userAdmin.getRole(dn) == null) {
-                                               boolean tryAgain = MessageDialog.openQuestion(
-                                                               getShell(), "Unexisting role", "User/group "
-                                                                               + dn + " does not exist. "
-                                                                               + "Do you want to try again?");
+                                               boolean tryAgain = MessageDialog.openQuestion(getShell(), "Unexisting role",
+                                                               "User/group " + dn + " does not exist. " + "Do you want to try again?");
                                                if (tryAgain)
                                                        groupNameTxt.setFocus();
                                                else
                                                        resetOnFail();
                                        } else {
                                                chosenUser = newChosen;
-                                               groupNameLbl.setText(UsersUtils
-                                                               .getCommonName(chosenUser));
+                                               groupNameLbl.setText(UserAdminUtils.getCommonName(chosenUser));
                                        }
                                }
 
                                private void resetOnFail() {
-                                       String oldDn = chosenUser == null ? "" : chosenUser
-                                                       .getName();
+                                       String oldDn = chosenUser == null ? "" : chosenUser.getName();
                                        groupNameTxt.setText(oldDn);
                                }
 
@@ -220,10 +206,8 @@ public class AddPrivilegeWizard extends Wizard {
 
                        // JCR Privileges
                        createBoldLabel(composite, "Privilege type");
-                       Combo authorizationCmb = new Combo(composite, SWT.BORDER
-                                       | SWT.READ_ONLY | SWT.V_SCROLL);
-                       authorizationCmb.setItems(AUTH_TYPE_LABELS.values().toArray(
-                                       new String[0]));
+                       Combo authorizationCmb = new Combo(composite, SWT.BORDER | SWT.READ_ONLY | SWT.V_SCROLL);
+                       authorizationCmb.setItems(AUTH_TYPE_LABELS.values().toArray(new String[0]));
                        authorizationCmb.setLayoutData(EclipseUiUtils.fillWidth(2));
                        createBoldLabel(composite, ""); // empty cell
                        final Label descLbl = new Label(composite, SWT.WRAP);
diff --git a/org.argeo.cms.ui.workbench/src/org/argeo/cms/ui/workbench/internal/useradmin/UsersUtils.java b/org.argeo.cms.ui.workbench/src/org/argeo/cms/ui/workbench/internal/useradmin/UsersUtils.java
deleted file mode 100644 (file)
index 55db40e..0000000
+++ /dev/null
@@ -1,93 +0,0 @@
-package org.argeo.cms.ui.workbench.internal.useradmin;
-
-import java.security.AccessController;
-import java.util.List;
-
-import javax.naming.InvalidNameException;
-import javax.naming.ldap.LdapName;
-import javax.naming.ldap.Rdn;
-import javax.security.auth.Subject;
-import javax.security.auth.x500.X500Principal;
-
-import org.argeo.eclipse.ui.EclipseUiException;
-import org.argeo.naming.LdapAttrs;
-import org.argeo.node.NodeConstants;
-import org.osgi.service.useradmin.Role;
-import org.osgi.service.useradmin.User;
-
-/**
- * Utility methods to manage user concepts in the ui.workbench bundle 
- * 
- * FIXME refactor amd centralise userAdminUtils
- */
-@Deprecated
-public class UsersUtils {
-
-       public final static boolean isCurrentUser(User user) {
-               String userName = getProperty(user, LdapAttrs.DN);
-               try {
-                       LdapName selfUserName = getLdapName();
-                       LdapName userLdapName = new LdapName(userName);
-                       if (userLdapName.equals(selfUserName))
-                               return true;
-                       else
-                               return false;
-               } catch (InvalidNameException e) {
-                       throw new EclipseUiException("User " + user
-                                       + " has an unvalid dn: " + userName, e);
-               }
-       }
-
-       public final static LdapName getLdapName() {
-               Subject subject = Subject.getSubject(AccessController.getContext());
-               String name = subject.getPrincipals(X500Principal.class).iterator()
-                               .next().toString();
-               LdapName dn;
-               try {
-                       dn = new LdapName(name);
-               } catch (InvalidNameException e) {
-                       throw new EclipseUiException("Invalid user dn " + name, e);
-               }
-               return dn;
-       }
-
-       public final static String getCommonName(User user) {
-               return getProperty(user, LdapAttrs.cn.name());
-       }
-
-       /** Simply retrieves a display name of the relevant domain */
-       public final static String getDomainName(User user) {
-               String dn = (String) user.getProperties().get(LdapAttrs.DN);
-               if (dn.endsWith(NodeConstants.ROLES_BASEDN))
-                       return "System roles";
-               try {
-                       LdapName name;
-                       name = new LdapName(dn);
-                       List<Rdn> rdns = name.getRdns();
-                       String dname = null;
-                       int i = 0;
-                       loop: while (i < rdns.size()) {
-                               Rdn currrRdn = rdns.get(i);
-                               if (!"dc".equals(currrRdn.getType()))
-                                       break loop;
-                               else {
-                                       String currVal = (String) currrRdn.getValue();
-                                       dname = dname == null ? currVal : currVal + "." + dname;
-                               }
-                               i++;
-                       }
-                       return dname;
-               } catch (InvalidNameException e) {
-                       throw new EclipseUiException("Unable to get domain name for " + dn,
-                                       e);
-               }
-       }
-
-       public final static String getProperty(Role role, String key) {
-               Object obj = role.getProperties().get(key);
-               if (obj != null)
-                       return (String) obj;
-               else
-                       return "";
-       }
-}
index 861c836a696fa5b132b20e367e2998e6437ccf00..e782e370e5fde92b65302eb22eef94f90d2ecebc 100644 (file)
@@ -1,11 +1,7 @@
 package org.argeo.cms.ui.workbench.useradmin;
 
-import javax.naming.InvalidNameException;
-import javax.naming.ldap.LdapName;
-
 import org.argeo.cms.ui.workbench.internal.useradmin.UsersImages;
-import org.argeo.cms.ui.workbench.internal.useradmin.UsersUtils;
-import org.argeo.eclipse.ui.EclipseUiException;
+import org.argeo.cms.util.UserAdminUtils;
 import org.argeo.node.NodeConstants;
 import org.eclipse.jface.resource.JFaceResources;
 import org.eclipse.jface.viewers.ColumnLabelProvider;
@@ -36,30 +32,14 @@ class UserLP extends ColumnLabelProvider {
 
        @Override
        public Font getFont(Object element) {
-               // Self as bold
-               try {
-                       // LdapName selfUserName = new LdapName(CurrentUser.getUsername());
-                       LdapName selfUserName = UsersUtils.getLdapName();
-                       String userName = ((User) element).getName();
-                       LdapName userLdapName = new LdapName(userName);
-                       if (userLdapName.equals(selfUserName)) {
-                               if (bold == null)
-                                       bold = JFaceResources.getFontRegistry()
-                                                       .defaultFontDescriptor().setStyle(SWT.BOLD)
-                                                       .createFont(Display.getCurrent());
-                               return bold;
-                       }
-               } catch (InvalidNameException e) {
-                       throw new EclipseUiException("cannot parse dn for " + element, e);
+               // Current user as bold
+               if (UserAdminUtils.isCurrentUser(((User) element))) {
+                       if (bold == null)
+                               bold = JFaceResources.getFontRegistry().defaultFontDescriptor().setStyle(SWT.BOLD)
+                                               .createFont(Display.getCurrent());
+                       return bold;
                }
-
-               // Disabled as Italic
-               // Node userProfile = (Node) elem;
-               // if (!userProfile.getProperty(ARGEO_ENABLED).getBoolean())
-               // return italic;
-
                return null;
-               // return super.getFont(element);
        }
 
        @Override
@@ -88,9 +68,9 @@ class UserLP extends ColumnLabelProvider {
                if (COL_DN.equals(currType))
                        return user.getName();
                else if (COL_DISPLAY_NAME.equals(currType))
-                       return UsersUtils.getCommonName(user);
+                       return UserAdminUtils.getCommonName(user);
                else if (COL_DOMAIN.equals(currType))
-                       return UsersUtils.getDomainName(user);
+                       return UserAdminUtils.getDomainName(user);
                else
                        return "";
        }