Package org.pac4j.core.profile
Class BasicUserProfile
- java.lang.Object
-
- org.pac4j.core.profile.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 attributevoid
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 namejava.util.Map<java.lang.String,java.lang.Object>
getAuthenticationAttributes()
Get all authentication attributes as an immutable mapjava.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 keyvoid
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)
-
-
-
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 identifierattributes
- 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 identifierattributes
- user attributesauthenticationAttributes
- authentication attributes
-
setId
public void setId(java.lang.String id)
Set the identifier.- Specified by:
setId
in interfaceUserProfile
- 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 interfaceUserProfile
- 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 interfaceUserProfile
- Returns:
- the typed user identifier
-
getUsername
public java.lang.String getUsername()
- Specified by:
getUsername
in interfaceUserProfile
-
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 interfaceUserProfile
- Parameters:
key
- key of the attributevalue
- 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 interfaceUserProfile
- Parameters:
key
- the attribute keyvalue
- 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 interfaceUserProfile
- Parameters:
key
- the key
-
removeAuthenticationAttribute
public void removeAuthenticationAttribute(java.lang.String key)
Remove an authentication attribute by its key- Specified by:
removeAuthenticationAttribute
in interfaceUserProfile
- 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 interfaceUserProfile
- 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 interfaceUserProfile
- 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 interfaceUserProfile
- 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 nameclazz
- 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 attributeclazz
- 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 interfaceUserProfile
- Parameters:
role
- the role to add.
-
addRoles
public void addRoles(java.util.Collection<java.lang.String> roles)
Add roles.- Specified by:
addRoles
in interfaceUserProfile
- 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 interfaceUserProfile
- 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 interfaceUserProfile
- Parameters:
permission
- the permission to add.
-
addPermissions
public void addPermissions(java.util.Collection<java.lang.String> permissions)
Add permissions.- Specified by:
addPermissions
in interfaceUserProfile
- 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 interfaceUserProfile
- 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 interfaceUserProfile
- Parameters:
rme
- whether the user is remembered.
-
isRemembered
public boolean isRemembered()
Is the user remembered?- Specified by:
isRemembered
in interfaceUserProfile
- Returns:
- whether the user is remembered.
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
writeExternal
public void writeExternal(java.io.ObjectOutput out) throws java.io.IOException
- Specified by:
writeExternal
in interfacejava.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 interfacejava.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.
-
getClientName
public java.lang.String getClientName()
- Specified by:
getClientName
in interfaceUserProfile
-
setClientName
public void setClientName(java.lang.String clientName)
- Specified by:
setClientName
in interfaceUserProfile
-
getLinkedId
public java.lang.String getLinkedId()
- Specified by:
getLinkedId
in interfaceUserProfile
-
setLinkedId
public void setLinkedId(java.lang.String linkedId)
- Specified by:
setLinkedId
in interfaceUserProfile
-
isExpired
public boolean isExpired()
- Specified by:
isExpired
in interfaceUserProfile
-
asPrincipal
public java.security.Principal asPrincipal()
- Specified by:
asPrincipal
in interfaceUserProfile
-
-