Improve user profile
authorMathieu Baudier <mbaudier@argeo.org>
Sat, 18 Feb 2012 20:03:50 +0000 (20:03 +0000)
committerMathieu Baudier <mbaudier@argeo.org>
Sat, 18 Feb 2012 20:03:50 +0000 (20:03 +0000)
git-svn-id: https://svn.argeo.org/commons/trunk@5102 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

eclipse/runtime/org.argeo.eclipse.ui/src/main/java/org/argeo/eclipse/ui/EclipseUiUtils.java
security/plugins/org.argeo.security.ui/src/main/java/org/argeo/security/ui/internal/CurrentUser.java
security/plugins/org.argeo.security.ui/src/main/java/org/argeo/security/ui/views/UserProfile.java
security/runtime/org.argeo.security.core/src/main/java/org/argeo/security/jcr/JcrUserDetails.java

index 5c85802c32a48b3518052d4923178aaa3b2b9616..85d514ba9cc5a76dac96d019f6cdbd77b4ec9367 100644 (file)
@@ -23,7 +23,10 @@ public class EclipseUiUtils {
         *            a {@link ModifyListener} to listen on events on the text, can
         *            be null
         * @return the created text
+        * 
+        * @deprecated use {@link #createGridLT(Composite, String)} instead
         */
+       @Deprecated
        public static Text createGridLT(Composite parent, String label,
                        ModifyListener modifyListener) {
                Label lbl = new Label(parent, SWT.LEAD);
@@ -31,11 +34,27 @@ public class EclipseUiUtils {
                lbl.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
                Text txt = new Text(parent, SWT.LEAD | SWT.BORDER);
                txt.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
-               if (txt != null)
+               if (modifyListener != null)
                        txt.addModifyListener(modifyListener);
                return txt;
        }
 
+       public static Text createGridLT(Composite parent, String label) {
+               return createGridLT(parent, label, null);
+       }
+
+       /**
+        * Creates one label and a text field not editable with background color of
+        * the parent (like a label but with selectable text)
+        */
+       public static Text createGridLL(Composite parent, String label, String text) {
+               Text txt = createGridLT(parent, label);
+               txt.setText(text);
+               txt.setEditable(false);
+               txt.setBackground(parent.getBackground());
+               return txt;
+       }
+
        public static Text createGridLP(Composite parent, String label,
                        ModifyListener modifyListener) {
                Label lbl = new Label(parent, SWT.LEAD);
@@ -60,7 +79,8 @@ public class EclipseUiUtils {
 
        public static Font getBoldItalicFont(Composite parent) {
                return JFaceResources.getFontRegistry().defaultFontDescriptor()
-                               .setStyle(SWT.BOLD | SWT.ITALIC).createFont(parent.getDisplay());
+                               .setStyle(SWT.BOLD | SWT.ITALIC)
+                               .createFont(parent.getDisplay());
        }
 
 }
index b26df1018965d797b9beca95a5e28124a2280f56..7b5287b7b0f572d57f444255e38eda4f4f482d4e 100644 (file)
@@ -28,17 +28,22 @@ public class CurrentUser {
 
        public final static Set<String> roles() {
                Set<String> roles = Collections.synchronizedSet(new HashSet<String>());
+               Authentication authentication = getAuthentication();
+               for (GrantedAuthority ga : authentication.getAuthorities()) {
+                       roles.add(ga.getAuthority());
+               }
+               return Collections.unmodifiableSet(roles);
+       }
 
+       public final static Authentication getAuthentication() {
                Set<Authentication> authens = getSubject().getPrincipals(
                                Authentication.class);
                if (authens != null && !authens.isEmpty()) {
                        Principal principal = authens.iterator().next();
                        Authentication authentication = (Authentication) principal;
-                       for (GrantedAuthority ga : authentication.getAuthorities()) {
-                               roles.add(ga.getAuthority());
-                       }
+                       return authentication;
                }
-               return Collections.unmodifiableSet(roles);
+               throw new ArgeoException("No authentication found");
        }
 
        public final static Subject getSubject() {
index afa56948813f58aeded3bac18a2859c904699d74..f28ab57339cacb2337d7498a47889ecb606232d8 100644 (file)
@@ -1,22 +1,67 @@
 package org.argeo.security.ui.views;
 
+import java.util.TreeSet;
+
+import org.argeo.eclipse.ui.EclipseUiUtils;
 import org.argeo.security.ui.SecurityUiPlugin;
 import org.argeo.security.ui.internal.CurrentUser;
+import org.eclipse.jface.viewers.IStructuredContentProvider;
+import org.eclipse.jface.viewers.LabelProvider;
+import org.eclipse.jface.viewers.TableViewer;
+import org.eclipse.jface.viewers.Viewer;
 import org.eclipse.swt.SWT;
+import org.eclipse.swt.layout.GridData;
+import org.eclipse.swt.layout.GridLayout;
 import org.eclipse.swt.widgets.Composite;
-import org.eclipse.swt.widgets.Label;
+import org.eclipse.swt.widgets.Table;
+import org.eclipse.swt.widgets.Text;
 import org.eclipse.ui.part.ViewPart;
+import org.springframework.security.Authentication;
 
+/** Information about the currently logged in user */
 public class UserProfile extends ViewPart {
        public static String ID = SecurityUiPlugin.PLUGIN_ID + ".userProfile";
 
+       private TableViewer viewer;
+
        @Override
        public void createPartControl(Composite parent) {
-               new Label(parent, SWT.NONE).setText(CurrentUser.getUsername());
+               parent.setLayout(new GridLayout(2, false));
+
+               Authentication authentication = CurrentUser.getAuthentication();
+               EclipseUiUtils.createGridLL(parent, "Name", authentication
+                               .getPrincipal().toString());
+               EclipseUiUtils.createGridLL(parent, "User ID",
+                               CurrentUser.getUsername());
+
+               // roles table
+               Table table = new Table(parent, SWT.V_SCROLL | SWT.BORDER);
+               table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
+               table.setLinesVisible(false);
+               table.setHeaderVisible(false);
+               viewer = new TableViewer(table);
+               viewer.setContentProvider(new RolesContentProvider());
+               viewer.setLabelProvider(new LabelProvider());
+               getViewSite().setSelectionProvider(viewer);
+               viewer.setInput(getViewSite());
        }
 
        @Override
        public void setFocus() {
+               viewer.getTable();
+       }
+
+       private class RolesContentProvider implements IStructuredContentProvider {
+               public Object[] getElements(Object inputElement) {
+                       return new TreeSet<String>(CurrentUser.roles()).toArray();
+               }
+
+               public void dispose() {
+               }
+
+               public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
+               }
+
        }
 
 }
index 05ae165e03fbcccd0d63be4679745b787742f567..223c06de0d3dd44b4a108cc39c1b01a14fc495b3 100644 (file)
@@ -4,6 +4,7 @@ import java.util.ArrayList;
 import java.util.List;
 
 import javax.jcr.Node;
+import javax.jcr.Property;
 import javax.jcr.RepositoryException;
 import javax.jcr.Session;
 
@@ -22,6 +23,9 @@ public class JcrUserDetails extends User implements ArgeoNames {
        private final String homePath;
        private final String securityWorkspace;
 
+       /** Human readable user name */
+       private String displayName;
+
        protected JcrUserDetails(String securityWorkspace, String homePath,
                        String username, String password, boolean enabled,
                        boolean accountNonExpired, boolean credentialsNonExpired,
@@ -43,6 +47,15 @@ public class JcrUserDetails extends User implements ArgeoNames {
                                userProfile.getProperty(ARGEO_CREDENTIALS_NON_EXPIRED)
                                                .getBoolean(), userProfile.getProperty(
                                                ARGEO_ACCOUNT_NON_LOCKED).getBoolean(), authorities);
+               // human readable name
+               if (userProfile.hasProperty(Property.JCR_TITLE)) {
+                       displayName = userProfile.getProperty(Property.JCR_TITLE)
+                                       .getString();
+                       if (displayName.trim().equals(""))
+                               displayName = null;
+               }
+               if (displayName == null)
+                       displayName = userProfile.getProperty(ARGEO_USER_ID).getString();
                // home is defined as the parent of the profile
                homePath = userProfile.getParent().getPath();
                securityWorkspace = userProfile.getSession().getWorkspace().getName();
@@ -109,4 +122,15 @@ public class JcrUserDetails extends User implements ArgeoNames {
        public String getSecurityWorkspace() {
                return securityWorkspace;
        }
+
+       /** The human readable name of this user */
+       public String getDisplayName() {
+               return displayName;
+       }
+
+       @Override
+       public String toString() {
+               return getDisplayName();
+       }
+
 }