Class User
public class User
extends java.lang.Object
This class represents a user. It is a value class that stores only the user id and a reference
to an AbstractUserDatabase
to access its properties.
An object can point to a valid user, or be invalid. Invalid users are typically used as return value for database queries which did not match with an existing user.
Not all methods are valid or applicable to your authentication system. See AbstractUserDatabase
for a discussion.
- See Also:
AbstractUserDatabase
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
User.EmailTokenRole
Enumeration for an email token stored for the user.static class
User.Status
Enumeration for a user's account status. -
Constructor Summary
Constructors Constructor Description User()
Default constructor.User(java.lang.String id, AbstractUserDatabase userDatabase)
Constructor. -
Method Summary
Modifier and Type Method Description void
addAuthToken(Token token)
Adds an authentication token.void
addIdentity(java.lang.String provider, java.lang.String identity)
Adds an identity.void
clearEmailToken()
Clears the email token.boolean
equals(User other)
Indicates whether some other object is "equal to" this one.AbstractUserDatabase
getDatabase()
Returns the user database.java.lang.String
getEmail()
Returns the email address.Token
getEmailToken()
Returns the email token.User.EmailTokenRole
getEmailTokenRole()
Returns the email token role.int
getFailedLoginAttempts()
Returns the number of consecutive unsuccessful login attempts.java.lang.String
getId()
Returns the user id.java.lang.String
getIdentity(java.lang.String provider)
Returns an identity.WDate
getLastLoginAttempt()
Returns the last login attempt.PasswordHash
getPassword()
Returns the password.User.Status
getStatus()
Returns the account status.java.lang.String
getUnverifiedEmail()
Returns the unverified email address.boolean
isValid()
Returns whether the user is valid.void
removeAuthToken(java.lang.String token)
Removes an authentication token.void
removeIdentity(java.lang.String provider)
Removes an identity.void
setAuthenticated(boolean success)
Logs the result of an authentication attempt.void
setEmail(java.lang.String address)
Sets the email address.void
setEmailToken(Token token, User.EmailTokenRole role)
Sets an email token.void
setIdentity(java.lang.String provider, java.lang.String identity)
Sets an identity.void
setPassword(PasswordHash password)
Sets a password.void
setStatus(User.Status status)
Sets the account status.void
setUnverifiedEmail(java.lang.String address)
Sets the unverified email address.int
updateAuthToken(java.lang.String hash, java.lang.String newHash)
Updates an authentication token.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Constructor Details
-
User
public User()Default constructor.Creates an invalid user.
- See Also:
isValid()
-
User
Constructor.Creates a user with id
id
, and whose information is stored in thedatabase
.
-
-
Method Details
-
getDatabase
Returns the user database.This returns the user database passed in the constructor, or 0 if the user is invalid, and was constructed using the default constructor.
-
equals
Indicates whether some other object is "equal to" this one.Two users are equal if they have the same identity and the same database.
-
isValid
public boolean isValid()Returns whether the user is valid.A invalid user is a sentinel value returned by methods that query the database but could not identify a matching user.
-
getId
public java.lang.String getId()Returns the user id.This returns the id that uniquely identifies the user, and acts as a "primary key" to obtain other information for the user in the database.
- See Also:
AbstractUserDatabase
-
getIdentity
public java.lang.String getIdentity(java.lang.String provider)Returns an identity. -
addIdentity
public void addIdentity(java.lang.String provider, java.lang.String identity)Adds an identity.Depending on whether the database supports multiple identities per provider, this may change (like
setIdentity()
), or add another identity to the user. For some identity providers (e.g. a 3rd party identity provider), it may be sensible to have more than one identity of the same provider for a single user (e.g. multiple email accounts managed by the same provider, that in fact identify the same user). -
setIdentity
public void setIdentity(java.lang.String provider, java.lang.String identity)Sets an identity.Unlike
addIdentity()
this overrides any other identity of the given provider, in case the underlying database supports multiple identities per user. -
removeIdentity
public void removeIdentity(java.lang.String provider)Removes an identity. -
setPassword
Sets a password. -
getPassword
Returns the password. -
setEmail
public void setEmail(java.lang.String address)Sets the email address. -
getEmail
public java.lang.String getEmail()Returns the email address.- See Also:
AbstractUserDatabase.getEmail(User user)
-
setUnverifiedEmail
public void setUnverifiedEmail(java.lang.String address)Sets the unverified email address. -
getUnverifiedEmail
public java.lang.String getUnverifiedEmail()Returns the unverified email address. -
getStatus
Returns the account status. -
setStatus
Sets the account status. -
getEmailToken
Returns the email token. -
getEmailTokenRole
Returns the email token role. -
setEmailToken
Sets an email token. -
clearEmailToken
public void clearEmailToken()Clears the email token. -
addAuthToken
Adds an authentication token. -
removeAuthToken
public void removeAuthToken(java.lang.String token)Removes an authentication token. -
updateAuthToken
public int updateAuthToken(java.lang.String hash, java.lang.String newHash)Updates an authentication token. -
setAuthenticated
public void setAuthenticated(boolean success)Logs the result of an authentication attempt.This changes the number of failed login attempts, and stores the current date as the last login attempt time.
- See Also:
getFailedLoginAttempts()
,getLastLoginAttempt()
-
getFailedLoginAttempts
public int getFailedLoginAttempts()Returns the number of consecutive unsuccessful login attempts.- See Also:
setAuthenticated(boolean success)
-
getLastLoginAttempt
Returns the last login attempt.- See Also:
setAuthenticated(boolean success)
-