def get_role(ctx, role_id): """ Get a role by its ID. Args: role_id (int): The ID of the role to retrieve Returns: hydra_complexmodels.Role: The role Raises: ResourceNotFoundError: If the role does not exist """ role_i = users.get_role(role_id, **ctx.in_header.__dict__) return Role(role_i)
def add_role(ctx, role): """ Add a new role. A role is the highest level in the permission tree and is container for permissions. Users are then assigned a role, which gives them the specified permissions Args: role (hydra_complexmodels.Role): The role to be added Returns: hydra_complexmodels.Role: The newly created role, complete with ID """ role_i = users.add_role(role, **ctx.in_header.__dict__) return Role(role_i)
def get_user_roles(ctx, user_id): """ Get the roles assigned to a user. (A user can have multiple roles) Args: user_id (int): The ID of the user whose roles you want Returns: List(hydra_complexmodels.Role): The roles of the user Raises: ResourceNotFoundError: If the user does not exist """ roles = users.get_user_roles(user_id, **ctx.in_header.__dict__) return [Role(r) for r in roles]
def get_all_roles(ctx): """ Get all roles Args: Returns: List(hydra_complexmodels.Role): All the roles in the system Raises: """ all_role_dicts = users.get_all_roles(**ctx.in_header.__dict__) all_role_cms = [Role(r) for r in all_role_dicts] return all_role_cms
def update_role(ctx, role): """ Update a role. Used to add multiple permissions and users to a role in one go. Args: role (hydra_complexmodels.Role): The role with new users & permissions Returns: hydra_complexmodels.Role: The newly updated role Raises: ResourceNotFoundError: If the role or any users or permissions do not exist """ role_i = users.update_role(role, **ctx.in_header.__dict__) return Role(role_i)
def set_role_perm(ctx, role_id, perm_id): """ Assign a permission to a role Args: role_id (int): The role to receive the permission perm_id (int): The permission being added to the role Returns: hydra_complexmodels.Role: The newly updated role, complete with new permission Raises: ResourceNotFoundError: If the role or permission do not exist """ role_i = users.set_role_perm(role_id, perm_id, **ctx.in_header.__dict__) return Role(role_i)
def get_role_by_code(ctx, role_code): """ Get a role by its code instead of ID (IDS can change between databases. codes are more stable) Args: role_code (string): The code of the role to retrieve Returns: hydra_complexmodels.Role: The role Raises: ResourceNotFoundError: If the role does not exist """ role_i = users.get_role_by_code(role_code, **ctx.in_header.__dict__) return Role(role_i)
def set_user_role(ctx, user_id, role_id): """ Assign a user to a role Args: user_id (int): The user to be given the new role role_id (int): The role to receive the new user Returns: hydra_complexmodels.Role: Returns the role to which the user has been added. (contains all the user roles). Raises: ResourceNotFoundError: If the user_id or role_id do not exist """ role_i = users.set_user_role(user_id, role_id, **ctx.in_header.__dict__) return Role(role_i)