Class PanelUserHandler

java.lang.Object
tv.phantombot.panel.PanelUser.PanelUserHandler

public final class PanelUserHandler extends Object
Panel user management for the web panel and websocket
Author:
Sartharon
  • Constructor Details

    • PanelUserHandler

      public PanelUserHandler()
  • Method Details

    • checkLoginB64

      public static boolean checkLoginB64(String base64Token, String requestUri)
      Checks if the panel user is allowed to logon to the web panel and/or use the websocket
      Parameters:
      base64Token - The user's login token in base64 from the HTTP-Headers
      requestUri - The requested uri
      Returns:
      true if the token is valid, the user is enabled and the user is allowed to access the uri; false otherwise
      See Also:
    • checkLoginAndGetUserB64

      public static PanelUser checkLoginAndGetUserB64(String base64Token, String requestUri)
      Checks if the panel user is allowed to logon to the web panel and/or use the websocket
      Parameters:
      base64Token - The user's login token in base64 from the HTTP-Headers
      requestUri - The requested uri
      Returns:
      A PanelUser if the user exists, is enabled and is allowed to access the uri; null otherwise
      See Also:
    • checkLogin

      public static boolean checkLogin(String username, String password)
      Checks if the panel user is allowed to logon to the web panel and/or use the websocket
      Parameters:
      username - The user's username
      password - The user's password
      Returns:
      true if the exists and is enabled; false otherwise
      See Also:
    • checkLoginAndGetUser

      public static PanelUser checkLoginAndGetUser(String username, String password)
      Checks if the panel user is allowed to logon to the web panel and/or use the websocket
      Parameters:
      username - The user's username
      password - The user's password
      Returns:
      A PanelUser if the user exists, is enabled and is allowed to access the uri; null otherwise
      See Also:
    • checkLogin

      public static boolean checkLogin(String username, String password, String requestUri)
      Checks if the panel user is allowed to logon to the web panel and/or use the websocket
      Parameters:
      username - The user's username
      password - The user's password
      requestUri - The requested uri
      Returns:
      true if the user exists, is enabled and is allowed to access the uri; false otherwise
      See Also:
    • checkLoginAndGetUser

      public static PanelUser checkLoginAndGetUser(String username, String password, String requestUri)
      Checks if the panel user is allowed to logon to the web panel and/or use the websocket
      Parameters:
      username - The user's username
      password - The user's password
      requestUri - The requested uri
      Returns:
      A PanelUser if the user exists, is enabled and is allowed to access the uri; null otherwise
      See Also:
    • checkAuthTokenAndGetUser

      public static PanelUser checkAuthTokenAndGetUser(String authToken)
      Checks if the panel user is allowed to logon to the web panel and/or use the websocket by his authentication token
      Parameters:
      authToken - The user's authentication token
      Returns:
      A PanelUser if a user with that authentication token exists and is enabled; null otherwise
      See Also:
    • getUserAuthToken

      public static String getUserAuthToken(String username)
      Get the authentication token for a panel user
      Parameters:
      username - The user's username
      Returns:
      The user's websocket authentication token if the user exists and is enabled; null otherwise
      See Also:
    • changePassword

      public static PanelUserHandler.PanelMessage changePassword(String username, String currentPassword, String newPassword)
      Changes a panel user's password

      Only existing and editable panel users can have their password changed
      Parameters:
      username -
      currentPassword -
      newPassword -
      Returns:
      The fitting panel message
      See Also:
    • createNewUser

      public static PanelUserHandler.PanelMessage createNewUser(String username, boolean enabled)
      Creates a new panel user with full access permissions to all panel sections if the user does not exist
      Parameters:
      username - The user's name
      enabled - true if the user should be enabled; false otherwise
      Returns:
      The fitting panel message
      See Also:
    • createNewUser

      public static PanelUserHandler.PanelMessage createNewUser(String username, JSONArray jsoPermissions, boolean enabled, boolean canManageUsers, boolean canRestartBot)
      Creates a new panel user if the user does not exist
      Parameters:
      username - The user's name
      permission - The user's permissions display name
      enabled - true if the user should be enabled; false otherwise
      canManageUsers - true to allow the user to manage other users; false to prohibit this
      canRestartBot - true to allow the user to restart the bot via the panel; false to prohibit this
      Returns:
      The fitting panel message
      See Also:
    • createNewUser

      public static PanelUserHandler.PanelMessage createNewUser(String username, Map<String,PanelUserHandler.Permission> permissions, boolean enabled, boolean canManageUsers, boolean canRestartBot)
      Creates a new panel user if the user does not exist
      Parameters:
      username - The user's name
      permission - The user's permissions
      enabled - true if the user should be enabled; false otherwise
      canManageUsers - true to allow the user to manage other users; false to prohibit this
      canRestartBot - true to allow the user to restart the bot via the panel; false to prohibit this
      Returns:
      The fitting panel message
      See Also:
    • deleteUser

      public static PanelUserHandler.PanelMessage deleteUser(String username)
      Deletes a panel user

      Only existing and editable panel users can be deleted
      Parameters:
      username - The user's name which should be deleted
      Returns:
      The fitting panel message
      See Also:
    • editUser

      public static PanelUserHandler.PanelMessage editUser(String currentUsername, String newUsername, JSONArray jsoPermissions, boolean enabled, boolean canManageUsers, boolean canRestartBot)
      Changes a panel user's properties

      Only existing and editable panel users can be deleted
      Parameters:
      currentUsername - The user's name which should be edited
      newUsername - The user's new name; null to not change the username
      permission - The user's new permissions; null to not change the permission
      enabled - true to enable the user; false to disable the user
      canManageUsers - true to allow the user to manage other users; false to prohibit this
      canRestartBot - true to allow the user to restart the bot via the panel; false to prohibit this
      Returns:
      The fitting panel message
      See Also:
    • editUser

      public static PanelUserHandler.PanelMessage editUser(String currentUsername, String newUsername, Map<String,PanelUserHandler.Permission> permissions, boolean enabled, Boolean canManageUsers, Boolean canRestartBot)
      Changes a panel user's properties

      Only existing and editable panel users can be deleted
      Parameters:
      currentUsername - The user's name which should be edited
      newUsername - The user's new name; null to not change the username
      permission - The user's new permission; null to not change the permissions
      enabled - true to enable the user; false to disable the user
      canManageUsers - true to allow the user to manage other users; false to prohibit this
      canRestartBot - true to allow the user to restart the bot via the panel; false to prohibit this
      Returns:
      The fitting panel message
      See Also:
    • editUser

      public static PanelUserHandler.PanelMessage editUser(String currentUsername, String newUsername, Map<String,PanelUserHandler.Permission> permissions, boolean enabled)
      Changes a panel user's properties

      Only existing and editable panel users can be deleted
      Parameters:
      currentUsername - The user's name which should be edited
      newUsername - The user's new name; null to not change the username
      permission - The user's new permission; null to not change the permissions
      enabled - true to enable the user; false to disable the user
      Returns:
      The fitting panel message
      See Also:
    • resetPassword

      public static PanelUserHandler.PanelMessage resetPassword(String username)
      Resets a panel user's password

      Only existing and editable panel users can have their passwords reset
      Parameters:
      username - The user's name who's password should be reset
      Returns:
      The new random password
      See Also:
    • getAllUsersJSONObject

      public static void getAllUsersJSONObject(JSONStringer jsonObject)
      Adds the panel users safe properties (username, isEnabled, permissions, creationDate, lastLogin) as an array to an JSONStringer instance
      Parameters:
      jsonObject - The JSONStringer instance to which the array should be added to
      See Also:
    • getPermissionsJSONObject

      public static void getPermissionsJSONObject(JSONStringer jsonObject)
      Adds the permissions as an array to an JSONStringer instance
      Parameters:
      jsonObject - The JSONStringer instance to which the permissions array should be added to
    • getUserJSONObject

      public static void getUserJSONObject(String username, JSONStringer jsonObject)
      Adds the panel user's safe properties (username, isEnabled, permissions, hasSetPassword, userType) as an array to an {@link JSONStringer JSONStringer} instance
      Parameters:
      username - The user's name which properties should be returned
      jsonObject - The JSONStringer instance to which the user's properties should be added to
      See Also:
    • getAllPanelSectionsJSONObject

      public static void getAllPanelSectionsJSONObject(JSONStringer jsonObject)
      Adds the available panel sections as an array to an JSONStringer instance
      Parameters:
      jsonObject - The JSONStringer instance to which the panel sections should be added to
    • getFullAccessPermissions

      public static Map<String,PanelUserHandler.Permission> getFullAccessPermissions()
      Gets a permissions map which includes full permission to all available panel sections
    • getAccessPermissions

      public static Map<String,PanelUserHandler.Permission> getAccessPermissions(boolean full)
      Gets a permissions map which includes permission to all available panel sections
      Parameters:
      full - true for PanelUserHandler.Permission.READ_WRITE; otherwise PanelUserHandler.Permission.READ_ONLY
    • checkPanelUserDatabaseAccess

      public static boolean checkPanelUserDatabaseAccess(PanelUser user, String tableName, String section, boolean isWriteAction)
      Checks if a user is allowed to access a database table
      Parameters:
      user - The user
      tablename - The tableName to be checked
      section - The panel section on which this request was created
      isWriteAction - Indicates if the action is an action requiring write permissions
      Returns:
      true if the user is allowed to access the table under the conditions; false otherwise
    • sectionConvert

      public static String sectionConvert(String section)
      Checks for alternate section names and converts them to standard section names
      Parameters:
      section - The section to check
      Returns:
      The normalized section name, in lower case
    • checkPanelUserScriptAccess

      public static boolean checkPanelUserScriptAccess(PanelUser user, String script, String[] args, String section)
      Checks if a user is allowed send websocket events to a specific script
      Parameters:
      user - The user
      script - The script path being accessed
      section - The panel section on which this request was created
      Returns:
      true if the user is allowed to access the table under the conditions; false otherwise
    • checkPanelUserSectionAccess

      public static boolean checkPanelUserSectionAccess(PanelUser user, String section, boolean isWriteAction)
      Checks if a user has to access a panel section
      Parameters:
      user - The user
      section - The panel section on which this request was created
      isWriteAction - Indicates if the action is an action requiring write permissions
      Returns:
      true if the user is allowed to access the table under the conditions; false otherwise
    • checkPanelUserCommandAccess

      public static boolean checkPanelUserCommandAccess(PanelUser user, String command, String section)
      Checks if a user is allowed to send a command to the bots modules
      Parameters:
      user - The user
      command - The command which has been sent by the user through the panel
      section - The panel section on which this request was created
      Returns:
      true if the user is allowed to access the table under the conditions; false otherwise