]> git.argeo.org Git - lgpl/argeo-commons.git/blobdiff - security/plugins/org.argeo.security.ui/src/main/java/org/argeo/security/ui/internal/CurrentUser.java
Improve logging
[lgpl/argeo-commons.git] / security / plugins / org.argeo.security.ui / src / main / java / org / argeo / security / ui / internal / CurrentUser.java
index 1abdc15646dc6453045762147f3ad503458f36fe..7b5287b7b0f572d57f444255e38eda4f4f482d4e 100644 (file)
@@ -12,6 +12,10 @@ import org.argeo.ArgeoException;
 import org.springframework.security.Authentication;
 import org.springframework.security.GrantedAuthority;
 
+/**
+ * Retrieves information about the current user. Not an API, can change without
+ * notice.
+ */
 public class CurrentUser {
        public final static String getUsername() {
                Subject subject = getSubject();
@@ -23,21 +27,29 @@ public class CurrentUser {
        }
 
        public final static Set<String> roles() {
-               Principal principal = getSubject().getPrincipals().iterator().next();
-               Authentication authentication = (Authentication) principal;
                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 Subject getSubject() {
+       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;
+                       return authentication;
+               }
+               throw new ArgeoException("No authentication found");
+       }
 
+       public final static Subject getSubject() {
                Subject subject = Subject.getSubject(AccessController.getContext());
                if (subject == null)
                        throw new ArgeoException("Not authenticated.");
                return subject;
-
        }
 }