Package com.jgcomptech.tools.authc
Class Subject
- java.lang.Object
-
- com.jgcomptech.tools.authc.Subject
-
public final class Subject extends java.lang.ObjectManages 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 booleancheckPasswordMatches(java.lang.String password)Checks to see if the specified password matches the stored password in the database.booleandisablePasswordExpiration()Sets the user's password to never expire.java.time.DurationgetLastSessionDuration()Returns a Duration object containing the elapsed time the last session was open.java.lang.StringgetLastSessionDurationString()Returns a string of the elapsed time the last session was open in the format 00:00:00.java.lang.StringgetLastSessionDurationStringFull()Returns a string of the elapsed time the last session was open in the format 00:00:00.0000.java.time.LocalDateTimegetPasswordExpirationDate()Returns the user's password expiration date as a LocalDateTime object.java.lang.StringgetPasswordExpirationDate(java.lang.String format)Returns the user's password expiration date as a formatted string.SessiongetSession()Returns the Session object of the current logged in session.SessiongetSession(boolean multiSession)Returns the Session object of the current logged in session.java.time.LocalDateTimegetUserCreationDate()Returns the date and time the user was initially created.java.lang.StringgetUserCreationDate(java.lang.String format)Returns the date and time the user was initially created.java.lang.StringgetUsername()Returns subject's the currently assigned username.UserRolegetUserRole()Returns the user role of the currently assigned username.booleanhasPermission(java.lang.String permissionName)Checks if the currently assigned username has the specified permission.booleanhasPermissions(java.lang.String... permissionNames)Checks if the currently assigned username has ALL the specified permissions.booleanhasPermissions(java.util.HashSet<java.lang.String> permissionNames)Checks if the currently assigned username has ALL the specified permissions.booleanisAnonymous()Checks if the subject has an assigned username to manage.booleanisAuthenticated()Checks if the subject's assigned username is currently logged in.booleanisAuthenticated(boolean multiSession)Checks if the subject's assigned username is currently logged in.booleanisPasswordExpired()Returnstrueif the user's password is expired and thus cannot be used to login.booleanisPasswordSetToExpire()Returnstrueif the user's password has a set expiration date.booleanisRemembered()Checks if the current username and token are set to be saved after logout.booleanisUserLocked()Returnstrueif the user is locked and thus cannot be used to login.booleanlockUser()Locks the user preventing use in login.booleanlogin()Attempts to login the specified user account from the previously saved token under the single-session context.booleanlogin(boolean multiSession)Attempts to login the specified user account from the previously saved token under the single-session context.booleanlogin(UsernamePasswordToken token)Attempts to login the specified user account from the specified token under the single-session context.booleanlogin(UsernamePasswordToken token, boolean multiSession)Attempts to login the specified user account from the specified token.booleanlogout()Attempts to logout the currently logged in user under the single-session context.booleanlogout(boolean multiSession)Attempts to logout the currently logged in user.booleansetPassword(java.lang.String password)Sets a new password for the user using BCrypt password hashing.booleansetPasswordExpirationDate(java.time.LocalDateTime dateTime)Sets the user's password to expire preventing login after the specified date and time.booleansetUserRole(UserRoleManager.SystemUserRoles userRole)Sets the user role of the currently assigned username.booleansetUserRole(java.lang.String userRole)Sets the user role of the currently assigned username.booleanunlockUser()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, ConcurrentAccessExceptionAttempts 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, ConcurrentAccessExceptionAttempts 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, ConcurrentAccessExceptionAttempts 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, ConcurrentAccessExceptionAttempts 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()
Returnstrueif the user is locked and thus cannot be used to login.- Returns:
trueif the specified user is locked and thus cannot be used to login
-
lockUser
public boolean lockUser()
Locks the user preventing use in login.- Returns:
trueif no errors occurred
-
unlockUser
public boolean unlockUser()
Unlocks the user allowing use in login.- Returns:
trueif no errors occurred
-
isPasswordExpired
public boolean isPasswordExpired()
Returnstrueif the user's password is expired and thus cannot be used to login.- Returns:
trueif the user's password is expired and thus cannot be used to login
-
isPasswordSetToExpire
public boolean isPasswordSetToExpire()
Returnstrueif the user's password has a set expiration date.- Returns:
trueif 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
-
-