]> git.argeo.org Git - lgpl/argeo-commons.git/blobdiff - org.argeo.security.ui.admin/src/org/argeo/security/ui/admin/internal/UiAdminUtils.java
Introduce a UserAdmin wrapper service
[lgpl/argeo-commons.git] / org.argeo.security.ui.admin / src / org / argeo / security / ui / admin / internal / UiAdminUtils.java
index 5430b34d90710432e4000511119e7a88e977ed32..136c41540957913db11dc7666be5deea1d489163 100644 (file)
@@ -1,25 +1,63 @@
 package org.argeo.security.ui.admin.internal;
 
 import java.security.AccessController;
-import java.security.Principal;
 
+import javax.naming.InvalidNameException;
+import javax.naming.ldap.LdapName;
 import javax.security.auth.Subject;
 import javax.security.auth.x500.X500Principal;
 import javax.transaction.Status;
 import javax.transaction.UserTransaction;
 
 import org.argeo.ArgeoException;
+import org.eclipse.ui.IWorkbenchWindow;
+import org.eclipse.ui.PlatformUI;
+import org.eclipse.ui.services.ISourceProviderService;
 import org.osgi.service.useradmin.Role;
 import org.osgi.service.useradmin.User;
+import org.osgi.service.useradmin.UserAdmin;
 
 /** First effort to centralize back end methods used by the user admin UI */
 public class UiAdminUtils {
-       public final static String getUsername() {
+
+       /** returns the local name of the current connected user */
+       public final static String getUsername(UserAdmin userAdmin) {
+               LdapName dn = getLdapName();
+               return getUsername(getUser(userAdmin, dn));
+       }
+
+       public final static boolean isCurrentUser(User user) {
+               String userName = UiAdminUtils.getProperty(user,
+                               UserAdminConstants.KEY_DN);
+               try {
+                       LdapName selfUserName = UiAdminUtils.getLdapName();
+                       LdapName userLdapName = new LdapName(userName);
+                       if (userLdapName.equals(selfUserName))
+                               return true;
+                       else
+                               return false;
+               } catch (InvalidNameException e) {
+                       throw new ArgeoException("User " + user + " has an unvalid dn: "
+                                       + userName, e);
+               }
+       }
+
+       public final static LdapName getLdapName() {
                Subject subject = Subject.getSubject(AccessController.getContext());
-               Principal principal = subject.getPrincipals(X500Principal.class)
-                               .iterator().next();
-               return principal.getName();
+               String name = subject.getPrincipals(X500Principal.class).iterator()
+                               .next().toString();
+               LdapName dn;
+               try {
+                       dn = new LdapName(name);
+               } catch (InvalidNameException e) {
+                       throw new ArgeoException("Invalid user dn " + name, e);
+               }
+               return dn;
+       }
 
+       public final static User getUser(UserAdmin userAdmin, LdapName dn) {
+               User user = userAdmin.getUser(UserAdminConstants.KEY_DN, dn.toString());
+               return user;
        }
 
        public final static String getUsername(User user) {
@@ -41,16 +79,6 @@ public class UiAdminUtils {
                return (firstName.trim() + " " + lastName.trim() + " ").trim();
        }
 
-       public final static void beginTransactionIfNeeded(
-                       UserTransaction userTransaction) {
-               try {
-                       if (userTransaction.getStatus() == Status.STATUS_NO_TRANSACTION)
-                               userTransaction.begin();
-               } catch (Exception e) {
-                       throw new ArgeoException("Unable to begin transaction", e);
-               }
-       }
-
        /*
         * INTERNAL METHODS: Below methods are meant to stay here and are not part
         * of a potential generic backend to manage the useradmin
@@ -69,4 +97,32 @@ public class UiAdminUtils {
                        return "".equals(string.trim());
        }
 
+       /** Must be called from the UI Thread. */
+       public final static void beginTransactionIfNeeded(
+                       UserTransaction userTransaction) {
+               try {
+                       if (userTransaction.getStatus() == Status.STATUS_NO_TRANSACTION) {
+                               userTransaction.begin();
+                               notifyTransactionStateChange(userTransaction);
+                       }
+               } catch (Exception e) {
+                       throw new ArgeoException("Unable to begin transaction", e);
+               }
+       }
+
+       /** Easily notify the ActiveWindow that the transaction had a state change */
+       public final static void notifyTransactionStateChange(
+                       UserTransaction userTransaction) {
+               try {
+                       IWorkbenchWindow aww = PlatformUI.getWorkbench()
+                                       .getActiveWorkbenchWindow();
+                       ISourceProviderService sourceProviderService = (ISourceProviderService) aww
+                                       .getService(ISourceProviderService.class);
+                       UserTransactionProvider esp = (UserTransactionProvider) sourceProviderService
+                                       .getSourceProvider(UserTransactionProvider.TRANSACTION_STATE);
+                       esp.fireTransactionStateChange();
+               } catch (Exception e) {
+                       throw new ArgeoException("Unable to begin transaction", e);
+               }
+       }
 }
\ No newline at end of file