]> git.argeo.org Git - lgpl/argeo-commons.git/blobdiff - org.argeo.security.ui.admin/src/org/argeo/security/ui/admin/editors/UserEditor.java
Plug password management
[lgpl/argeo-commons.git] / org.argeo.security.ui.admin / src / org / argeo / security / ui / admin / editors / UserEditor.java
index f2f233fe4f3a3f9a5613bb6d0c0e15dd595d8f78..2f7aa41c7c2672a3562bb909c746eded2507ebbb 100644 (file)
  */
 package org.argeo.security.ui.admin.editors;
 
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.transaction.UserTransaction;
+
 import org.argeo.ArgeoException;
-import org.argeo.security.ui.admin.SecurityAdminImages;
 import org.argeo.security.ui.admin.SecurityAdminPlugin;
+import org.argeo.security.ui.admin.internal.UiAdminUtils;
 import org.argeo.security.ui.admin.internal.UserAdminConstants;
 import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.ui.IEditorInput;
 import org.eclipse.ui.IEditorSite;
 import org.eclipse.ui.PartInitException;
 import org.eclipse.ui.forms.editor.FormEditor;
+import org.osgi.service.useradmin.Authorization;
 import org.osgi.service.useradmin.Role;
 import org.osgi.service.useradmin.User;
 import org.osgi.service.useradmin.UserAdmin;
@@ -37,6 +43,7 @@ public class UserEditor extends FormEditor implements UserAdminConstants {
 
        /* DEPENDENCY INJECTION */
        private UserAdmin userAdmin;
+       private UserTransaction userTransaction;
 
        // Context
        private User user;
@@ -49,16 +56,37 @@ public class UserEditor extends FormEditor implements UserAdminConstants {
                user = (User) userAdmin.getRole(username);
 
                String commonName = getProperty(KEY_CN);
-               // this.setPartProperty("name", commonName != null ? commonName
-               // : "username");
 
-               // if (user.getType() == Role.GROUP) {
-               // this.setPartProperty("icon", "icons/users.gif");
-               // firePartPropertyChanged("icon", "icons/user.gif", "icons/users.gif");
-               // }
                setPartName(commonName != null ? commonName : "username");
-               setTitleImage(user.getType() == Role.GROUP ? SecurityAdminImages.ICON_GROUP
-                               : SecurityAdminImages.ICON_USER);
+
+               // TODO: following has been disabled because it causes NPE after a
+               // login/logout on RAP
+               // Image titleIcon = user.getType() == Role.GROUP ?
+               // SecurityAdminImages.ICON_GROUP
+               // : SecurityAdminImages.ICON_USER;
+               // setTitleImage(titleIcon);
+       }
+
+       /**
+        * returns the list of all authorisation for the given user or of the
+        * current displayed user if parameter is null
+        */
+       protected List<User> getFlatGroups(User aUser) {
+               Authorization currAuth;
+               if (aUser == null)
+                       currAuth = userAdmin.getAuthorization(this.user);
+               else
+                       currAuth = userAdmin.getAuthorization(aUser);
+
+               String[] roles = currAuth.getRoles();
+
+               List<User> groups = new ArrayList<User>();
+               for (String roleStr : roles) {
+                       User currRole = (User) userAdmin.getRole(roleStr);
+                       if (!groups.contains(currRole))
+                               groups.add(currRole);
+               }
+               return groups;
        }
 
        /** Exposes the user (or group) that is displayed by the current editor */
@@ -72,12 +100,10 @@ public class UserEditor extends FormEditor implements UserAdminConstants {
 
        protected void addPages() {
                try {
-
                        if (user.getType() == Role.GROUP)
                                addPage(new GroupMainPage(this, userAdmin));
                        else
                                addPage(new UserMainPage(this, userAdmin));
-
                } catch (Exception e) {
                        throw new ArgeoException("Cannot add pages", e);
                }
@@ -91,7 +117,14 @@ public class UserEditor extends FormEditor implements UserAdminConstants {
                        return "";
        }
 
-       /** The property is directly updated!!! */
+       protected void beginTransactionIfNeeded() {
+               UiAdminUtils.beginTransactionIfNeeded(userTransaction);
+       }
+
+       /**
+        * Updates the property in the working copy. The transaction must be
+        * explicitly committed to persist the update.
+        */
        @SuppressWarnings("unchecked")
        protected void setProperty(String key, String value) {
                user.getProperties().put(key, value);
@@ -99,8 +132,15 @@ public class UserEditor extends FormEditor implements UserAdminConstants {
 
        @Override
        public void doSave(IProgressMonitor monitor) {
+               UiAdminUtils.beginTransactionIfNeeded(userTransaction);
                commitPages(true);
                firePropertyChange(PROP_DIRTY);
+               // FIXME transaction should be managed at a higher level
+               // try {
+               // userTransaction.commit();
+               // } catch (Exception e) {
+               // throw new ArgeoException("Could not save user editor", e);
+               // }
        }
 
        @Override
@@ -125,4 +165,9 @@ public class UserEditor extends FormEditor implements UserAdminConstants {
        public void setUserAdmin(UserAdmin userAdmin) {
                this.userAdmin = userAdmin;
        }
+
+       public void setUserTransaction(UserTransaction userTransaction) {
+               this.userTransaction = userTransaction;
+       }
+
 }
\ No newline at end of file