Class BasicUserProfile

  • All Implemented Interfaces:
    java.io.Externalizable, java.io.Serializable, UserProfile
    Direct Known Subclasses:
    CommonProfile

    public class BasicUserProfile
    extends java.lang.Object
    implements UserProfile, java.io.Externalizable
    This class is the user profile retrieved from a provider after successful authentication: it's an identifier (string) and attributes (objects). Additional concepts are the "remember me" nature of the user profile, the associated roles, permissions, client name and linked identifier.
    Since:
    1.0.0
    Author:
    Jerome Leleu
    See Also:
    Serialized Form
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected org.slf4j.Logger logger  
    • Constructor Summary

      Constructors 
      Constructor Description
      BasicUserProfile()  
      BasicUserProfile​(boolean canAttributesBeMerged)
      Create a profile with possibility to merge attributes with the same name and collection-type values.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addAttribute​(java.lang.String key, java.lang.Object value)
      Add an attribute.
      void addAttributes​(java.util.Map<java.lang.String,​java.lang.Object> attributes)
      Add attributes.
      void addAuthenticationAttribute​(java.lang.String key, java.lang.Object value)
      Add an authentication-related attribute
      void addAuthenticationAttributes​(java.util.Map<java.lang.String,​java.lang.Object> attributeMap)
      Add authentication attributes.
      void addPermission​(java.lang.String permission)
      Add a permission.
      void addPermissions​(java.util.Collection<java.lang.String> permissions)
      Add permissions.
      void addRole​(java.lang.String role)
      Add a role.
      void addRoles​(java.util.Collection<java.lang.String> roles)
      Add roles.
      java.security.Principal asPrincipal()  
      void build​(java.lang.Object id, java.util.Map<java.lang.String,​java.lang.Object> attributes)
      Build a profile from user identifier and attributes.
      void build​(java.lang.Object id, java.util.Map<java.lang.String,​java.lang.Object> attributes, java.util.Map<java.lang.String,​java.lang.Object> authenticationAttributes)
      Build a profile from user identifier, attributes, and authentication attributes.
      boolean containsAttribute​(java.lang.String name)
      Check to see if profile contains attribute name.
      boolean containsAuthenicationAttribute​(java.lang.String name)
      Check to see if profile contains attribute name.
      java.util.List<java.lang.String> extractAttributeValues​(java.lang.String name)
      Return the attribute values with name.
      java.lang.Object getAttribute​(java.lang.String name)
      Return the attribute with name.
      <T> T getAttribute​(java.lang.String name, java.lang.Class<T> clazz)
      Return the attribute with name.
      java.util.Map<java.lang.String,​java.lang.Object> getAttributes()
      Get all attributes as immutable map.
      java.lang.Object getAuthenticationAttribute​(java.lang.String name)
      Return the authentication attribute with name.
      <T> T getAuthenticationAttribute​(java.lang.String name, java.lang.Class<T> clazz)
      Return authentication attribute with name
      java.util.Map<java.lang.String,​java.lang.Object> getAuthenticationAttributes()
      Get all authentication attributes as an immutable map
      java.lang.String getClientName()  
      java.lang.String getId()
      Get the user identifier.
      java.lang.String getLinkedId()  
      java.util.Set<java.lang.String> getPermissions()
      Get the permissions of the user.
      java.util.Set<java.lang.String> getRoles()
      Get the roles of the user.
      java.lang.String getTypedId()
      Get the user identifier with a prefix which is the profile type (full class name with package).
      java.lang.String getUsername()  
      boolean isExpired()  
      boolean isRemembered()
      Is the user remembered?
      void readExternal​(java.io.ObjectInput in)  
      void removeAttribute​(java.lang.String key)
      Remove an attribute by its key.
      void removeAuthenticationAttribute​(java.lang.String key)
      Remove an authentication attribute by its key
      void removeLoginData()
      Remove the specific data retrieved during the login process to only keep the user attributes, roles and permissions.
      void setClientName​(java.lang.String clientName)  
      void setId​(java.lang.String id)
      Set the identifier.
      void setLinkedId​(java.lang.String linkedId)  
      void setPermissions​(java.util.Set<java.lang.String> permissions)  
      void setRemembered​(boolean rme)
      Define if this profile is remembered.
      void setRoles​(java.util.Set<java.lang.String> roles)  
      java.lang.String toString()  
      void writeExternal​(java.io.ObjectOutput out)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • logger

        protected final transient org.slf4j.Logger logger
    • Constructor Detail

      • BasicUserProfile

        public BasicUserProfile()
      • BasicUserProfile

        public BasicUserProfile​(boolean canAttributesBeMerged)
        Create a profile with possibility to merge attributes with the same name and collection-type values.
        Parameters:
        canAttributesBeMerged - if true - merge attributes with the same name and collection-type values, if false - overwrite them
        Since:
        3.1.0
    • Method Detail

      • build

        public void build​(java.lang.Object id,
                          java.util.Map<java.lang.String,​java.lang.Object> attributes)
        Build a profile from user identifier and attributes.
        Parameters:
        id - user identifier
        attributes - user attributes
      • build

        public void build​(java.lang.Object id,
                          java.util.Map<java.lang.String,​java.lang.Object> attributes,
                          java.util.Map<java.lang.String,​java.lang.Object> authenticationAttributes)
        Build a profile from user identifier, attributes, and authentication attributes.
        Parameters:
        id - user identifier
        attributes - user attributes
        authenticationAttributes - authentication attributes
      • setId

        public void setId​(java.lang.String id)
        Set the identifier.
        Specified by:
        setId in interface UserProfile
        Parameters:
        id - user identifier
      • getId

        public java.lang.String getId()
        Get the user identifier. This identifier is unique for this provider but not necessarily through all providers.
        Specified by:
        getId in interface UserProfile
        Returns:
        the user identifier
      • getTypedId

        public java.lang.String getTypedId()
        Get the user identifier with a prefix which is the profile type (full class name with package). This identifier is unique through all providers.
        Specified by:
        getTypedId in interface UserProfile
        Returns:
        the typed user identifier
      • addAttribute

        public void addAttribute​(java.lang.String key,
                                 java.lang.Object value)
        Add an attribute. If existing attribute value is collection and the new value is collection - merge the collections
        Specified by:
        addAttribute in interface UserProfile
        Parameters:
        key - key of the attribute
        value - value of the attribute
      • addAuthenticationAttribute

        public void addAuthenticationAttribute​(java.lang.String key,
                                               java.lang.Object value)
        Add an authentication-related attribute
        Specified by:
        addAuthenticationAttribute in interface UserProfile
        Parameters:
        key - the attribute key
        value - the attribute value
      • addAttributes

        public void addAttributes​(java.util.Map<java.lang.String,​java.lang.Object> attributes)
        Add attributes.
        Parameters:
        attributes - use attributes
      • addAuthenticationAttributes

        public void addAuthenticationAttributes​(java.util.Map<java.lang.String,​java.lang.Object> attributeMap)
        Add authentication attributes.
        Parameters:
        attributeMap - the authentication attributes
      • removeAttribute

        public void removeAttribute​(java.lang.String key)
        Remove an attribute by its key.
        Specified by:
        removeAttribute in interface UserProfile
        Parameters:
        key - the key
      • removeAuthenticationAttribute

        public void removeAuthenticationAttribute​(java.lang.String key)
        Remove an authentication attribute by its key
        Specified by:
        removeAuthenticationAttribute in interface UserProfile
        Parameters:
        key - the key
      • getAttributes

        public java.util.Map<java.lang.String,​java.lang.Object> getAttributes()
        Get all attributes as immutable map.
        Specified by:
        getAttributes in interface UserProfile
        Returns:
        the immutable attributes
      • getAuthenticationAttributes

        public java.util.Map<java.lang.String,​java.lang.Object> getAuthenticationAttributes()
        Get all authentication attributes as an immutable map
        Returns:
        the immutable authentication attributes
      • getAttribute

        public java.lang.Object getAttribute​(java.lang.String name)
        Return the attribute with name.
        Specified by:
        getAttribute in interface UserProfile
        Parameters:
        name - attribute name
        Returns:
        the attribute with name
      • extractAttributeValues

        public java.util.List<java.lang.String> extractAttributeValues​(java.lang.String name)
        Return the attribute values with name.
        Parameters:
        name - attribute name
        Returns:
        the attribute values as List of strings.
      • getAuthenticationAttribute

        public java.lang.Object getAuthenticationAttribute​(java.lang.String name)
        Return the authentication attribute with name.
        Parameters:
        name - authentication attribute name
        Returns:
        the authentication attribute with name
      • containsAttribute

        public boolean containsAttribute​(java.lang.String name)
        Check to see if profile contains attribute name.
        Specified by:
        containsAttribute in interface UserProfile
        Parameters:
        name - the name
        Returns:
        true/false
      • containsAuthenicationAttribute

        public boolean containsAuthenicationAttribute​(java.lang.String name)
        Check to see if profile contains attribute name.
        Parameters:
        name - the name
        Returns:
        true/false
      • getAttribute

        public <T> T getAttribute​(java.lang.String name,
                                  java.lang.Class<T> clazz)
        Return the attribute with name.
        Type Parameters:
        T - the type of the attribute
        Parameters:
        name - the attribute name
        clazz - the class of the attribute
        Returns:
        the attribute by its name
        Since:
        1.8
      • getAuthenticationAttribute

        public <T> T getAuthenticationAttribute​(java.lang.String name,
                                                java.lang.Class<T> clazz)
        Return authentication attribute with name
        Type Parameters:
        T - The type of the authentication attribute
        Parameters:
        name - Name of authentication attribute
        clazz - The class of the authentication attribute
        Returns:
        the named attribute
      • addRole

        public void addRole​(java.lang.String role)
        Add a role.
        Specified by:
        addRole in interface UserProfile
        Parameters:
        role - the role to add.
      • addRoles

        public void addRoles​(java.util.Collection<java.lang.String> roles)
        Add roles.
        Specified by:
        addRoles in interface UserProfile
        Parameters:
        roles - the roles to add.
      • getRoles

        public java.util.Set<java.lang.String> getRoles()
        Get the roles of the user.
        Specified by:
        getRoles in interface UserProfile
        Returns:
        the user roles.
      • setRoles

        public void setRoles​(java.util.Set<java.lang.String> roles)
      • addPermission

        public void addPermission​(java.lang.String permission)
        Add a permission.
        Specified by:
        addPermission in interface UserProfile
        Parameters:
        permission - the permission to add.
      • addPermissions

        public void addPermissions​(java.util.Collection<java.lang.String> permissions)
        Add permissions.
        Specified by:
        addPermissions in interface UserProfile
        Parameters:
        permissions - the permissions to add.
      • getPermissions

        public java.util.Set<java.lang.String> getPermissions()
        Get the permissions of the user.
        Specified by:
        getPermissions in interface UserProfile
        Returns:
        the user permissions.
      • setPermissions

        public void setPermissions​(java.util.Set<java.lang.String> permissions)
      • setRemembered

        public void setRemembered​(boolean rme)
        Define if this profile is remembered.
        Specified by:
        setRemembered in interface UserProfile
        Parameters:
        rme - whether the user is remembered.
      • isRemembered

        public boolean isRemembered()
        Is the user remembered?
        Specified by:
        isRemembered in interface UserProfile
        Returns:
        whether the user is remembered.
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • writeExternal

        public void writeExternal​(java.io.ObjectOutput out)
                           throws java.io.IOException
        Specified by:
        writeExternal in interface java.io.Externalizable
        Throws:
        java.io.IOException
      • readExternal

        public void readExternal​(java.io.ObjectInput in)
                          throws java.io.IOException,
                                 java.lang.ClassNotFoundException
        Specified by:
        readExternal in interface java.io.Externalizable
        Throws:
        java.io.IOException
        java.lang.ClassNotFoundException
      • removeLoginData

        public void removeLoginData()
        Remove the specific data retrieved during the login process to only keep the user attributes, roles and permissions.
      • setClientName

        public void setClientName​(java.lang.String clientName)
        Specified by:
        setClientName in interface UserProfile
      • setLinkedId

        public void setLinkedId​(java.lang.String linkedId)
        Specified by:
        setLinkedId in interface UserProfile
      • asPrincipal

        public java.security.Principal asPrincipal()
        Specified by:
        asPrincipal in interface UserProfile