Skip to main content

rbac.go

rbac.go - Overview

  1. Overview This file defines the data access object (DAO) layer for managing users, organizations, groups, and invitations in a SQLite database. It includes functions for creating, reading, updating, and deleting these entities. It also contains functions for password reset requests and pre-login checks.

  2. Detailed Documentation

CreateInviteEntry

  • Purpose: Creates a new invite entry in the database.
  • Parameters:
    • ctx (context.Context): The context for the operation.
    • req (*types.Invite): The invite object to be created.
  • Returns:
    • *model.ApiError: An API error if the creation fails, otherwise nil.

DeleteInvitation

  • Purpose: Deletes an invitation from the database based on organization ID and email.
  • Parameters:
    • ctx (context.Context): The context for the operation.
    • orgID (string): The organization ID of the invitation to delete.
    • email (string): The email associated with the invitation to delete.
  • Returns:
    • *model.ApiError: An API error if the deletion fails, otherwise nil.

GetInviteFromEmail

  • Purpose: Retrieves an invite from the database based on the provided email.
  • Parameters:
    • ctx (context.Context): The context for the operation.
    • email (string): The email associated with the invite to retrieve.
  • Returns:
    • *types.Invite: The invite object if found, otherwise nil.
    • *model.ApiError: An API error if there is an error during retrieval, otherwise nil.

GetInviteFromToken

  • Purpose: Retrieves an invite from the database based on the provided token.
  • Parameters:
    • ctx (context.Context): The context for the operation.
    • token (string): The token associated with the invite to retrieve.
  • Returns:
    • *types.Invite: The invite object if found, otherwise nil.
    • *model.ApiError: An API error if there is an error during retrieval, otherwise nil.

GetInvites

  • Purpose: Retrieves all invites for a given organization ID.
  • Parameters:
    • ctx (context.Context): The context for the operation.
    • orgID (string): The organization ID to filter invites by.
  • Returns:
    • []types.Invite: A slice of invite objects if found, otherwise nil.
    • *model.ApiError: An API error if there is an error during retrieval, otherwise nil.

CreateOrg

  • Purpose: Creates a new organization in the database.
  • Parameters:
    • ctx (context.Context): The context for the operation.
    • org (*types.Organization): The organization object to be created.
  • Returns:
    • *types.Organization: The created organization object, if successful, otherwise nil.
    • *model.ApiError: An API error if the creation fails, otherwise nil.

GetOrg

  • Purpose: Retrieves an organization from the database based on its ID.
  • Parameters:
    • ctx (context.Context): The context for the operation.
    • id (string): The ID of the organization to retrieve.
  • Returns:
    • *types.Organization: The organization object if found, otherwise nil.
    • *model.ApiError: An API error if there is an error during retrieval, otherwise nil.

GetOrgByName

  • Purpose: Retrieves an organization from the database based on its name.
  • Parameters:
    • ctx (context.Context): The context for the operation.
    • name (string): The name of the organization to retrieve.
  • Returns:
    • *types.Organization: The organization object if found, otherwise nil.
    • *model.ApiError: An API error if there is an error during retrieval, otherwise nil.

GetOrgs

  • Purpose: Retrieves all organizations from the database.
  • Parameters:
    • ctx (context.Context): The context for the operation.
  • Returns:
    • []types.Organization: A slice of organization objects.
    • *model.ApiError: An API error if there is an error during retrieval, otherwise nil.

EditOrg

  • Purpose: Updates an existing organization in the database.
  • Parameters:
    • ctx (context.Context): The context for the operation.
    • org (*types.Organization): The organization object to be updated.
  • Returns:
    • *model.ApiError: An API error if the update fails, otherwise nil.

DeleteOrg

  • Purpose: Deletes an organization from the database based on its ID.
  • Parameters:
    • ctx (context.Context): The context for the operation.
    • id (string): The ID of the organization to delete.
  • Returns:
    • *model.ApiError: An API error if the deletion fails, otherwise nil.

CreateUser

  • Purpose: Creates a new user in the database.
  • Parameters:
    • ctx (context.Context): The context for the operation.
    • user (*types.User): The user object to be created.
    • isFirstUser (bool): Indicates whether this is the first user being created.
  • Returns:
    • *types.User: The created user object, if successful, otherwise nil.
    • *model.ApiError: An API error if the creation fails, otherwise nil.

EditUser

  • Purpose: Updates an existing user in the database.
  • Parameters:
    • ctx (context.Context): The context for the operation.
    • update (*types.User): The user object containing the updated information.
  • Returns:
    • *types.User: The updated user object, if successful, otherwise nil.
    • *model.ApiError: An API error if the update fails, otherwise nil.

UpdateUserPassword

  • Purpose: Updates a user's password in the database.
  • Parameters:
    • ctx (context.Context): The context for the operation.
    • passwordHash (string): The hashed password to be set.
    • userId (string): The ID of the user to update.
  • Returns:
    • *model.ApiError: An API error if the update fails, otherwise nil.

UpdateUserGroup

  • Purpose: Updates a user's group in the database.
  • Parameters:
    • ctx (context.Context): The context for the operation.
    • userId (string): The ID of the user to update.
    • groupId (string): The ID of the group to assign to the user.
  • Returns:
    • *model.ApiError: An API error if the update fails, otherwise nil.

DeleteUser

  • Purpose: Deletes a user from the database based on their ID.
  • Parameters:
    • ctx (context.Context): The context for the operation.
    • id (string): The ID of the user to delete.
  • Returns:
    • *model.ApiError: An API error if the deletion fails, otherwise nil.

GetUser

  • Purpose: Retrieves a user from the database based on their ID, including role and organization information.
  • Parameters:
    • ctx (context.Context): The context for the operation.
    • id (string): The ID of the user to retrieve.
  • Returns:
    • *types.GettableUser: The user object with role and organization information if found, otherwise nil.
    • *model.ApiError: An API error if there is an error during retrieval, otherwise nil.

GetUserByEmail

  • Purpose: Retrieves a user from the database based on their email address, including role and organization information.
  • Parameters:
    • ctx (context.Context): The context for the operation.
    • email (string): The email address of the user to retrieve.
  • Returns:
    • *types.GettableUser: The user object with role and organization information if found, otherwise nil.
    • *model.ApiError: An API error if there is an error during retrieval, otherwise nil.

GetUsers

  • Purpose: Retrieves all users from the database.
  • Parameters:
    • ctx (context.Context): The context for the operation.
  • Returns:
    • []types.GettableUser: A slice of user objects.
    • *model.ApiError: An API error if there is an error during retrieval, otherwise nil.

GetUsersWithOpts

  • Purpose: Retrieves users from the database, with an optional limit on the number of users returned.
  • Parameters:
    • ctx (context.Context): The context for the operation.
    • limit (int): The maximum number of users to return. If 0, no limit is applied.
  • Returns:
    • []types.GettableUser: A slice of user objects.
    • *model.ApiError: An API error if there is an error during retrieval, otherwise nil.

GetUsersByOrg

  • Purpose: Retrieves all users belonging to a specific organization.
  • Parameters:
    • ctx (context.Context): The context for the operation.
    • orgId (string): The ID of the organization.
  • Returns:
    • []types.GettableUser: A slice of user objects.
    • *model.ApiError: An API error if there is an error during retrieval, otherwise nil.

GetUsersByGroup

  • Purpose: Retrieves all users belonging to a specific group.
  • Parameters:
    • ctx (context.Context): The context for the operation.
    • groupId (string): The ID of the group.
  • Returns:
    • []types.GettableUser: A slice of user objects.
    • *model.ApiError: An API error if there is an error during retrieval, otherwise nil.

CreateGroup

  • Purpose: Creates a new group in the database.
  • Parameters:
    • ctx (context.Context): The context for the operation.
    • group (*types.Group): The group object to be created.
  • Returns:
    • *types.Group: The created group object, if successful, otherwise nil.
    • *model.ApiError: An API error if the creation fails, otherwise nil.

DeleteGroup

  • Purpose: Deletes a group from the database based on its ID.
  • Parameters:
    • ctx (context.Context): The context for the operation.
    • id (string): The ID of the group to delete.
  • Returns:
    • *model.ApiError: An API error if the deletion fails, otherwise nil.

GetGroup

  • Purpose: Retrieves a group from the database based on its ID.
  • Parameters:
    • ctx (context.Context): The context for the operation.
    • id (string): The ID of the group to retrieve.
  • Returns:
    • *types.Group: The group object if found, otherwise nil.
    • *model.ApiError: An API error if there is an error during retrieval, otherwise nil.

GetGroupByName

  • Purpose: Retrieves a group from the database based on its name.
  • Parameters:
    • ctx (context.Context): The context for the operation.
    • name (string): The name of the group to retrieve.
  • Returns:
    • *types.Group: The group object if found, otherwise nil.
    • *model.ApiError: An API error if there is an error during retrieval, otherwise nil.

GetGroups

  • Purpose: Retrieves all groups from the database.
  • Parameters:
    • ctx (context.Context): The context for the operation.
  • Returns:
    • []types.Group: A slice of group objects.
    • *model.ApiError: An API error if there is an error during retrieval, otherwise nil.

CreateResetPasswordEntry

  • Purpose: Creates a new reset password entry in the database.
  • Parameters:
    • ctx (context.Context): The context for the operation.
    • req (*types.ResetPasswordRequest): The reset password request object to be created.
  • Returns:
    • *model.ApiError: An API error if the creation fails, otherwise nil.

DeleteResetPasswordEntry

  • Purpose: Deletes a reset password entry from the database based on its token.
  • Parameters:
    • ctx (context.Context): The context for the operation.
    • token (string): The token of the reset password entry to delete.
  • Returns:
    • *model.ApiError: An API error if the deletion fails, otherwise nil.

GetResetPasswordEntry

  • Purpose: Retrieves a reset password entry from the database based on its token.
  • Parameters:
    • ctx (context.Context): The context for the operation.
    • token (string): The token of the reset password entry to retrieve.
  • Returns:
    • *types.ResetPasswordRequest: The reset password request object if found, otherwise nil.
    • *model.ApiError: An API error if there is an error during retrieval, otherwise nil.

PrecheckLogin

  • Purpose: Performs a pre-login check to determine user validity and self-registration options.
  • Parameters:
    • ctx (context.Context): The context for the operation.
    • email (string): The email address to check.
    • sourceUrl (string): The source URL of the login attempt.
  • Returns:
    • *model.PrecheckResponse: A response containing flags indicating user validity, self-registration availability, SSO status, and SSO URL.
    • model.BaseApiError: An API error if there is an error during the check, otherwise nil.

GetUserRole

  • Purpose: Retrieves the role name of a user based on their group ID.
  • Parameters:
    • ctx (context.Context): The context for the operation.
    • groupId (string): The ID of the user's group.
  • Returns:
    • string: The name of the user's role.
    • error: An error if there is an error during retrieval, otherwise nil.

GetUserCount

  • Purpose: Retrieves the total number of users in the database.
  • Parameters:
    • ctx (context.Context): The context for the operation.
  • Returns:
    • int: The total number of users.
    • error: An error if there is an error during retrieval, otherwise nil.

Include in Getting Started: NO