From: Mathieu Baudier Date: Sat, 18 Feb 2012 20:03:50 +0000 (+0000) Subject: Improve user profile X-Git-Tag: argeo-commons-2.1.30~1011 X-Git-Url: https://git.argeo.org/?a=commitdiff_plain;h=c76283038c867fc0ef9eb6fa623040c58c6139bb;p=lgpl%2Fargeo-commons.git Improve user profile git-svn-id: https://svn.argeo.org/commons/trunk@5102 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc --- diff --git a/eclipse/runtime/org.argeo.eclipse.ui/src/main/java/org/argeo/eclipse/ui/EclipseUiUtils.java b/eclipse/runtime/org.argeo.eclipse.ui/src/main/java/org/argeo/eclipse/ui/EclipseUiUtils.java index 5c85802c3..85d514ba9 100644 --- a/eclipse/runtime/org.argeo.eclipse.ui/src/main/java/org/argeo/eclipse/ui/EclipseUiUtils.java +++ b/eclipse/runtime/org.argeo.eclipse.ui/src/main/java/org/argeo/eclipse/ui/EclipseUiUtils.java @@ -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()); } } diff --git a/security/plugins/org.argeo.security.ui/src/main/java/org/argeo/security/ui/internal/CurrentUser.java b/security/plugins/org.argeo.security.ui/src/main/java/org/argeo/security/ui/internal/CurrentUser.java index b26df1018..7b5287b7b 100644 --- a/security/plugins/org.argeo.security.ui/src/main/java/org/argeo/security/ui/internal/CurrentUser.java +++ b/security/plugins/org.argeo.security.ui/src/main/java/org/argeo/security/ui/internal/CurrentUser.java @@ -28,17 +28,22 @@ public class CurrentUser { public final static Set roles() { Set roles = Collections.synchronizedSet(new HashSet()); + Authentication authentication = getAuthentication(); + for (GrantedAuthority ga : authentication.getAuthorities()) { + roles.add(ga.getAuthority()); + } + return Collections.unmodifiableSet(roles); + } + public final static Authentication getAuthentication() { Set 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() { diff --git a/security/plugins/org.argeo.security.ui/src/main/java/org/argeo/security/ui/views/UserProfile.java b/security/plugins/org.argeo.security.ui/src/main/java/org/argeo/security/ui/views/UserProfile.java index afa569488..f28ab5733 100644 --- a/security/plugins/org.argeo.security.ui/src/main/java/org/argeo/security/ui/views/UserProfile.java +++ b/security/plugins/org.argeo.security.ui/src/main/java/org/argeo/security/ui/views/UserProfile.java @@ -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(CurrentUser.roles()).toArray(); + } + + public void dispose() { + } + + public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { + } + } } diff --git a/security/runtime/org.argeo.security.core/src/main/java/org/argeo/security/jcr/JcrUserDetails.java b/security/runtime/org.argeo.security.core/src/main/java/org/argeo/security/jcr/JcrUserDetails.java index 05ae165e0..223c06de0 100644 --- a/security/runtime/org.argeo.security.core/src/main/java/org/argeo/security/jcr/JcrUserDetails.java +++ b/security/runtime/org.argeo.security.core/src/main/java/org/argeo/security/jcr/JcrUserDetails.java @@ -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(); + } + }