diff --git a/.gitignore b/.gitignore deleted file mode 100644 index 1db73ab..0000000 --- a/.gitignore +++ /dev/null @@ -1,6 +0,0 @@ -# Created by .ignore support plugin (hsz.mobi) -site/ -#.gitignore -.idea/ -JavaUltimateToolsDocs.iml -CNAME diff --git a/.nojekyll b/.nojekyll new file mode 100644 index 0000000..e69de29 diff --git a/404.html b/404.html new file mode 100755 index 0000000..d063aca --- /dev/null +++ b/404.html @@ -0,0 +1,565 @@ + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +Package Name: com.jgcomptech.tools.authc.AuthManager
+The Auth Manager class contains all methods needed for controlling the Auth System. +This includes methods to manage user accounts, login sessions, user roles and user permissions.
+The Auth Manager is a singleton class but a new instance still needs to be initialized upon first use. +When a new instance is created a "users" table is automatically created in the specified database. +The following code can be used to create a new instance: +
1 +2 +3 | try(final var db = new Database("./mydb.db", DatabaseType.H2)) { + final var manager = AuthManager.getNewInstance(db); +} + |
After the new instance has been created you can use the following to retrieve the instance again: +
1 +2 +3 | try(final var db = new Database("./mydb.db", DatabaseType.H2)) { + final var manager = AuthManager.getInstance(); +} + |
An icon path and program name may also be optionally provided as parameters to change the +icon and program name for all login dialogs.
+To create a new user use the createUser method supplying the following parameters:
+The user role can be a text string or one of the +items from the UserRoleManager.SystemUserRoles enum.
+The UserRoleManager.SystemUserRoles list contains:
+Note
+The NONE user role can never have permissions assigned to it. +Using the NONE user role is a quick way to remove all permissions from a user.
+Example
+1 +2 +3 | final var authManager = AuthManager.getInstance(); +authManager.createUser("admin", "1234", UserRoleManager.SystemUserRoles.ADMIN); +authManager.createUser("editor", "1234", UserRoleManager.SystemUserRoles.EDITOR); + |
To delete an existing user use the deleteUser method supplying the following parameter:
+Example
+1 +2 | final var authManager = AuthManager.getInstance(); +authManager.deleteUser("admin"); + |
Retrieves a readonly immutable UserAccount object. +To retrieve the object supply the following parameter:
+Example
+1 +2 | final var authManager = AuthManager.getInstance(); +final UserAccount user = authManager.getUser("admin"); + |
Checks to see if the specified user account exists. +To check a user account, supply the following parameter:
+Example
+1 +2 +3 +4 | final var authManager = AuthManager.getInstance(); +final boolean result = authManager.userExists("admin"); +if(result) MessageBox.show("Admin User Exists!"); +else MessageBox.show("Admin User Does Not Exist!"); + |
Returns the date and time the user was initially created.
+There is two methods to retrieving the user creation date. +The date can either be returned as a LocalDateTime object or as a formatted string.
+To retrieve the user creation date, supply the following parameter:
+Example
+1 +2 +3 +4 | final var authManager = AuthManager.getInstance(); +final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"); +final LocalDateTime result = authManager.getUserCreationDate("admin"); +MessageBox.show("Account Was Created On " + result.format(formatter)); + |
To retrieve the user creation date, supply the following parameters:
+Example
+1 +2 +3 | final var authManager = AuthManager.getInstance(); +final String result = authManager.getUserCreationDate("admin", "yyyy-MM-dd HH:mm"); +MessageBox.show("Account Was Created On " + result); + |
Retrieves the UserRole object assigned to the specified user. +To retrieve the object, supply the following parameter:
+Example
+1 +2 | final var authManager = AuthManager.getInstance(); +final UserRole userRole = authManager.getUserRole("admin"); + |
Sets the user role assigned to the specified user. +To set the user role, supply the following parameters:
+The user role can be a text string or one of the +items from the UserRoleManager.SystemUserRoles enum.
+The UserRoleManager.SystemUserRoles list contains:
+Note
+The NONE user role can never have permissions assigned to it. +Using the NONE user role is a quick way to remove all permissions from a user.
+Example
+1 +2 | final var authManager = AuthManager.getInstance(); +userManager.setUserRole("admin", UserRoleManager.SystemUserRoles.ADMIN); + |
Sets the password assigned to the specified user. +To set the new password, supply the following parameters:
+Note
+If you want to prevent a user from setting an empty password +or to check for password complexity requirements those checks need +to be implemented separately. This method allows empty passwords. +Even if the password is empty, the empty password is still encrypted +with a random secure salt just like any supplied password would be.
+Example
+1 +2 | final var authManager = AuthManager.getInstance(); +authManager.setPassword("admin", "newPass"); + |
This method allows you to check if the supplied password matches +the user's stored password in the database. +To verify the password, supply the following parameters:
+Example
+1 +2 +3 +4 | final var authManager = AuthManager.getInstance(); +final boolean result = authManager.checkPasswordMatches("admin", "1234"); +if(result) MessageBox.show("Password Matches!"); +else MessageBox.show("Password Does Not Match!"); + |
A locked user account prevents login and prevents any new sessions +to be opened for the user. Locking an user account may be needed for numerous reasons.
+To lock and unlock an account, supply the following parameter:
+Example
+1 +2 +3 +4 +5 | final var authManager = AuthManager.getInstance(); +//Lock The Account +authManager.lockUser("admin"); +//Unlock The Account +authManager.unlockUser("admin"); + |
A locked user account prevents login and prevents any new sessions +to be opened for the user. Locking an user account may be needed for numerous reasons.
+To check if an account is locked, supply the following parameter:
+Example
+1 +2 +3 +4 | final var authManager = AuthManager.getInstance(); +final boolean result = authManager.isUserLocked("admin"); +if(result) MessageBox.show("Admin Account Is Locked!"); +else MessageBox.show("Admin Account Is Unlocked!"); + |
Setting a password to expire after a specified date allows the prevention of login and +new session creation if the password is expired. Setting a password to expire could +be used to force regular password changes or allow for temporary accounts to be made.
+To set a password expiration date, supply the following parameters:
+Example
+1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +10 +11 +12 +13 | final var authManager = AuthManager.getInstance(); +//Sets the date to now +authManager.setPasswordExpirationDate("admin", LocalDateTime.now()); +//Sets the date to 30 days from today +authManager.setPasswordExpirationDate("admin", LocalDateTime.now().plusDays(30)); +//First Method To Set the date to January 1st, 2019 at 8am +authManager.setPasswordExpirationDate("admin", LocalDateTime.of(2019, 1, 1, 8, 0)); +//Second Method To Set the date to January 1st of 2019 at 8am +authManager.setPasswordExpirationDate("admin", LocalDateTime.of(2018, Month.JANUARY, 1, 8, 0)); +//Third Method To Set the date to January 1st of 2019 at 8am +final String str = "2019-01-01 08:00"; +final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"); +authManager.setPasswordExpirationDate("admin", LocalDateTime.parse(str, formatter)); + |
Setting a password to expire after a specified date allows the prevention of login and +new session creation if the password is expired. Setting a password to expire could +be used to force regular password changes or allow for temporary accounts to be made.
+To disable password expiration for a user, supply the following parameter:
+Example
+1 +2 | final var authManager = AuthManager.getInstance(); +authManager.disablePasswordExpiration("admin"); + |
Setting a password to expire after a specified date allows the prevention of login and +new session creation if the password is expired. Setting a password to expire could +be used to force regular password changes or allow for temporary accounts to be made.
+To check if a user's password is expired, supply the following parameter:
+Example
+1 +2 +3 +4 | final var authManager = AuthManager.getInstance(); +final boolean result = authManager.isPasswordExpired("admin"); +if(result) MessageBox.show("Admin Account Password Is Expired!"); +else MessageBox.show("Admin Account Password Is Not Expired!"); + |
Setting a password to expire after a specified date allows the prevention of login and +new session creation if the password is expired. Setting a password to expire could +be used to force regular password changes or allow for temporary accounts to be made.
+To check if a user's password has an expiration date, supply the following parameter:
+Example
+1 +2 +3 +4 | final var authManager = AuthManager.getInstance(); +final boolean result = authManager.isPasswordSetToExpire("admin"); +if(result) MessageBox.show("Admin Account Password Has An Expiration Date!"); +else MessageBox.show("Admin Account Password Does Not Have An Expiration Date!"); + |
Setting a password to expire after a specified date allows the prevention of login and +new session creation if the password is expired. Setting a password to expire could +be used to force regular password changes or allow for temporary accounts to be made.
+There is two methods to retrieving the password expiration date. +The date can either be returned as a LocalDateTime object or as a formatted string.
+Note
+If the expiration date has been disabled or was never enabled this method will +still return an expiration date. For logistical database reasons a date still +has to be set. So the date will be set to 1000 years after the user account +was created or 1000 years after the date when the password expiration date +was last disabled. This number can be updated if any bugs occur but it seems +like 1000 years is far enough in the future to not cause any problems.
+To retrieve the password expiration date, supply the following parameter:
+Example
+1 +2 +3 +4 | final var authManager = AuthManager.getInstance(); +final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"); +final LocalDateTime result = authManager.getPasswordExpirationDate("admin"); +MessageBox.show("Admin Account Password Expires At " + result.format(formatter)); + |
To retrieve the password expiration date, supply the following parameters:
+Example
+1 +2 +3 | final var authManager = AuthManager.getInstance(); +final String result = authManager.getPasswordExpirationDate("admin", "yyyy-MM-dd HH:mm"); +MessageBox.show("Admin Account Password Expires At " + result); + |
Returns the username of the currently logged in user under the single session context.
+Example
+1 +2 | final var authManager = AuthManager.getInstance(); +final String currentUsername = authManager.getLoggedInUsername(); + |
Checks if a user is currently logged in under the single-user context.
+Example
+1 +2 +3 +4 | final var authManager = AuthManager.getInstance(); +final boolean result = authManager.isUserLoggedIn(); +if(result) MessageBox.show("A user is logged-in!"); +else MessageBox.show("No user is logged-in!"); + |
Checks if the specified user is currently logged in under the single-user context. +To check if a specific user is logged-in, supply the following parameter:
+Example
+1 +2 +3 +4 | final var authManager = AuthManager.getInstance(); +final boolean result = authManager.isUserLoggedIn("admin"); +if(result) MessageBox.show("Admin user is logged-in!"); +else MessageBox.show("Admin user is not logged-in!"); + |
Retrieves the current session for the specified username, under the single-user context.
+Example
+1 +2 | final var authManager = AuthManager.getInstance(); +final Session currentSession = authManager.getSession(); + |
Retrieves the current session for the specified username, under the single-user context.
+To retrieve a specific user session, supply the following parameter:
+Example
+1 +2 | final var authManager = AuthManager.getInstance(); +final Session session = authManager.getSession("admin"); + |
Shows the login dialog window to log a user into the single-user context. +The method returns true if a user was logged in successfully. +Fires either the sessionLoginSuccess or the sessionLoginFailure event +allowing the getUser method to be called by the assigned EventHandler. +If the user does not exist, getUser will return null.
+To show the login dialog, supply the following parameter:
+Example
+1 +2 +3 +4 | final var authManager = AuthManager.getInstance(); +final boolean result = authManager.showLoginWindow(true); +if(result) MessageBox.show("User was logged-in successfully!"); +else MessageBox.show("User login failed!"); + |
Logs in a user, under the single-user context, with the specified username, +without checking for valid credentials.
+To retrieve a specific user session, supply the following parameter:
+Warning
+This method does not validate credentials and should not be used except +in advanced use cases. A example case could be to login a user during unit testing. +The method showLoginWindow should be used instead. With this method +the user is immediately logged in as long as the user exists and other +checks pass (account unlocked, user role enabled and password not expired).
+Example
+1 +2 +3 +4 | final var authManager = AuthManager.getInstance(); +final boolean result = authManager.loginUser("admin"); +if(result) MessageBox.show("User was logged-in successfully!"); +else MessageBox.show("User login failed!"); + |
Logs out the specified user from the single session context +and clears any set permissions. This method will return false +if no user is currently logged-in.
+Caution
+If the user was deleted from the database while logged in, then +the getUser event method that is accessible from the +assigned event handler will return null.
+Example
+1 +2 +3 +4 | final var authManager = AuthManager.getInstance(); +final boolean result = authManager.logoutUser(); +if(result) MessageBox.show("User was logged-out successfully!"); +else MessageBox.show("User logout failed!"); + |
Logs out the specified user from the single session context +and clears any set permissions. This method will return false +if no user is currently logged-in or if the specified user does not exist.
+To logout a specific user, supply the following parameter:
+Caution
+If the user was deleted from the database while logged in, then +the getUser event method that is accessible from the +assigned event handler will return null.
+Example
+1 +2 +3 +4 | final var authManager = AuthManager.getInstance(); +final boolean result = authManager.logoutUser("admin"); +if(result) MessageBox.show("User was logged-out successfully!"); +else MessageBox.show("User logout failed!"); + |
Retrieves the user role of the currently logged in user under +the single user context. If no user is currently logged-in then this +method will return null.
+Example
+1 +2 | final var authManager = AuthManager.getInstance(); +final UserRole result = authManager.getLoggedInUserRole(); + |
Checks if an admin user is currently logged in under the single user context.
+Example
+1 +2 +3 +4 | final var authManager = AuthManager.getInstance(); +final boolean result = authManager.isAdminLoggedIn(); +if(result) MessageBox.show("An Admin user is logged-in!"); +else MessageBox.show("An Admin user is not logged-in!"); + |
Requests an admin user to authenticate to override permissions by +showing the login dialog using the single session context. During this +process the admin user is not actually logged-in, instead just their +username, password and user role is verified. Returns true if +admin override succeeded and false if the cancel button was pressed +or if override failed. +Fires either the sessionLoginSuccess or the sessionLoginFailure event +allowing the getUser method to be called by the assigned EventHandler. +If the user does not exist, getUser will return null.
+To show the login dialog, supply the following parameter:
+Example
+1 +2 +3 +4 | final var authManager = AuthManager.getInstance(); +final boolean result = authManager.getAdminOverride(); +if(result) MessageBox.show("Admin override succedded!"); +else MessageBox.show("Admin override failed!"); + |
Requests that the currently logged in username to re-login using +the single session context. During this process the user is not +actually logged-in again, instead just their username and password is +verified. Returns true if the user verification succeeded and false +if the cancel button was pressed or if override failed. +Fires either the sessionLoginSuccess or the sessionLoginFailure event +allowing the getUser method to be called by the assigned EventHandler. +If the user does not exist, getUser will return null.
+To show the login dialog, supply the following parameter:
+Example
+1 +2 +3 +4 | final var authManager = AuthManager.getInstance(); +final boolean result = authManager.getUserVerification(); +if(result) MessageBox.show("User verification succedded!"); +else MessageBox.show("User verification failed!"); + |
Checks that the logged-in user, under the single session context, +is an admin and if not, requests an admin override. +During this process the user is not actually logged-in again, +instead their username, password and user role is just verified.
+Example
+1 +2 +3 +4 | final var authManager = AuthManager.getInstance(); +final boolean result = authManager.requireAdmin(); +if(result) MessageBox.show("Admin check passed!"); +else MessageBox.show("Admin check failed!"); + |
Checks that the logged-in user, under the single session context, +is an admin, if true, prompts the admin to re-login and +if false, requests an admin override. +During this process the user is not actually logged-in again, +instead their username, password and user role is just verified.
+Example
+1 +2 +3 +4 | final var authManager = AuthManager.getInstance(); +final boolean result = authManager.requireAdmin(); +if(result) MessageBox.show("Admin check passed!"); +else MessageBox.show("Admin check failed!"); + |
Checks if the specified user is currently logged in under the multi-user context. +To check if a specific user is logged-in, supply the following parameters:
+Example
+1 +2 +3 +4 | final var authManager = AuthManager.getInstance(); +final boolean result = authManager.isUserLoggedIn("admin", true); +if(result) MessageBox.show("Admin user is logged-in!"); +else MessageBox.show("Admin user is not logged-in!"); + |
Retrieves the maximum number of allowed sessions, +under the multi session context, before login is disabled.
+Example
+1 +2 +3 | final var authManager = AuthManager.getInstance(); +final int result = authManager.getMaxSessions(); +MessageBox.show("The max number of allowed sessions is " + result + '!'); + |
Sets the maximum number of allowed sessions, +under the multi session context, before login is disabled. +To disable the limit set the value to a negative number. +To set the maximum number of allowed sessions, supply the following parameter:
+Example
+1 +2 +3 +4 +5 | final var authManager = AuthManager.getInstance(); +//Set max sessions to 30. +authManager.setMaxSessions(30); +//Block all session creation by setting to 0 +authManager.setMaxSessions(0); + |
Note
+Setting this value to a negative number will disable the limit +because a negative amount of sessions can never be created. +When the SessionManager is initialized the value is initially set to -1.
+Retrieves the current number of logged in sessions under the multi-user context.
+Example
+1 +2 +3 | final var authManager = AuthManager.getInstance(); +final int result = authManager.getSessionsCount(); +MessageBox.show("The max number of logged-in sessions is " + result + '!'); + |
Retrieves the current logged in sessions as a HashMap under the multi session context. +The returned HashMap key is the session username and the value is the related Session object.
+Example
+1 +2 +3 | final var authManager = AuthManager.getInstance(); +final Map<String, Session> sessions = authManager.getSessions(); +MessageBox.show("The max number of logged-in sessions is " + sessions.size() + '!'); + |
Retrieves the current session for the specified username, under the multi-user context.
+To retrieve a specific user session, supply the following parameters:
+Example
+1 +2 | final var authManager = AuthManager.getInstance(); +final Session session = authManager.getSession("admin", true); + |
Logs in a user, under the multi-user context, with the specified username, +without checking for valid credentials.
+To retrieve a specific user session, supply the following parameters:
+Warning
+This method does not validate credentials and should be used carefully. +With this method the user is immediately logged in as long as the user +exists and other checks pass (account unlocked, user role enabled and +password not expired). An example is listed below on how to implement this.
+Example
+1 +2 +3 +4 | final var authManager = AuthManager.getInstance(); +final boolean result = authManager.loginUser("admin", true); +if(result) MessageBox.show("User was logged-in successfully!"); +else MessageBox.show("User login failed!"); + |
This example adds password checking to verify credentials before login:
+Example
+1 +2 +3 +4 +5 +6 | final var authManager = AuthManager.getInstance(); +if(authManager.checkPasswordMatches("admin", "1234")) { + final boolean result = authManager.loginUser("admin", true); + if(result) MessageBox.show("User was logged-in successfully!"); + else MessageBox.show("User login failed!"); +} else MessageBox.show("User login failed!"); + |
Logs out the specified user from the single session context +and clears any set permissions. This method will return false +if no user is currently logged-in or if the specified user does not exist.
+To logout a specific user, supply the following parameters:
+Caution
+If the user was deleted from the database while logged in, then +the getUser event method that is accessible from the +assigned event handler will return null.
+Example
+1 +2 +3 +4 | final var authManager = AuthManager.getInstance(); +final boolean result = authManager.logoutUser("admin"); +if(result) MessageBox.show("User was logged-out successfully!"); +else MessageBox.show("User logout failed!"); + |
Checks if creation of a new session for the specified username is allowed +under the specified context. This method checks to see if the specified user +is already logged-in and if the second parameter is true checks to see if the +max sessions limit has been reached.
+To retrieve a specific user session, supply the following parameters:
+Example
+1 +2 +3 +4 | final var authManager = AuthManager.getInstance(); +final boolean result = authManager.isNewSessionAllowed("admin", true); +if(result) MessageBox.show("New User Session Is Allowed For Admin!"); +else MessageBox.show("New User Session Is Not Allowed For Admin!"); + |
To create a new user role use the createRole method supplying the following parameter:
+Note
+When using this method the UserRole object related to the new user role is returned. +If a user role already exists with the specified name the already existing UserRole +object is returned instead.
+Example
+1 +2 +3 | final var authManager = AuthManager.getInstance(); +final var newRole = authManager.createRole("moderator"); +newRole.modify().add("edit", "create", "read"); + |
You can manually create a new UserRole object and directly add that to the list. +To add an existing user role use the addExistingUserRole method supplying the following parameter:
+Example
+1 +2 +3 +4 | final var authManager = AuthManager.getInstance(); +final UserRole role = new UserRole("moderator"); +role.modify().add("edit", "create", "read"); +authManager.addExistingRole(role); + |
Retrieves an unmodifiable list of all user roles.
+Example
+1 +2 | final var authManager = AuthManager.getInstance(); +final Map<String, UserRole> roles = authManager.getRoles(); + |
Retrieves the specified user role.
+To retrieve a user role use the getUserRole method supplying the following parameter:
+Example
+1 +2 | final var authManager = AuthManager.getInstance(); +final UserRole role = authManager.getRole("admin"); + |
Checks if the current username has the specified permission.
+To check for the permission, supply the following parameter:
+The permission manager has the following built in permissions:
+Example
+1 +2 +3 +4 | final var authManager = AuthManager.getInstance(); +final boolean result = authManager.userHasPermission("admin"); +if(result) MessageBox.show("User Has Admin Permission!"); +else MessageBox.show("User Does Not Have Admin Permission!"); + |
This can also be done with a list of permissions:
+Example
+1 +2 +3 +4 | final var authManager = AuthManager.getInstance(); +final boolean result = authManager.userHasPermissions(new HashSet<>(Arrays.asList("admin", "edit"))); +if(result) MessageBox.show("User Has Specified Permissions!"); +else MessageBox.show("User Does Not Have Specified Permissions!"); + |
Retrieves a readonly immutable UserAccount object for each user +account and returns the list as a HashSet.
+Example
+1 +2 | final var authManager = AuthManager.getInstance(); +final HashSet<UserAccount> users = authManager.getUsersList(); + |
Retrieves a list of the user names in the database.
+Example
+1 +2 | final var authManager = AuthManager.getInstance(); +final HashSet<String> users = authManager.getUsernameList(); + |
Package Name: com.jgcomptech.tools.authz.PermissionManager
+The Permission Manager manages all role and user based permissions. +Permissions have a hierarchy and thus if a permission is assigned to a +user or role then all its child permissions are also assigned.
+The Permission Manager has the following built in permissions:
+The Permission Manager is a singleton class and on first use is initialized automatically. +The following code can be used to retrieve the instance: +
1 | final var manager = PermissionManager.getInstance(); + |
Adds new permission with the specified name and parent name, disabled by default.
+To add a new custom permission, supply the following parameters:
+Note
+If you want the new permission to not have a parent and want it to be root level +then set the Parent Name to null or an empty string.
+Example
+1 +2 | final var permissionsManager = PermissionManager.getInstance(); +permissionsManager.addCustomPermission("change_global_settings", "admin"); + |
Adds new permission with the specified name and parent name, enabled by default.
+To add a new custom permission, supply the following parameters:
+Note
+If you want the new permission to not have a parent and want it to be root level +then set the Parent Name to null or an empty string.
+Example
+1 +2 | final var permissionsManager = PermissionManager.getInstance(); +permissionsManager.addAndEnableCustomPermission("change_global_settings", "admin"); + |
Loading permissions enables or disables permissions in bulk. +There is 3 ways of doing this, enable all, disable all or apply a user role's permissions.
+Parameter must be true.
+Example
+1 +2 | final var permissionsManager = PermissionManager.getInstance(); +permissionsManager.loadPermissions(true); + |
Parameter must be false.
+Example
+1 +2 | final var permissionsManager = PermissionManager.getInstance(); +permissionsManager.loadPermissions(false); + |
When run all permissions are disabled and then the permissions assigned to +the specified user role are enabled.
+Parameter must be the name of a user role.
+Example
+1 +2 | final var permissionsManager = PermissionManager.getInstance(); +permissionsManager.loadPermissions("admin"); + |
Removes the specified permission.
+To remove a permission, supply the following parameter:
+Example
+1 +2 | final var permissionsManager = PermissionManager.getInstance(); +permissionsManager.removePermission("admin"); + |
Checks if the specified permission exists.
+To check if a permission exists, supply the following parameter:
+Example
+1 +2 +3 +4 | final var permissionsManager = PermissionManager.getInstance(); +final var result = permissionsManager.doesPermissionExist("admin"); +if(result) MessageBox.show("Permission Exists!"); +else MessageBox.show("Permission Not Found!"); + |
Enables the specified permission.
+To enable a permission, supply the following parameter:
+Example
+1 +2 | final var permissionsManager = PermissionManager.getInstance(); +permissionsManager.enablePermission("admin"); + |
Disables the specified permission.
+To disable a permission, supply the following parameter:
+Example
+1 +2 | final var permissionsManager = PermissionManager.getInstance(); +permissionsManager.disablePermission("admin"); + |
Checks if the specified permission is enabled.
+To check if a permission is enabled, supply the following parameter:
+Example
+1 +2 +3 +4 | final var permissionsManager = PermissionManager.getInstance(); +final var result = permissionsManager.isPermissionEnabled("admin"); +if(result) MessageBox.show("Permission Is Enabled!"); +else MessageBox.show("Permission Is Disabled!"); + |
Checks if the specified permission is disabled.
+To check if a permission is disabled, supply the following parameter:
+Example
+1 +2 +3 +4 | final var permissionsManager = PermissionManager.getInstance(); +final var result = permissionsManager.isPermissionDisabled("admin"); +if(result) MessageBox.show("Permission Is Disabled!"); +else MessageBox.show("Permission Is Enabled!"); + |
Retrieves an unmodifiable list of all the specified permission's child permissions.
+To retrieve a list of all child permissions, supply the following parameter:
+Example
+1 +2 | final var permissionsManager = PermissionManager.getInstance(); +final HashSet<String> children = permissionsManager.getPermissionChildren("admin"); + |
Retrieves an unmodifiable list of all permissions.
+Example
+1 +2 | final var permissionsManager = PermissionManager.getInstance(); +final Map<String, Permission> roles = permissionsManager.getPermissions(); + |
Retrieves an unmodifiable list of names of all permissions.
+Example
+1 +2 | final var permissionsManager = PermissionManager.getInstance(); +final Set<String> names = permissionsManager.getPermissionNames(); + |
Retrieves the specified permission.
+To retrieve a permission, supply the following parameter:
+Example
+1 +2 | final var permissionsManager = PermissionManager.getInstance(); +final UserRole role = permissionsManager.getPermission("admin"); + |
Package Name: com.jgcomptech.tools.authc.SessionManager
+Warning
+This class is embedded in the AuthManager,
+Subject, and UserManager classes
+and is not meant to be accessed directly except in advanced use cases.
+To access the AuthManager's embedded SessionManager instance use the following code:
+
1 +2 | final var authManager = AuthManager.getNewInstance(db); +final var sessionManager = authManager.getSessionManager(); + |
1 +2 | final var userManager = new UserManager(db); +final var sessionManager = userManager.getSessionManager(); + |
The Session Manager class contains all methods needed for managing +login sessions to allow users to login to your application. +This includes completing login via either using the UsernamePasswordToken +or requesting credentials from the user via a login dialog.
+The Session Manager is not a singleton class so a new instance needs to be initialized. +Since the Session Manager is a low level class it also requires an instance +of the User Manager in addition to the database instance. +The following code can be used to create a new instance:
+Example
+1 +2 +3 +4 | try(final var db = new Database("./mydb.db", DatabaseType.H2)) { + final var userManager = new UserManager(db); + final var sessionManager = new SessionManager(userManager); +} + |
An icon path and program name may also be optionally provided as parameters to change the +icon and program name for all login dialogs.
+A login Session is a temporary connection established by a user with a login, +until either the user or the application/service terminates the connection.
+The user must only provide credentials for authentication at the start +of a login session. The user is then authorized to use some or all of the services +offered by the application/service depending on the permissions assigned to the user.
+Within JUT there are two types of login sessions, single-user and multi-user. +All of the following documentation will be separated into two sections one +for each session type/context.
+Single-User sessions are meant for frontend login to an application. +These sessions can either be initiated via a UsernamePasswordToken or +via a login dialog. Only one user can be logged in at one time thus meaning +only one session can be open at a time. Some of the methods that are part +of the Session Manager can only be used under a Single-User context.
+Multi-User sessions are meant for backend login to an application, usually +via a remote client software. These sessions can only be initiated +via a UsernamePasswordToken. Almost an unlimited amount of users can be logged in +at one time and is only dependant on system resources and can be limited +via the setMaxSessions method. Some of the methods that are part +of the Session Manager can only be used under a Multi-User context.
+Returns the username of the currently logged in user under the single session context.
+Example
+1 +2 | final var sessionManager = new SessionManager(userManager); +final String currentUsername = sessionManager.getLoggedInUsername(); + |
Checks if a user is currently logged in under the single-user context.
+Example
+1 +2 +3 +4 | final var sessionManager = new SessionManager(userManager); +final boolean result = sessionManager.isUserLoggedIn(); +if(result) MessageBox.show("A user is logged-in!"); +else MessageBox.show("No user is logged-in!"); + |
Checks if the specified user is currently logged in under the single-user context. +To check if a specific user is logged-in, supply the following parameter:
+Example
+1 +2 +3 +4 | final var sessionManager = new SessionManager(userManager); +final boolean result = sessionManager.isUserLoggedIn("admin"); +if(result) MessageBox.show("Admin user is logged-in!"); +else MessageBox.show("Admin user is not logged-in!"); + |
Retrieves the current session for the specified username, under the single-user context.
+Example
+1 +2 | final var sessionManager = new SessionManager(userManager); +final Session currentSession = sessionManager.getSession(); + |
Retrieves the current session for the specified username, under the single-user context.
+To retrieve a specific user session, supply the following parameter:
+Example
+1 +2 | final var sessionManager = new SessionManager(userManager); +final Session session = sessionManager.getSession("admin"); + |
Shows the login dialog window to log a user into the single-user context. +The method returns true if a user was logged in successfully. +Fires either the sessionLoginSuccess or the sessionLoginFailure event +allowing the getUser method to be called by the assigned EventHandler. +If the user does not exist, getUser will return null.
+To show the login dialog, supply the following parameter:
+Example
+1 +2 +3 +4 | final var sessionManager = new SessionManager(userManager); +final boolean result = sessionManager.showLoginWindow(true); +if(result) MessageBox.show("User was logged-in successfully!"); +else MessageBox.show("User login failed!"); + |
Logs in a user, under the single-user context, with the specified username, +without checking for valid credentials.
+To retrieve a specific user session, supply the following parameter:
+Warning
+This method does not validate credentials and should not be used except +in advanced use cases. A example case could be to login a user during unit testing. +The method showLoginWindow should be used instead. With this method +the user is immediately logged in as long as the user exists and other +checks pass (account unlocked, user role enabled and password not expired).
+Example
+1 +2 +3 +4 | final var sessionManager = new SessionManager(userManager); +final boolean result = sessionManager.loginUser("admin"); +if(result) MessageBox.show("User was logged-in successfully!"); +else MessageBox.show("User login failed!"); + |
Logs out the specified user from the single session context +and clears any set permissions. This method will return false +if no user is currently logged-in.
+Caution
+If the user was deleted from the database while logged in, then +the getUser event method that is accessible from the +assigned event handler will return null.
+Example
+1 +2 +3 +4 | final var sessionManager = new SessionManager(userManager); +final boolean result = sessionManager.logoutUser(); +if(result) MessageBox.show("User was logged-out successfully!"); +else MessageBox.show("User logout failed!"); + |
Logs out the specified user from the single session context +and clears any set permissions. This method will return false +if no user is currently logged-in or if the specified user does not exist.
+To logout a specific user, supply the following parameter:
+Caution
+If the user was deleted from the database while logged in, then +the getUser event method that is accessible from the +assigned event handler will return null.
+Example
+1 +2 +3 +4 | final var sessionManager = new SessionManager(userManager); +final boolean result = sessionManager.logoutUser("admin"); +if(result) MessageBox.show("User was logged-out successfully!"); +else MessageBox.show("User logout failed!"); + |
Retrieves the user role of the currently logged in user under +the single user context. If no user is currently logged-in then this +method will return null.
+Example
+1 +2 | final var sessionManager = new SessionManager(userManager); +final UserRole result = sessionManager.getLoggedInUserRole(); + |
Checks if an admin user is currently logged in under the single user context.
+Example
+1 +2 +3 +4 | final var sessionManager = new SessionManager(userManager); +final boolean result = sessionManager.isAdminLoggedIn(); +if(result) MessageBox.show("An Admin user is logged-in!"); +else MessageBox.show("An Admin user is not logged-in!"); + |
Requests an admin user to authenticate to override permissions by +showing the login dialog using the single session context. During this +process the admin user is not actually logged-in, instead just their +username, password and user role is verified. Returns true if +admin override succeeded and false if the cancel button was pressed +or if override failed. +Fires either the sessionLoginSuccess or the sessionLoginFailure event +allowing the getUser method to be called by the assigned EventHandler. +If the user does not exist, getUser will return null.
+To show the login dialog, supply the following parameter:
+Example
+1 +2 +3 +4 | final var sessionManager = new SessionManager(userManager); +final boolean result = sessionManager.getAdminOverride(); +if(result) MessageBox.show("Admin override succedded!"); +else MessageBox.show("Admin override failed!"); + |
Requests that the currently logged in username to re-login using +the single session context. During this process the user is not +actually logged-in again, instead just their username and password is +verified. Returns true if the user verification succeeded and false +if the cancel button was pressed or if override failed. +Fires either the sessionLoginSuccess or the sessionLoginFailure event +allowing the getUser method to be called by the assigned EventHandler. +If the user does not exist, getUser will return null.
+To show the login dialog, supply the following parameter:
+Example
+1 +2 +3 +4 | final var sessionManager = new SessionManager(userManager); +final boolean result = sessionManager.getUserVerification(); +if(result) MessageBox.show("User verification succedded!"); +else MessageBox.show("User verification failed!"); + |
Checks that the logged-in user, under the single session context, +is an admin and if not, requests an admin override. +During this process the user is not actually logged-in again, +instead their username, password and user role is just verified.
+Example
+1 +2 +3 +4 | final var sessionManager = new SessionManager(userManager); +final boolean result = sessionManager.requireAdmin(); +if(result) MessageBox.show("Admin check passed!"); +else MessageBox.show("Admin check failed!"); + |
Checks that the logged-in user, under the single session context, +is an admin, if true, prompts the admin to re-login and +if false, requests an admin override. +During this process the user is not actually logged-in again, +instead their username, password and user role is just verified.
+Example
+1 +2 +3 +4 | final var sessionManager = new SessionManager(userManager); +final boolean result = sessionManager.requireAdmin(); +if(result) MessageBox.show("Admin check passed!"); +else MessageBox.show("Admin check failed!"); + |
Checks if the specified user is currently logged in under the multi-user context. +To check if a specific user is logged-in, supply the following parameters:
+Example
+1 +2 +3 +4 | final var sessionManager = new SessionManager(userManager); +final boolean result = sessionManager.isUserLoggedIn("admin", true); +if(result) MessageBox.show("Admin user is logged-in!"); +else MessageBox.show("Admin user is not logged-in!"); + |
Retrieves the maximum number of allowed sessions, +under the multi session context, before login is disabled.
+Example
+1 +2 +3 | final var sessionManager = new SessionManager(userManager); +final int result = sessionManager.getMaxSessions(); +MessageBox.show("The max number of allowed sessions is " + result + '!'); + |
Sets the maximum number of allowed sessions, +under the multi session context, before login is disabled. +To disable the limit set the value to a negative number. +To set the maximum number of allowed sessions, supply the following parameter:
+Example
+1 +2 +3 +4 +5 | final var sessionManager = new SessionManager(userManager); +//Set max sessions to 30. +sessionManager.setMaxSessions(30); +//Block all session creation by setting to 0 +sessionManager.setMaxSessions(0); + |
Note
+Setting this value to a negative number will disable the limit +because a negative amount of sessions can never be created. +When the SessionManager is initialized the value is initially set to -1.
+Retrieves the current number of logged in sessions under the multi-user context.
+Example
+1 +2 +3 | final var sessionManager = new SessionManager(userManager); +final int result = sessionManager.getSessionsCount(); +MessageBox.show("The max number of logged-in sessions is " + result + '!'); + |
Retrieves the current logged in sessions as a HashMap under the multi session context. +The returned HashMap key is the session username and the value is the related Session object.
+Example
+1 +2 +3 | final var sessionManager = new SessionManager(userManager); +final Map<String, Session> sessions = sessionManager.getSessions(); +MessageBox.show("The max number of logged-in sessions is " + sessions.size() + '!'); + |
Retrieves the current session for the specified username, under the multi-user context.
+To retrieve a specific user session, supply the following parameters:
+Example
+1 +2 | final var sessionManager = new SessionManager(userManager); +final Session session = sessionManager.getSession("admin", true); + |
Logs in a user, under the multi-user context, with the specified username, +without checking for valid credentials.
+To retrieve a specific user session, supply the following parameters:
+Warning
+This method does not validate credentials and should be used carefully. +With this method the user is immediately logged in as long as the user +exists and other checks pass (account unlocked, user role enabled and +password not expired). An example is listed below on how to implement this.
+Example
+1 +2 +3 +4 | final var sessionManager = new SessionManager(userManager); +final boolean result = sessionManager.loginUser("admin", true); +if(result) MessageBox.show("User was logged-in successfully!"); +else MessageBox.show("User login failed!"); + |
This example adds password checking to verify credentials before login:
+Example
+1 +2 +3 +4 +5 +6 +7 | final var userManager = new UserManager(db); +final var sessionManager = new SessionManager(userManager); +if(userManager.checkPasswordMatches("admin", "1234")) { + final boolean result = sessionManager.loginUser("admin", true); + if(result) MessageBox.show("User was logged-in successfully!"); + else MessageBox.show("User login failed!"); +} else MessageBox.show("User login failed!"); + |
Logs out the specified user from the single session context +and clears any set permissions. This method will return false +if no user is currently logged-in or if the specified user does not exist.
+To logout a specific user, supply the following parameters:
+Caution
+If the user was deleted from the database while logged in, then +the getUser event method that is accessible from the +assigned event handler will return null.
+Example
+1 +2 +3 +4 | final var sessionManager = new SessionManager(userManager); +final boolean result = sessionManager.logoutUser("admin"); +if(result) MessageBox.show("User was logged-out successfully!"); +else MessageBox.show("User logout failed!"); + |
Checks if creation of a new session for the specified username is allowed +under the specified context. This method checks to see if the specified user +is already logged-in and if the second parameter is true checks to see if the +max sessions limit has been reached.
+To retrieve a specific user session, supply the following parameters:
+Example
+1 +2 +3 +4 | final var sessionManager = new SessionManager(userManager); +final boolean result = sessionManager.isNewSessionAllowed("admin", true); +if(result) MessageBox.show("New User Session Is Allowed For Admin!"); +else MessageBox.show("New User Session Is Not Allowed For Admin!"); + |
Package Name: com.jgcomptech.tools.authc.Subject
+A Subject is just fancy security term that basically means a security-specific ‘view’ of an application user. +In JUT the Subject object handles the management of one specific user account. +The subject allows management of the user settings, role, permissions, and sessions.
+A Subject object instance can be created by providing an instance of the Auth Manager or by +calling the getSubject method on the Auth Manager. Multiple subject instances can exist but multiple +instances are only useful if using the multi-user context.
+1 +2 | final var manager = AuthManager.getInstance(); +final Subject subject = manager.getSubject(); + |
1 +2 | final var manager = AuthManager.getInstance(); +final Subject subject = new Subject(manager); + |
A new instance of a subject is considered anonymous until a user is logged-in. +A subject login requires a UsernamePasswordToken to pass in the user's credentials. +For security reasons, passwords supplied to the UsernamePasswordToken must be converted to a char array.
+There is 4 methods supplied for logging in a user:
+To login a user, supply the following parameter:
+Example
+1 +2 +3 +4 +5 | final var subject = AuthManager.getInstance().getSubject(); +final var token = new UsernamePasswordToken("admin", "1234".toCharArray()); +final boolean result = subject.login(token); +if(result) MessageBox.show("Login Succeeded!"); +else MessageBox.show("Login Failed!"); + |
To login a user, supply the following parameters:
+Multi-User Context Example:
+Example
+1 +2 +3 +4 +5 | final var subject = AuthManager.getInstance().getSubject(); +final var token = new UsernamePasswordToken("admin", "1234".toCharArray()); +final boolean result = subject.login(token, true); +if(result) MessageBox.show("Login Succeeded!"); +else MessageBox.show("Login Failed!"); + |
For previous token to be saved, setRememberMe needs to be set to true during token creation. +Also if no token is saved CredentialsException will be thrown. +To login a user, no parameters are needed.
+Example
+1 +2 +3 +4 +5 | final var subject = AuthManager.getInstance().getSubject(); +final var token = new UsernamePasswordToken("admin", "1234".toCharArray()); +final boolean result = subject.login(); +if(result) MessageBox.show("Login Succeeded!"); +else MessageBox.show("Login Failed!"); + |
For previous token to be saved, setRememberMe needs to be set to true during token creation. +Also if no token is saved CredentialsException will be thrown. +To login a user, supply the following parameters:
+Multi-User Context Example:
+Example
+1 +2 +3 +4 +5 | final var subject = AuthManager.getInstance().getSubject(); +final var token = new UsernamePasswordToken("admin", "1234".toCharArray()); +final boolean result = subject.login(true); +if(result) MessageBox.show("Login Succeeded!"); +else MessageBox.show("Login Failed!"); + |
When logging out a user, unless the setRememberMe is set to true during the token +creation, the subject username and token are cleared thus making the subject anonymous again. +This method will return false if the user is not already logged-in.
+There is 4 methods supplied for logging out a user:
+To logout a user, no parameters are needed.
+Example
+1 +2 +3 +4 | final var subject = AuthManager.getInstance().getSubject(); +final boolean result = subject.logout(); +if(result) MessageBox.show("Logout Succeeded!"); +else MessageBox.show("Logout Failed!"); + |
To logout a user, supply the following parameter:
+Multi-User Context Example:
+Example
+1 +2 +3 +4 | final var subject = AuthManager.getInstance().getSubject(); +final boolean result = subject.login(true); +if(result) MessageBox.show("Logout Succeeded!"); +else MessageBox.show("Logout Failed!"); + |
When a session is opened a timer starts recording the duration the session was open. +This duration can be retrieved after a session is closed with the subject.
+There is 3 methods supplied for returning this value:
+Example
+1 +2 | final var subject = AuthManager.getInstance().getSubject(); +final Duration duration = subject.getLastSessionDuration(); + |
Example
+1 +2 | final var subject = AuthManager.getInstance().getSubject(); +final String duration = subject.getLastSessionDurationStringFull(); + |
Example
+1 +2 | final var subject = AuthManager.getInstance().getSubject(); +final String duration = subject.getLastSessionDurationString(); + |
Checks if the subject is anonymous.
+Example
+1 +2 +3 +4 | final var subject = AuthManager.getInstance().getSubject(); +final boolean result = subject.isAnonymous(); +if(result) MessageBox.show("Subject Is Anonymous!"); +else MessageBox.show("Subject Is Not Anonymous!"); + |
Checks if the subject is anonymous.
+Example
+1 +2 +3 +4 | final var subject = AuthManager.getInstance().getSubject(); +final boolean result = subject.isAuthenticated(); +if(result) MessageBox.show("Subject Is Anonymous!"); +else MessageBox.show("Subject Is Not Anonymous!"); + |
Checks if the subject is anonymous.
+To check status, supply the following parameter:
+Multi-User Context Example:
+Example
+1 +2 +3 +4 | final var subject = AuthManager.getInstance().getSubject(); +final boolean result = subject.isAuthenticated(true); +if(result) MessageBox.show("Subject Is Anonymous!"); +else MessageBox.show("Subject Is Not Anonymous!"); + |
Retrieves the current logged in session or returns null if no session is open.
+Example
+1 +2 | final var subject = AuthManager.getInstance().getSubject(); +final Session session = subject.getSession(); + |
Retrieves the current logged in session or returns null if no session is open.
+To retrieve the current session, supply the following parameter:
+Multi-User Context Example:
+Example
+1 +2 | final var subject = AuthManager.getInstance().getSubject(); +final Session session = subject.getSession(true); + |
Checks to see if the username and token are set to be saved after logout. +For credentials to be saved, setRememberMe needs to be set to true during token creation.
+Example
+1 +2 +3 +4 | final var subject = AuthManager.getInstance().getSubject(); +final boolean result = subject.isRemembered(); +if(result) MessageBox.show("Subject Credentials Is Remembered!"); +else MessageBox.show("Subject Credentials Is Not Remembered!"); + |
Retrieves the current username. Returns null if the subject is anonymous.
+Example
+1 +2 +3 | final var subject = AuthManager.getInstance().getSubject(); +final boolean result = subject.getUsername(); +MessageBox.show("The Current Username Is " + result + '!'); + |
Sets a new password for the current user.
+To set a new password, supply the following parameter:
+Example
+1 +2 | final var subject = AuthManager.getInstance().getSubject(); +subject.setPassword("pass"); + |
Retrieves the UserRole object assigned to the current user.
+Example
+1 +2 | final var subject = AuthManager.getInstance().getSubject(); +final UserRole role = subject.getUserRole(); + |
Sets the user role assigned to the specified user. +To set the user role, supply the following parameter:
+The user role can be a text string or one of the +items from the UserRoleManager.SystemUserRoles enum.
+The UserRoleManager.SystemUserRoles list contains:
+Note
+The NONE user role can never have permissions assigned to it. +Using the NONE user role is a quick way to remove all permissions from a user.
+Example
+1 +2 | final var subject = AuthManager.getInstance().getSubject(); +subject.setUserRole(UserRoleManager.SystemUserRoles.ADMIN); + |
Checks if the current username has the specified permission.
+To check for the permission, supply the following parameter:
+The permission manager has the following built in permissions:
+Example
+1 +2 +3 +4 | final var subject = AuthManager.getInstance().getSubject(); +final boolean result = subject.hasPermission("admin"); +if(result) MessageBox.show("User Has Admin Permission!"); +else MessageBox.show("User Does Not Have Admin Permission!"); + |
Example
+1 +2 +3 +4 +5 | final var subject = AuthManager.getInstance().getSubject(); +final HashSet<String> list = new HashSet<>(Arrays.asList("admin", "edit")); +final boolean result = subject.hasPermissions(list); +if(result) MessageBox.show("User Has Specified Permissions!"); +else MessageBox.show("User Does Not Have Specified Permissions!"); + |
Example
+1 +2 +3 +4 | final var subject = AuthManager.getInstance().getSubject(); +final boolean result = subject.hasPermissions("admin", "edit"); +if(result) MessageBox.show("User Has Specified Permissions!"); +else MessageBox.show("User Does Not Have Specified Permissions!"); + |
Returns the date and time the user was initially created.
+There is two methods to retrieving the user creation date. +The date can either be returned as a LocalDateTime object or as a formatted string.
+Example
+1 +2 +3 +4 | final var subject = AuthManager.getInstance().getSubject(); +final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"); +final LocalDateTime result = subject.getUserCreationDate(); +MessageBox.show("Account Was Created On " + result.format(formatter)); + |
To retrieve the user creation date, supply the following parameter:
+Example
+1 +2 +3 | final var subject = AuthManager.getInstance().getSubject(); +final String result = subject.getUserCreationDate("yyyy-MM-dd HH:mm"); +MessageBox.show("Account Was Created On " + result); + |
A locked user account prevents login and prevents any new sessions +to be opened for the user. Locking an user account may be needed for numerous reasons.
+Example
+1 +2 +3 +4 | final var subject = AuthManager.getInstance().getSubject(); +final boolean result = subject.isUserLocked(); +if(result) MessageBox.show("Admin Account Is Locked!"); +else MessageBox.show("Admin Account Is Unlocked!"); + |
A locked user account prevents login and prevents any new sessions +to be opened for the user. Locking an user account may be needed for numerous reasons.
+Example
+1 +2 +3 +4 +5 | final var subject = AuthManager.getInstance().getSubject(); +//Lock The Account +subject.lockUser(); +//Unlock The Account +subject.unlockUser(); + |
Setting a password to expire after a specified date allows the prevention of login and +new session creation if the password is expired. Setting a password to expire could +be used to force regular password changes or allow for temporary accounts to be made.
+Example
+1 +2 +3 +4 | final var subject = AuthManager.getInstance().getSubject(); +final boolean result = subject.isPasswordExpired(); +if(result) MessageBox.show("Admin Account Password Is Expired!"); +else MessageBox.show("Admin Account Password Is Not Expired!"); + |
Setting a password to expire after a specified date allows the prevention of login and +new session creation if the password is expired. Setting a password to expire could +be used to force regular password changes or allow for temporary accounts to be made.
+Example
+1 +2 +3 +4 | final var subject = AuthManager.getInstance().getSubject(); +final boolean result = subject.isPasswordSetToExpire(); +if(result) MessageBox.show("Admin Account Password Has An Expiration Date!"); +else MessageBox.show("Admin Account Password Does Not Have An Expiration Date!"); + |
Setting a password to expire after a specified date allows the prevention of login and +new session creation if the password is expired. Setting a password to expire could +be used to force regular password changes or allow for temporary accounts to be made.
+There is two methods to retrieving the password expiration date. +The date can either be returned as a LocalDateTime object or as a formatted string.
+Note
+If the expiration date has been disabled or was never enabled this method will +still return an expiration date. For logistical database reasons a date still +has to be set. So the date will be set to 1000 years after the user account +was created or 1000 years after the date when the password expiration date +was last disabled. This number can be updated if any bugs occur but it seems +like 1000 years is far enough in the future to not cause any problems.
+Example
+1 +2 +3 +4 | final var subject = AuthManager.getInstance().getSubject(); +final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"); +final LocalDateTime result = subject.getPasswordExpirationDate(); +MessageBox.show("Admin Account Password Expires At " + result.format(formatter)); + |
To retrieve the password expiration date, supply the following parameter:
+Example
+1 +2 +3 | final var subject = AuthManager.getInstance().getSubject(); +final String result = subject.getPasswordExpirationDate("yyyy-MM-dd HH:mm"); +MessageBox.show("Admin Account Password Expires At " + result); + |
Setting a password to expire after a specified date allows the prevention of login and +new session creation if the password is expired. Setting a password to expire could +be used to force regular password changes or allow for temporary accounts to be made.
+To set a password expiration date, supply the following parameter:
+Example
+1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +10 +11 +12 +13 | final var subject = AuthManager.getInstance().getSubject(); +//Sets the date to now +subject.setPasswordExpirationDate(LocalDateTime.now()); +//Sets the date to 30 days from today +subject.setPasswordExpirationDate(LocalDateTime.now().plusDays(30)); +//First Method To Set the date to January 1st, 2019 at 8am +subject.setPasswordExpirationDate(LocalDateTime.of(2019, 1, 1, 8, 0)); +//Second Method To Set the date to January 1st of 2019 at 8am +subject.setPasswordExpirationDate(LocalDateTime.of(2018, Month.JANUARY, 1, 8, 0)); +//Third Method To Set the date to January 1st of 2019 at 8am +final String str = "2019-01-01 08:00"; +final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"); +subject.setPasswordExpirationDate(LocalDateTime.parse(str, formatter)); + |
Setting a password to expire after a specified date allows the prevention of login and +new session creation if the password is expired. Setting a password to expire could +be used to force regular password changes or allow for temporary accounts to be made.
+Example
+1 +2 | final var subject = AuthManager.getInstance().getSubject(); +authManager.disablePasswordExpiration(); + |
This method allows you to check if the supplied password matches +the user's stored password in the database. +To verify the password, supply the following parameter:
+Example
+1 +2 +3 +4 | final var subject = AuthManager.getInstance().getSubject(); +final boolean result = subject.checkPasswordMatches("1234"); +if(result) MessageBox.show("Password Matches!"); +else MessageBox.show("Password Does Not Match!"); + |
Package Name: com.jgcomptech.tools.authc.UserManager
+Warning
+This class is embedded in the AuthManager
+and Subject classes and is not meant to be
+accessed directly except in advanced use cases.
+To access the AuthManager's embedded UserManager instance use the following code:
+
1 +2 | final var authManager = AuthManager.getNewInstance(db); +final var userManager = authManager.getUserManager(); + |
The User Manager manages all user accounts in the database. +This is where all the native SQL code is automatically +generated using the Database Builder classes.
+The User Manager is not a singleton class so a new instance needs to be initialized. +When a new instance is created a "users" table is automatically created in the specified database. +The following code can be used to create a new instance:
+Example
+1 +2 +3 | try(final var db = new Database("./mydb.db", DatabaseType.H2)) { + final var userManager = new UserManager(db); +} + |
An icon path and program name may also be optionally provided as parameters to change the +icon and program name for all login dialogs.
+To create a new user use the createUser method supplying the following parameters:
+The user role can be a text string or one of the +items from the UserRoleManager.SystemUserRoles enum.
+The UserRoleManager.SystemUserRoles list contains:
+Note
+The NONE user role can never have permissions assigned to it. +Using the NONE user role is a quick way to remove all permissions from a user.
+Example
+1 +2 +3 | final var userManager = new UserManager(db); +userManager.createUser("admin", "1234", UserRoleManager.SystemUserRoles.ADMIN); +userManager.createUser("editor", "1234", UserRoleManager.SystemUserRoles.EDITOR); + |
To delete an existing user use the deleteUser method supplying the following parameter:
+Example
+1 +2 | final var userManager = new UserManager(db); +userManager.deleteUser("admin"); + |
Retrieves a readonly immutable UserAccount object. +To retrieve the object, supply the following parameter:
+Example
+1 +2 | final var userManager = new UserManager(db); +final UserAccount user = userManager.getUser("admin"); + |
Checks to see if the specified user account exists. +To check a user account, supply the following parameter:
+Example
+1 +2 +3 +4 | final var userManager = new UserManager(db); +final boolean result = userManager.userExists("admin"); +if(result) MessageBox.show("Admin User Exists!"); +else MessageBox.show("Admin User Does Not Exist!"); + |
Retrieves the UserRole object assigned to the specified user. +To retrieve the object, supply the following parameter:
+Example
+1 +2 | final var userManager = new UserManager(db); +final UserRole userRole = userManager.getUserRole("admin"); + |
Sets the user role assigned to the specified user. +To set the user role, supply the following parameters:
+The user role can be a text string or one of the +items from the UserRoleManager.SystemUserRoles enum.
+The UserRoleManager.SystemUserRoles list contains:
+Note
+The NONE user role can never have permissions assigned to it. +Using the NONE user role is a quick way to remove all permissions from a user.
+Example
+1 +2 | final var userManager = new UserManager(db); +userManager.setUserRole("admin", UserRoleManager.SystemUserRoles.ADMIN); + |
Sets the password assigned to the specified user. +To set the new password, supply the following parameters:
+Note
+If you want to prevent a user from setting an empty password +or to check for password complexity requirements those checks need +to be implemented separately. This method allows empty passwords. +Even if the password is empty, the empty password is still encrypted +with a random secure salt just like any supplied password would be.
+Example
+1 +2 | final var userManager = new UserManager(db); +userManager.setPassword("admin", "newPass"); + |
This method allows you to check if the supplied password matches +the user's stored password in the database. +To verify the password, supply the following parameters:
+Example
+1 +2 +3 +4 | final var userManager = new UserManager(db); +final boolean result = userManager.checkPasswordMatches("admin", "1234"); +if(result) MessageBox.show("Password Matches!"); +else MessageBox.show("Password Does Not Match!"); + |
A locked user account prevents login and prevents any new sessions +to be opened for the user. Locking an user account may be needed for numerous reasons.
+To lock and unlock an account, supply the following parameters:
+Example
+1 +2 +3 +4 +5 | final var userManager = new UserManager(db); +//Lock The Account +userManager.setLockStatus("admin", true); +//Unlock The Account +userManager.setLockStatus("admin", false); + |
Setting a password to expire after a specified date allows the prevention of login and +new session creation if the password is expired. Setting a password to expire could +be used to force regular password changes or allow for temporary accounts to be made.
+To set a password expiration date, supply the following parameters:
+Example
+1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +10 +11 +12 +13 | final var userManager = new UserManager(db); +//Sets the date to now +userManager.setPasswordExpirationDate("admin", LocalDateTime.now()); +//Sets the date to 30 days from today +userManager.setPasswordExpirationDate("admin", LocalDateTime.now().plusDays(30)); +//First Method To Set the date to January 1st, 2019 at 8am +userManager.setPasswordExpirationDate("admin", LocalDateTime.of(2019, 1, 1, 8, 0)); +//Second Method To Set the date to January 1st of 2019 at 8am +userManager.setPasswordExpirationDate("admin", LocalDateTime.of(2018, Month.JANUARY, 1, 8, 0)); +//Third Method To Set the date to January 1st of 2019 at 8am +final String str = "2019-01-01 08:00"; +final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"); +userManager.setPasswordExpirationDate("admin", LocalDateTime.parse(str, formatter)); + |
Setting a password to expire after a specified date allows the prevention of login and +new session creation if the password is expired. Setting a password to expire could +be used to force regular password changes or allow for temporary accounts to be made.
+To disable password expiration for a user, supply the following parameter:
+Example
+1 +2 | final var userManager = new UserManager(db); +userManager.disablePasswordExpiration("admin"); + |
The getUsersList method retrieves a readonly immutable UserAccount object for each user +account and returns the list as a HashSet.
+Example
+1 +2 | final var userManager = new UserManager(db); +final HashMap<UserAccount> users = userManager.getUsersList(); + |
Retrieves a list of the user names in the database.
+Example
+1 +2 | final var authManager = AuthManager.getInstance(); +final HashSet<String> users = authManager.getUsernameList(); + |
Package Name: com.jgcomptech.tools.authc.UserRoleManager
+The User Role Manager class contains all methods needed for managing +User Roles that can be assigned to a User Account.
+Role-based Access Control (RBAC) is a common approach to restricting system access to authorized users. +A user role defines permissions for a user to perform a group of tasks. +Although every role has a predefined set of permissions, +new permissions can be added and removed from each role. +New custom roles can also be created as a new role or a copy of an existing role.
+The base permissions are (Descriptions are based on a CMS implementation and are just examples):
+The base user roles are (Descriptions are based on a CMS implementation and are just examples):
+Note
+Currently JUT is designed to allow a user to be assigned only one user role. +This will be changing in future releases to allow for assignment of multiple roles.
+The User Role Manager is a singleton class and on first use is initialized automatically. +The following code can be used to retrieve the instance: +
1 | final var manager = UserRoleManager.getInstance(); + |
To create a new user role use the createUserRole method supplying the following parameter:
+Note
+When using this method the UserRole object related to the new user role is returned. +If a user role already exists with the specified name the already existing UserRole +object is returned instead.
+Example
+1 +2 +3 | final var userRoleManager = UserRoleManager.getInstance(); +final var newRole = userRoleManager.createUserRole("moderator"); +newRole.modify().add("edit", "create", "read"); + |
You can manually create a new UserRole object and directly add that to the list. +To add an existing user role use the addExistingUserRole method supplying the following parameter:
+Example
+1 +2 +3 +4 | final var userRoleManager = UserRoleManager.getInstance(); +final UserRole role = new UserRole("moderator"); +role.modify().add("edit", "create", "read"); +userRoleManager.addExistingUserRole(role); + |
Retrieves an unmodifiable list of all user roles.
+Example
+1 +2 | final var userRoleManager = UserRoleManager.getInstance(); +final Map<String, UserRole> roles = userRoleManager.getUserRoles(); + |
Retrieves the specified user role.
+To retrieve a user role use the getUserRole method supplying the following parameter:
+Example
+1 +2 | final var userRoleManager = UserRoleManager.getInstance(); +final UserRole role = userRoleManager.getUserRole("admin"); + |
Package Names:
+ +The JUT Auth System is a set of classes that allow for authentication and authorization. +This system uses the Database class to connect to a database to store all account info.
+Package Names:
+ +Contains tools to communicate with a database using JDBC. +The goal behind this class is to allow connection with the +database and creation of auto-generated SQL statements so manually +creating native SQL code is not needed. This includes initial table creation +which is not normally included in most database frameworks.
+Info
+This page is a WIP and more documentation is coming soon.
+Package Name: com.jgcomptech.tools.dialogs
+Contains JavaFX dialogs to use in your application. +Platform.runLater is not needed to be called since all dialogs use a +wrapper class that automatically calls runLater if it is needed.
+Note
+The MessageBox.Show() method is used throughout this documentation to show messages + for demo example purposes. These code statements are not required for the example + to function.
+Info
+This page is a WIP and more documentation is coming soon.
+Package Name: com.jgcomptech.tools.events
+Contains classes to manage custom events. +All events are managed through the Event Manager class.
+Note
+The JavaFX EventHandler class is not the same as the included +EventHandler class and is not compatible with the JUT event system.
+Info
+This page is a WIP and more documentation is coming soon.
+Package Name: com.jgcomptech.tools.HWInfo
+The HWInfo class returns information about the currently installed hardware.
+Info
+This page is a WIP and more documentation is coming soon.
+Package Name: com.jgcomptech.tools.OSInfo
+The OSInfo class returns information about the current operating system.
+Info
+This page is a WIP and more documentation is coming soon.
+Package Name: com.jgcomptech.tools.SecurityTools
+Contains methods surrounding hashing and encryption. Includes methods using MD5, SHA1, SHA256, SHA384, SHA512 and BCrypt. Also includes encryption/decryption with RSA.
+Info
+This page is a WIP and more documentation is coming soon.
+JUT contains many utils classes to allow for extension and simplification of many aspects of Java.
+Info
+This page is a WIP and more documentation is coming soon.
+NOTE: Some API changes have occurred: +* Moved Database.Users class to UsersManager class +* Made Database class constructor auto-connect to database +* Deprecated DatabaseConnectionInfo fields and replaced with getters/setters +* Made all utility classes final and have private constructors +* Made all method parameters and most existing classes final +* Added period to first line of all javadoc comments +* Updated SecurityTools.getFileHash to use Apache Commons Codec Library (Java 9 Fix) +* Renamed ReInitalizeHW to ReInitializeHW +* Renamed ReInitalizeOS to ReInitializeOS
+Java Ultimate Tools, or JUT for short, is a large powerful and flexible open-source repository for use in any Java program.
+NOTE: This Project Has Now Been Updated To Use Java 10!!!
+Want to contribute? Great! +Any help with development is greatly appreciated. If you want to add something or fix any issues please submit a pull request and if it is helpful it may be merged. Please check out our Code of Conduct for Contributors.
+The documentation for JUT is currently a work in progress and new changes will be occurring soon.
+The JavaDoc info is hosted at the following 2 locations:
+
github.io(Current GitHub Branch) - stored in the doc folder in the project repository on Github
+
javadoc.io(Current Maven Release) - the released JavaDoc jar on Maven Central
The changelog can be found here.
+If you are familiar with Maven, add the following XML +fragments into your pom.xml file. With those settings, your Maven will automatically download our library into your local Maven repository, since our libraries are synchronized with the Maven central repository.
+1 +2 +3 +4 +5 +6 +7 | <dependencies> + <dependency> + <groupId>com.jgcomptech.tools</groupId> + <artifactId>java-ultimate-tools</artifactId> + <version>1.5.1</version> + </dependency> +</dependencies> + |
1 | compile 'com.jgcomptech.tools:java-ultimate-tools:1.5.1' + |
1 | compile(group = "com.jgcomptech.tools", name = "java-ultimate-tools", version = "1.5.1") + |
1 | libraryDependencies += "com.jgcomptech.tools" % "java-ultimate-tools" % "1.5.1" + |
1 | <dependency org="com.jgcomptech.tools" name="java-ultimate-tools" rev="1.5.1" /> + |
1 +2 +3 | @Grapes( + @Grab(group='com.jgcomptech.tools', module='java-ultimate-tools', version='1.5.1') +) + |
JavaUltimateTools by J&G CompTech is licensed under a Creative Commons Attribution 4.0 International License.
+©2018 J&G CompTech
+ + + + + + + + + +