Source code for app.domain.teams.guards
from uuid import UUID
from litestar.connection import ASGIConnection
from litestar.exceptions import PermissionDeniedException
from litestar.handlers.base import BaseRouteHandler
from app.config import constants
from app.db.models import TeamRoles
__all__ = ["requires_team_admin", "requires_team_membership", "requires_team_ownership"]
[docs]
def requires_team_membership(connection: ASGIConnection, _: BaseRouteHandler) -> None:
"""Verify the connection user is a member of the team.
Args:
connection (ASGIConnection): _description_
_ (BaseRouteHandler): _description_
Raises:
PermissionDeniedException: _description_
"""
team_id = connection.path_params["team_id"]
has_system_role = any(
assigned_role.role_name
for assigned_role in connection.user.roles
if assigned_role.role.name in {constants.SUPERUSER_ACCESS_ROLE}
)
has_team_role = any(membership.team.id == team_id for membership in connection.user.teams)
if connection.user.is_superuser or has_system_role or has_team_role:
return
raise PermissionDeniedException(detail="Insufficient permissions to access team.")
[docs]
def requires_team_admin(connection: ASGIConnection, _: BaseRouteHandler) -> None:
"""Verify the connection user is a team admin.
Args:
connection (ASGIConnection): _description_
_ (BaseRouteHandler): _description_
Raises:
PermissionDeniedException: _description_
"""
team_id = connection.path_params["team_id"]
has_system_role = any(
assigned_role.role_name
for assigned_role in connection.user.roles
if assigned_role.role.name in {constants.SUPERUSER_ACCESS_ROLE}
)
has_team_role = any(
membership.team.id == team_id and membership.role == TeamRoles.ADMIN for membership in connection.user.teams
)
if connection.user.is_superuser or has_system_role or has_team_role:
return
raise PermissionDeniedException(detail="Insufficient permissions to access team.")
[docs]
def requires_team_ownership(connection: ASGIConnection, _: BaseRouteHandler) -> None:
"""Verify that the connection user is the team owner.
Args:
connection (ASGIConnection): _description_
_ (BaseRouteHandler): _description_
Raises:
PermissionDeniedException: _description_
"""
team_id = UUID(connection.path_params["team_id"])
has_system_role = any(
assigned_role.role.name
for assigned_role in connection.user.roles
if assigned_role.role.name in {constants.SUPERUSER_ACCESS_ROLE}
)
has_team_role = any(membership.team.id == team_id and membership.is_owner for membership in connection.user.teams)
if connection.user.is_superuser or has_system_role or has_team_role:
return
msg = "Insufficient permissions to access team."
raise PermissionDeniedException(detail=msg)