Package com.jgcomptech.tools.authc
Class Subject
- java.lang.Object
-
- com.jgcomptech.tools.authc.Subject
-
public final class Subject extends java.lang.Object
Manages all tasks related to the currently logged in user account.- Since:
- 1.5.0
-
-
Constructor Summary
Constructors Constructor Description Subject(AuthManager authManager)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
checkPasswordMatches(java.lang.String password)
Checks to see if the specified password matches the stored password in the database.boolean
disablePasswordExpiration()
Sets the user's password to never expire.java.time.Duration
getLastSessionDuration()
Returns a Duration object containing the elapsed time the last session was open.java.lang.String
getLastSessionDurationString()
Returns a string of the elapsed time the last session was open in the format 00:00:00.java.lang.String
getLastSessionDurationStringFull()
Returns a string of the elapsed time the last session was open in the format 00:00:00.0000.java.time.LocalDateTime
getPasswordExpirationDate()
Returns the user's password expiration date as a LocalDateTime object.java.lang.String
getPasswordExpirationDate(java.lang.String format)
Returns the user's password expiration date as a formatted string.Session
getSession()
Returns the Session object of the current logged in session.Session
getSession(boolean multiSession)
Returns the Session object of the current logged in session.java.time.LocalDateTime
getUserCreationDate()
Returns the date and time the user was initially created.java.lang.String
getUserCreationDate(java.lang.String format)
Returns the date and time the user was initially created.java.lang.String
getUsername()
Returns subject's the currently assigned username.UserRole
getUserRole()
Returns the user role of the currently assigned username.boolean
hasPermission(java.lang.String permissionName)
Checks if the currently assigned username has the specified permission.boolean
hasPermissions(java.lang.String... permissionNames)
Checks if the currently assigned username has ALL the specified permissions.boolean
hasPermissions(java.util.HashSet<java.lang.String> permissionNames)
Checks if the currently assigned username has ALL the specified permissions.boolean
isAnonymous()
Checks if the subject has an assigned username to manage.boolean
isAuthenticated()
Checks if the subject's assigned username is currently logged in.boolean
isAuthenticated(boolean multiSession)
Checks if the subject's assigned username is currently logged in.boolean
isPasswordExpired()
Returnstrue
if the user's password is expired and thus cannot be used to login.boolean
isPasswordSetToExpire()
Returnstrue
if the user's password has a set expiration date.boolean
isRemembered()
Checks if the current username and token are set to be saved after logout.boolean
isUserLocked()
Returnstrue
if the user is locked and thus cannot be used to login.boolean
lockUser()
Locks the user preventing use in login.boolean
login()
Attempts to login the specified user account from the previously saved token under the single-session context.boolean
login(boolean multiSession)
Attempts to login the specified user account from the previously saved token under the single-session context.boolean
login(UsernamePasswordToken token)
Attempts to login the specified user account from the specified token under the single-session context.boolean
login(UsernamePasswordToken token, boolean multiSession)
Attempts to login the specified user account from the specified token.boolean
logout()
Attempts to logout the currently logged in user under the single-session context.boolean
logout(boolean multiSession)
Attempts to logout the currently logged in user.boolean
setPassword(java.lang.String password)
Sets a new password for the user using BCrypt password hashing.boolean
setPasswordExpirationDate(java.time.LocalDateTime dateTime)
Sets the user's password to expire preventing login after the specified date and time.boolean
setUserRole(UserRoleManager.SystemUserRoles userRole)
Sets the user role of the currently assigned username.boolean
setUserRole(java.lang.String userRole)
Sets the user role of the currently assigned username.boolean
unlockUser()
Unlocks the user allowing use in login.
-
-
-
Constructor Detail
-
Subject
public Subject(AuthManager authManager)
-
-
Method Detail
-
login
@Contract("null -> fail") public boolean login(UsernamePasswordToken token) throws CredentialsException, ConcurrentAccessException
Attempts to login the specified user account from the specified token under the single-session context.- Parameters:
token
- the username and password token to use for login- Returns:
- true if login succeeds, false if login fails
- Throws:
CredentialsException
- if the token is null, if the username or password in the specified token are null or if the username in the specified token is an empty stringConcurrentAccessException
- if user is already logged in under the specified context
-
login
public boolean login() throws CredentialsException, ConcurrentAccessException
Attempts to login the specified user account from the previously saved token under the single-session context. The token is only saved if the previously supplied token had rememberMe set to true.- Returns:
- true if login succeeds, false if login fails
- Throws:
CredentialsException
- if no token was previously saved, if the token is null, if the username or password in the specified token are null or if the username in the specified token is an empty stringConcurrentAccessException
- if user is already logged in under the specified context
-
login
public boolean login(boolean multiSession) throws CredentialsException, ConcurrentAccessException
Attempts to login the specified user account from the previously saved token under the single-session context. The token is only saved if the previously supplied token had rememberMe set to true.- Parameters:
multiSession
- if true, logs in the user under the multi-session context, or if false, under the single-session context- Returns:
- true if login succeeds, false if login fails
- Throws:
CredentialsException
- if no token was previously saved, if the token is null, if the username or password in the specified token are null or if the username in the specified token is an empty stringConcurrentAccessException
- if user is already logged in under the specified context- Since:
- 1.5.1 new overload
-
login
@Contract("null, _ -> fail") public boolean login(UsernamePasswordToken token, boolean multiSession) throws CredentialsException, ConcurrentAccessException
Attempts to login the specified user account from the specified token.- Parameters:
token
- the username and password token to use for loginmultiSession
- if true, logs in the user under the multi-session context, or if false, under the single-session context- Returns:
- true if login succeeds, false if login fails
- Throws:
CredentialsException
- if the token is null, if the username or password in the specified token are null or if the username in the specified token is an empty stringConcurrentAccessException
- if user is already logged in under the specified context
-
logout
public boolean logout()
Attempts to logout the currently logged in user under the single-session context.- Returns:
- true if logout succeeds, false if logout fails
-
logout
public boolean logout(boolean multiSession)
Attempts to logout the currently logged in user.- Parameters:
multiSession
- if true, logs out the user under the multi-session context, * or if false, under the single-session context- Returns:
- true if logout succeeds, false if logout fails
-
getLastSessionDuration
@Contract(pure=true) public java.time.Duration getLastSessionDuration()
Returns a Duration object containing the elapsed time the last session was open.- Returns:
- a Duration object containing the elapsed time the last session was open
-
getLastSessionDurationString
public java.lang.String getLastSessionDurationString()
Returns a string of the elapsed time the last session was open in the format 00:00:00.- Returns:
- a string of the elapsed time the last session was open in the format 00:00:00
-
getLastSessionDurationStringFull
public java.lang.String getLastSessionDurationStringFull()
Returns a string of the elapsed time the last session was open in the format 00:00:00.0000.- Returns:
- a string of the elapsed time the last session was open in the format 00:00:00.0000
-
isAnonymous
public boolean isAnonymous()
Checks if the subject has an assigned username to manage.- Returns:
- true if the subject has an assigned username to manage
-
isAuthenticated
public boolean isAuthenticated()
Checks if the subject's assigned username is currently logged in.- Returns:
- true if the subject's assigned username is currently logged in
-
isAuthenticated
public boolean isAuthenticated(boolean multiSession)
Checks if the subject's assigned username is currently logged in.- Parameters:
multiSession
- if true, checks under the multi-session context, or if false, under the single-session context- Returns:
- true if the subject's assigned username is currently logged in
-
getSession
public Session getSession()
Returns the Session object of the current logged in session.- Returns:
- the Session object of the current logged in session
-
getSession
public Session getSession(boolean multiSession)
Returns the Session object of the current logged in session.- Parameters:
multiSession
- if true, checks under the multi-session context, or if false, under the single-session context- Returns:
- the Session object of the current logged in session
-
isRemembered
@Contract(pure=true) public boolean isRemembered()
Checks if the current username and token are set to be saved after logout.- Returns:
- true if the current username and token are set to be saved after logout
-
getUsername
@Contract(pure=true) public java.lang.String getUsername()
Returns subject's the currently assigned username.- Returns:
- subject's the currently assigned username
-
setPassword
public boolean setPassword(java.lang.String password)
Sets a new password for the user using BCrypt password hashing.- Parameters:
password
- the new password- Returns:
- true if password is changed successfully
-
getUserRole
public UserRole getUserRole()
Returns the user role of the currently assigned username.- Returns:
- the user role of the currently assigned username
-
hasPermission
public boolean hasPermission(java.lang.String permissionName)
Checks if the currently assigned username has the specified permission.- Parameters:
permissionName
- the name of the permission to check- Returns:
- true if the currently assigned username has the specified permission
-
hasPermissions
public boolean hasPermissions(java.util.HashSet<java.lang.String> permissionNames)
Checks if the currently assigned username has ALL the specified permissions.- Parameters:
permissionNames
- a list of all the names of the permissions to check- Returns:
- true if the currently assigned username has ALL the specified permissions
-
hasPermissions
public boolean hasPermissions(java.lang.String... permissionNames)
Checks if the currently assigned username has ALL the specified permissions.- Parameters:
permissionNames
- a list of all the names of the permissions to check- Returns:
- true if the currently assigned username has ALL the specified permissions
- Since:
- 1.5.1 new overload
-
setUserRole
public boolean setUserRole(UserRoleManager.SystemUserRoles userRole)
Sets the user role of the currently assigned username.- Parameters:
userRole
- the system user role to change to- Returns:
- true if no errors occurred
-
setUserRole
public boolean setUserRole(java.lang.String userRole)
Sets the user role of the currently assigned username.- Parameters:
userRole
- the name of the user role to change to- Returns:
- true if no errors occurred
-
getUserCreationDate
public java.time.LocalDateTime getUserCreationDate()
Returns the date and time the user was initially created.- Returns:
- the date and time the user was initially created as a LocalDateTime object
-
getUserCreationDate
public java.lang.String getUserCreationDate(java.lang.String format)
Returns the date and time the user was initially created.- Parameters:
format
- the pattern to use to format the timestamp- Returns:
- the date and time the user was initially created as a formatted string
-
isUserLocked
public boolean isUserLocked()
Returnstrue
if the user is locked and thus cannot be used to login.- Returns:
true
if the specified user is locked and thus cannot be used to login
-
lockUser
public boolean lockUser()
Locks the user preventing use in login.- Returns:
true
if no errors occurred
-
unlockUser
public boolean unlockUser()
Unlocks the user allowing use in login.- Returns:
true
if no errors occurred
-
isPasswordExpired
public boolean isPasswordExpired()
Returnstrue
if the user's password is expired and thus cannot be used to login.- Returns:
true
if the user's password is expired and thus cannot be used to login
-
isPasswordSetToExpire
public boolean isPasswordSetToExpire()
Returnstrue
if the user's password has a set expiration date.- Returns:
true
if the user's password has a set expiration date.
-
getPasswordExpirationDate
public java.time.LocalDateTime getPasswordExpirationDate()
Returns the user's password expiration date as a LocalDateTime object.- Returns:
- the user's password expiration date as a LocalDateTime object
-
getPasswordExpirationDate
public java.lang.String getPasswordExpirationDate(java.lang.String format)
Returns the user's password expiration date as a formatted string.- Parameters:
format
- the pattern to use to format the timestamp- Returns:
- the user's password expiration date as a formatted string
-
setPasswordExpirationDate
public boolean setPasswordExpirationDate(java.time.LocalDateTime dateTime)
Sets the user's password to expire preventing login after the specified date and time.- Parameters:
dateTime
- the expiration date and time to set- Returns:
- true if no errors occur
-
disablePasswordExpiration
public boolean disablePasswordExpiration()
Sets the user's password to never expire.- Returns:
- true if no errors occur
-
checkPasswordMatches
public boolean checkPasswordMatches(java.lang.String password)
Checks to see if the specified password matches the stored password in the database.- Parameters:
password
- the password to check against- Returns:
- true if the passwords match
-
-