Package com.jgcomptech.tools.authc
Class UserManager
- java.lang.Object
-
- com.jgcomptech.tools.authc.UserManager
-
public final class UserManager extends java.lang.Object
Manages all user accounts in the database.- Since:
- 1.4.0
-
-
Constructor Summary
Constructors Constructor Description UserManager(Database db)
Creates a new instance of the User Manager.UserManager(Database db, java.lang.String appIconPath)
Creates a new instance of the User Manager.UserManager(Database db, java.lang.String appIconPath, java.lang.String programName)
Creates a new instance of the User Manager.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
checkPasswordMatches(java.lang.String username, java.lang.String password)
Checks to see if the specified password matches the stored password in the database.java.lang.Object
clone()
boolean
createUser(java.lang.String username, java.lang.String password, UserRoleManager.SystemUserRoles userRole)
Creates a new user in the database using BCrypt password hashing.boolean
createUser(java.lang.String username, java.lang.String password, java.lang.String userRole)
Creates a new user in the database using BCrypt password hashing.boolean
deleteUser(java.lang.String username)
Deletes the specified user.boolean
disablePasswordExpiration(java.lang.String username)
Disables the specified user's password expiration.java.lang.String
getAppIconPath()
Returns the path to the icon to use for all dialogs.java.lang.String
getProgramName()
Returns the program name to use for all dialogs.SessionManager
getSessionManager()
Returns an instance of the SessionManager for login use.UserAccount
getUser(java.lang.String username)
Returns a UserAccount object representing the specified username.java.util.HashSet<java.lang.String>
getUsernameList()
Returns a list of the user names in the database.UserRole
getUserRole(java.lang.String username)
Returns user role for the specified username.java.util.HashSet<UserAccount>
getUsersList()
Returns a list of user accounts.void
setAppIconPath(java.lang.String appIconPath)
Sets the path to the icon to use for all dialogs.boolean
setLockStatus(java.lang.String username, java.lang.Boolean status)
Sets the lock status for the specified user.boolean
setPassword(java.lang.String username, java.lang.String password)
Sets a new password for an existing user using BCrypt password hashing.boolean
setPasswordExpirationDate(java.lang.String username, java.time.LocalDateTime date)
Sets the specified user's password expiration date.void
setProgramName(java.lang.String programName)
Sets the program name to use for all dialogs.boolean
setUserRole(java.lang.String username, UserRoleManager.SystemUserRoles userRole)
Sets the user role of the specified user.boolean
setUserRole(java.lang.String username, java.lang.String userRole)
Sets the user role of the specified user.boolean
userExists(java.lang.String username)
Checks if the specified username exists in the database.
-
-
-
Constructor Detail
-
UserManager
public UserManager(Database db)
Creates a new instance of the User Manager.- Parameters:
db
- the database to use- Throws:
java.lang.IllegalArgumentException
- if db is nullUserManagerException
- if an error occurs while creating the users table
-
UserManager
public UserManager(Database db, java.lang.String appIconPath)
Creates a new instance of the User Manager.- Parameters:
db
- the database to useappIconPath
- the path to the icon to use for all dialogs- Throws:
java.lang.IllegalArgumentException
- if db is nullUserManagerException
- if an error occurs while creating the users table
-
UserManager
public UserManager(Database db, java.lang.String appIconPath, java.lang.String programName)
Creates a new instance of the User Manager.- Parameters:
db
- the database to useappIconPath
- the path to the icon to use for all dialogsprogramName
- the program name to use for all dialogs- Throws:
java.lang.IllegalArgumentException
- if db is nullUserManagerException
- if an error occurs while creating the users table
-
-
Method Detail
-
createUser
public boolean createUser(java.lang.String username, java.lang.String password, UserRoleManager.SystemUserRoles userRole)
Creates a new user in the database using BCrypt password hashing.- Parameters:
username
- the username to addpassword
- the password for the new useruserRole
- the system user role for the new user- Returns:
- true if user creation is successful
- Throws:
java.lang.IllegalArgumentException
- if values are null or emptyTableNotFoundException
- if users table is missingUserManagerException
- if an error occurs while creating the userPasswordHashingFailedException
- if an error occurs while hashing the password
-
createUser
public boolean createUser(java.lang.String username, java.lang.String password, java.lang.String userRole)
Creates a new user in the database using BCrypt password hashing.- Parameters:
username
- the username to addpassword
- the password for the new useruserRole
- the name of the user role for the new user- Returns:
- true if user creation is successful
- Throws:
java.lang.IllegalArgumentException
- if values are null or emptyTableNotFoundException
- if users table is missingUserManagerException
- if an error occurs while creating the userPasswordHashingFailedException
- if an error occurs while hashing the password
-
deleteUser
public boolean deleteUser(java.lang.String username)
Deletes the specified user.- Parameters:
username
- the username of the user to delete- Returns:
- true if no errors occur
- Throws:
java.lang.IllegalArgumentException
- if username is null or emptyTableNotFoundException
- if users table is missingUserManagerException
- if an error occurs while deleting user
-
getUser
public UserAccount getUser(java.lang.String username)
Returns a UserAccount object representing the specified username.- Parameters:
username
- the username to lookup- Returns:
- a UserAccount object representing the specified username
- Throws:
java.lang.IllegalArgumentException
- if username is null or emptyTableNotFoundException
- if users table is missingUserManagerException
- if an error occurs during lookup
-
userExists
public boolean userExists(java.lang.String username)
Checks if the specified username exists in the database.- Parameters:
username
- the username to check- Returns:
- true if the user exists
- Throws:
java.lang.IllegalArgumentException
- if username is null or emptyTableNotFoundException
- if users table is missingUserManagerException
- if an error occurs during lookup
-
getUserRole
public UserRole getUserRole(java.lang.String username)
Returns user role for the specified username.- Parameters:
username
- the username to lookup- Returns:
- the user role
- Throws:
java.lang.IllegalArgumentException
- if username is null or empty or if username does not existTableNotFoundException
- if users table is missingUserManagerException
- if error occurs during lookup
-
setUserRole
public boolean setUserRole(java.lang.String username, UserRoleManager.SystemUserRoles userRole)
Sets the user role of the specified user.- Parameters:
username
- the username of the user to updateuserRole
- the system user role to change to- Returns:
- true if no errors occurred
- Throws:
java.lang.IllegalArgumentException
- if values are null or emptyTableNotFoundException
- if users table is missingUserManagerException
- if an error occurs while changing user role
-
setUserRole
public boolean setUserRole(java.lang.String username, java.lang.String userRole)
Sets the user role of the specified user.- Parameters:
username
- the username of the user to updateuserRole
- the name of the user role to change to- Returns:
- true if no errors occurred
- Throws:
java.lang.IllegalArgumentException
- if values are null or emptyTableNotFoundException
- if users table is missingUserManagerException
- if an error occurs while changing user role
-
setPassword
public boolean setPassword(java.lang.String username, java.lang.String password)
Sets a new password for an existing user using BCrypt password hashing.- Parameters:
username
- the username to changepassword
- the new password- Returns:
- true if password is changed successfully
- Throws:
java.lang.IllegalArgumentException
- if values are null or emptyTableNotFoundException
- if users table is missingUserManagerException
- if an error occurs while changing passwordPasswordHashingFailedException
- if an error occurs while hashing the password
-
setLockStatus
public boolean setLockStatus(java.lang.String username, java.lang.Boolean status)
Sets the lock status for the specified user.- Parameters:
username
- the username to changestatus
- the status to set- Returns:
- true if the status is changed successfully
- Throws:
java.lang.IllegalArgumentException
- if values are null or emptyTableNotFoundException
- if users table is missingUserManagerException
- if an error occurs while changing passwordPasswordHashingFailedException
- if an error occurs while hashing the password- Since:
- 1.5.0
-
disablePasswordExpiration
public boolean disablePasswordExpiration(java.lang.String username)
Disables the specified user's password expiration.- Parameters:
username
- the username to change- Returns:
- true if password expiration disabled successfully
- Throws:
java.lang.IllegalArgumentException
- if the username is null or emptyTableNotFoundException
- if users table is missingUserManagerException
- if an error occurs while changing passwordPasswordHashingFailedException
- if an error occurs while hashing the password- Since:
- 1.5.0
-
setPasswordExpirationDate
public boolean setPasswordExpirationDate(java.lang.String username, java.time.LocalDateTime date)
Sets the specified user's password expiration date.- Parameters:
username
- the username to changedate
- the date to set- Returns:
- true if the date is changed successfully
- Throws:
java.lang.IllegalArgumentException
- if values are null or emptyTableNotFoundException
- if users table is missingUserManagerException
- if an error occurs while changing passwordPasswordHashingFailedException
- if an error occurs while hashing the password- Since:
- 1.5.0
-
checkPasswordMatches
public boolean checkPasswordMatches(java.lang.String username, java.lang.String password)
Checks to see if the specified password matches the stored password in the database.- Parameters:
username
- the username to check againstpassword
- the password to check against- Returns:
- true if the passwords match
- Throws:
java.lang.IllegalArgumentException
- if values are null or empty or if username does not existTableNotFoundException
- if users table is missingUserManagerException
- if an error occurs during lookupPasswordHashingFailedException
- if an error occurs while hashing the password
-
getUsersList
public java.util.HashSet<UserAccount> getUsersList()
Returns a list of user accounts.- Returns:
- a HashSet of UserAccount objects representing the users in the users table
- Throws:
TableNotFoundException
- if users table is missingUserManagerException
- if an error occurs during lookup
-
getUsernameList
public java.util.HashSet<java.lang.String> getUsernameList()
Returns a list of the user names in the database.- Returns:
- a list of the user names in the database
- Throws:
TableNotFoundException
- if users table is missing
-
getSessionManager
public SessionManager getSessionManager()
Returns an instance of the SessionManager for login use.- Returns:
- an instance of the SessionManager
-
getAppIconPath
public java.lang.String getAppIconPath()
Returns the path to the icon to use for all dialogs.- Returns:
- the path to the icon to use for all dialogs
-
setAppIconPath
public void setAppIconPath(java.lang.String appIconPath)
Sets the path to the icon to use for all dialogs.- Parameters:
appIconPath
- the path to set
-
getProgramName
public java.lang.String getProgramName()
Returns the program name to use for all dialogs.- Returns:
- the program name to use for all dialogs
-
setProgramName
public void setProgramName(java.lang.String programName)
Sets the program name to use for all dialogs.- Parameters:
programName
- the program name to set
-
clone
@Contract(" -> fail") public java.lang.Object clone() throws java.lang.CloneNotSupportedException
- Overrides:
clone
in classjava.lang.Object
- Throws:
java.lang.CloneNotSupportedException
-
-