Class SessionManager


  • public final class SessionManager
    extends EventTarget<SessionEvent>
    Manages a login sessions to allow a users to login to your application.
    Since:
    1.4.0, 1.5.0 merged with MultiSessionManager
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      class  SessionManager.EventListeners
      Contains methods for all session related event handlers.
    • Constructor Summary

      Constructors 
      Constructor Description
      SessionManager​(UserManager userManager)
      Creates an instance of the SessionManager.
      SessionManager​(UserManager userManager, java.lang.String appIconPath)
      Creates an instance of the SessionManager.
      SessionManager​(UserManager userManager, java.lang.String appIconPath, java.lang.String programName)
      Creates an instance of the SessionManager.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.lang.Object clone()  
      void disableDebugLogging()
      Disables basic event handlers with System.Out logging for all events.
      void enableDebugLogging()
      Enables basic event handlers with System.Out logging for all events.
      void enableDefaultErrorMessages()
      Enables basic default error messages to be used for the login dialog.
      boolean getAdminOverride​(boolean retryLoginOnFailure)
      Requests an admin user to authenticate to override permissions using the single session context.
      java.lang.String getAppIconPath()
      Returns the path to the icon to use for all login dialogs.
      SessionManager.EventListeners getEventListeners()
      Returns a new instance of the EventListeners class that contains methods for all session related event handlers.
      @Nullable java.lang.String getLoggedInUsername()
      Returns the username of the currently logged in user under the single session context.
      @Nullable UserRole getLoggedInUserRole()
      Returns the user role of the currently logged in user under the single user context.
      LoginErrorMessages getLoginErrorMessages()
      Returns the text to use for the error message when errors occur during login.
      int getMaxSessions()
      Returns the maximum number of allowed sessions, under the multi session context, before login is disabled.
      java.lang.String getProgramName()
      Returns the name of the program to use in all login dialogs for the window title.
      Session getSession()
      Returns the current session for the currently logged in username under the single session context.
      Session getSession​(java.lang.String username)
      Returns the current session for the specified username under the single session context.
      Session getSession​(java.lang.String username, boolean multiSession)
      Returns the current session for the specified username, under the specified context.
      java.util.Map<java.lang.String,​Session> getSessions()
      Returns the current logged in sessions under the multi session context.
      int getSessionsCount()
      Returns the current number of logged in sessions under the multi session context.
      boolean getUserVerification​(boolean retryLoginOnFailure)
      Requests that the currently logged in username to re-login using the single session context.
      boolean isAdminLoggedIn()
      Returns true if an admin user is currently logged in under the single user context.
      boolean isNewSessionAllowed​(java.lang.String username, boolean multiSession)
      Returns true if creation of a new session for the specified username is allowed under the specified context.
      boolean isUserLoggedIn()
      Returns true if a username is logged in under the single session context.
      boolean isUserLoggedIn​(java.lang.String username)
      Returns true if the specified username is logged in under the single session context.
      boolean isUserLoggedIn​(java.lang.String username, boolean multiSession)
      Returns true if the specified username is logged in under the specified context.
      boolean loginUser​(java.lang.String username)
      Logs in a user under the single session context with the specified username, no password checking is used.
      boolean loginUser​(java.lang.String username, boolean multiSession)
      Logs in a user, under the specified context, with the specified username, no password checking is used.
      boolean logoutUser()
      Logs out the currently logged in user from the single session context and clears any set permissions.
      boolean logoutUser​(java.lang.String username)
      Logs out the specified user from the single session context and clears any set permissions.
      boolean logoutUser​(java.lang.String username, boolean multiSession)
      Logs out the specified user, under the specified context, and, if in single session context, clears any set permissions.
      boolean requireAdmin()
      Checks that the logged in user, under the single session context, is an admin and if false, requests an admin override.
      boolean requireAndVerifyAdmin()
      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.
      void setAppIconPath​(java.lang.String appIconPath)
      Sets the path to the icon to use for all login dialogs.
      void setLoginErrorMessages​(LoginErrorMessages loginErrorMessages)
      Sets the text to use for the error message when errors occur during login
      void setMaxSessions​(int maxSessions)
      Sets the maximum number of allowed sessions, under the multi session context, before login is disabled.
      void setProgramName​(java.lang.String programName)
      Sets the name of the program to use in all login dialogs for the window title.
      boolean showLoginWindow​(boolean retryLoginOnFailure)
      Shows the login dialog window to log a user into the single session context.
      • Methods inherited from class java.lang.Object

        finalize, getClass, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • SessionManager

        public SessionManager​(UserManager userManager)
        Creates an instance of the SessionManager. Also creates the sessionLoginSuccess, sessionLoginFailure, multiSessionOpened and multiSessionClosed events.
        Parameters:
        userManager - the User Manager to use for the session
      • SessionManager

        public SessionManager​(UserManager userManager,
                              java.lang.String appIconPath)
        Creates an instance of the SessionManager. Also creates the sessionLoginSuccess, sessionLoginFailure, multiSessionOpened and multiSessionClosed events.
        Parameters:
        userManager - the User Manager to use for the session
        appIconPath - the path to the icon to use for all login dialogs
      • SessionManager

        public SessionManager​(UserManager userManager,
                              java.lang.String appIconPath,
                              java.lang.String programName)
        Creates an instance of the SessionManager. Also creates the sessionLoginSuccess, sessionLoginFailure, multiSessionOpened and multiSessionClosed events.
        Parameters:
        userManager - the User Manager to use for the session
        appIconPath - the path to the icon to use for all login dialogs
        programName - the name of the program to use in all login dialogs for the window title
    • Method Detail

      • getAppIconPath

        public java.lang.String getAppIconPath()
        Returns the path to the icon to use for all login dialogs.
        Returns:
        the path to the icon to use for all login dialogs
      • setAppIconPath

        public void setAppIconPath​(java.lang.String appIconPath)
        Sets the path to the icon to use for all login dialogs.
        Parameters:
        appIconPath - the icon path to set
      • getProgramName

        public java.lang.String getProgramName()
        Returns the name of the program to use in all login dialogs for the window title.
        Returns:
        the name of the program to use in all login dialogs for the window title
      • setProgramName

        public void setProgramName​(java.lang.String programName)
        Sets the name of the program to use in all login dialogs for the window title.
        Parameters:
        programName - the name of the program to set
      • enableDefaultErrorMessages

        public void enableDefaultErrorMessages()
        Enables basic default error messages to be used for the login dialog.
        Since:
        1.5.0
      • getLoginErrorMessages

        public LoginErrorMessages getLoginErrorMessages()
        Returns the text to use for the error message when errors occur during login.
        Returns:
        the text to use for the error message when errors occur during login
      • setLoginErrorMessages

        public void setLoginErrorMessages​(LoginErrorMessages loginErrorMessages)
        Sets the text to use for the error message when errors occur during login
        Parameters:
        loginErrorMessages - the error messages to set
      • enableDebugLogging

        public void enableDebugLogging()
        Enables basic event handlers with System.Out logging for all events.
        Since:
        1.5.0
      • disableDebugLogging

        public void disableDebugLogging()
        Disables basic event handlers with System.Out logging for all events.
        Since:
        1.5.0
      • getEventListeners

        public SessionManager.EventListeners getEventListeners()
        Returns a new instance of the EventListeners class that contains methods for all session related event handlers.
        Returns:
        a new instance of the EventListeners class
        Since:
        1.5.0
      • setMaxSessions

        public void setMaxSessions​(int maxSessions)
        Sets the maximum number of allowed sessions, under the multi session context, before login is disabled. The default of -1 removes limit and 0 blocks all new sessions.
        Parameters:
        maxSessions - the maximum number of allowed sessions
      • getMaxSessions

        public int getMaxSessions()
        Returns the maximum number of allowed sessions, under the multi session context, before login is disabled.
        Returns:
        the maximum number of allowed sessions
      • getSessionsCount

        public int getSessionsCount()
        Returns the current number of logged in sessions under the multi session context.
        Returns:
        the current number of logged in sessions under the multi session context
      • getSessions

        public java.util.Map<java.lang.String,​Session> getSessions()
        Returns the current logged in sessions under the multi session context.
        Returns:
        the current logged in sessions under the multi session context
      • getLoggedInUsername

        @Nullable
        public @Nullable java.lang.String getLoggedInUsername()
        Returns the username of the currently logged in user under the single session context.
        Returns:
        the username of the currently logged in user under the single session context
      • isUserLoggedIn

        public boolean isUserLoggedIn()
        Returns true if a username is logged in under the single session context.
        Returns:
        true if a username is logged in under the single session context
      • isUserLoggedIn

        public boolean isUserLoggedIn​(java.lang.String username)
        Returns true if the specified username is logged in under the single session context.
        Parameters:
        username - the username to lookup
        Returns:
        true if the specified username is logged in
      • isUserLoggedIn

        public boolean isUserLoggedIn​(java.lang.String username,
                                      boolean multiSession)
        Returns true if the specified username is logged in under the specified context.
        Parameters:
        username - the username to lookup
        multiSession - if true uses the multi session context, otherwise the single session context
        Returns:
        true if the specified username is logged in
        Since:
        1.5.0
      • getSession

        public Session getSession()
        Returns the current session for the currently logged in username under the single session context.
        Returns:
        the current session for the currently logged in username
      • getSession

        public Session getSession​(java.lang.String username)
        Returns the current session for the specified username under the single session context.
        Parameters:
        username - the username to lookup
        Returns:
        the current session for the specified username
      • getSession

        public Session getSession​(java.lang.String username,
                                  boolean multiSession)
        Returns the current session for the specified username, under the specified context.
        Parameters:
        username - the username to lookup
        multiSession - if true uses the multi session context, otherwise the single session context
        Returns:
        the current session for the specified username
        Since:
        1.5.0
      • loginUser

        public boolean loginUser​(java.lang.String username)
                          throws java.lang.IllegalStateException,
                                 ExpiredCredentialsException,
                                 LockedAccountException
        Logs in a user under the single session context with the specified username, no password checking is used.
        Parameters:
        username - the username of the user
        Returns:
        false if username does not exist or if session already exists
        Throws:
        java.lang.IllegalStateException - if the user role is disabled
        ExpiredCredentialsException - if the user credentials are expired
        LockedAccountException - if the user account is locked
      • loginUser

        public boolean loginUser​(java.lang.String username,
                                 boolean multiSession)
                          throws java.lang.IllegalStateException,
                                 ExpiredCredentialsException,
                                 LockedAccountException
        Logs in a user, under the specified context, with the specified username, no password checking is used.
        Parameters:
        username - the username of the user
        multiSession - if true uses the multi session context, otherwise the single session context
        Returns:
        false if username does not exist or if session already exists or if multi user and if max sessions is reached or equals 0
        Throws:
        java.lang.IllegalStateException - if the user role is disabled
        ExpiredCredentialsException - if the user credentials are expired
        LockedAccountException - if the user account is locked
        Since:
        1.5.0
      • isNewSessionAllowed

        public boolean isNewSessionAllowed​(java.lang.String username,
                                           boolean multiSession)
        Returns true if creation of a new session for the specified username is allowed under the specified context.
        Parameters:
        username - the username to lookup
        multiSession - if true checks status under the multi session context, otherwise checks under the single session context
        Returns:
        true if creation of a new session is allowed under the specified context
        Since:
        1.5.0
      • logoutUser

        public boolean logoutUser()
        Logs out the currently logged in user from the single session context and clears any set permissions. NOTE: if user was deleted from the database while logged in, the getUser event method will return null.
        Returns:
        false if a session does not exist
      • logoutUser

        public boolean logoutUser​(java.lang.String username)
        Logs out the specified user from the single session context and clears any set permissions. NOTE: if user was deleted from the database while logged in, the getUser event method will return null.
        Parameters:
        username - the username of the user
        Returns:
        false if the username does not exist or if a session does not exist for the username
      • logoutUser

        public boolean logoutUser​(java.lang.String username,
                                  boolean multiSession)
        Logs out the specified user, under the specified context, and, if in single session context, clears any set permissions. NOTE: if user was deleted from the database while logged in, the getUser event method will return null.
        Parameters:
        username - the username of the user
        multiSession - if true uses the multi session context, otherwise the single session context
        Returns:
        false if the username does not exist or if a session does not exist for the username
        Since:
        1.5.0
      • showLoginWindow

        public boolean showLoginWindow​(boolean retryLoginOnFailure)
        Shows the login dialog window to log a user into the single session context. 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.
        Parameters:
        retryLoginOnFailure - if true the login dialog will be shown again on failure
        Returns:
        true if user logged in successfully and false if cancel button was pressed or login failed
        Since:
        1.4.1, 1.5.0 changed implementation
      • getLoggedInUserRole

        @Nullable
        public @Nullable UserRole getLoggedInUserRole()
        Returns the user role of the currently logged in user under the single user context.
        Returns:
        the user role of the currently logged in user under the single user context
      • isAdminLoggedIn

        public boolean isAdminLoggedIn()
        Returns true if an admin user is currently logged in under the single user context.
        Returns:
        true if an admin user is currently logged in under the single user context
      • getAdminOverride

        public boolean getAdminOverride​(boolean retryLoginOnFailure)
        Requests an admin user to authenticate to override permissions using the single session context. 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.
        Parameters:
        retryLoginOnFailure - if true the login dialog will be shown again on failure
        Returns:
        true if admin override succeeded and false if cancel button was pressed or override failed
        Since:
        1.4.1
      • getUserVerification

        public boolean getUserVerification​(boolean retryLoginOnFailure)
        Requests that the currently logged in username to re-login using the single session context. 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.
        Parameters:
        retryLoginOnFailure - if true the login dialog will be shown again on failure
        Returns:
        true if user verified successfully and false if cancel button was pressed or verification failed
        Since:
        1.4.1
      • requireAdmin

        public boolean requireAdmin()
        Checks that the logged in user, under the single session context, is an admin and if false, requests an admin override.
        Returns:
        true if admin permissions retrieved successfully
        Since:
        1.4.1
      • requireAndVerifyAdmin

        public boolean requireAndVerifyAdmin()
        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.
        Returns:
        true if admin permissions retrieved successfully and admin verification succeeded
        Since:
        1.4.1
      • clone

        @Contract(" -> fail")
        public java.lang.Object clone()
                               throws java.lang.CloneNotSupportedException
        Overrides:
        clone in class java.lang.Object
        Throws:
        java.lang.CloneNotSupportedException