|
| 1 | +"""This manages Authorization Data.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from enum import Enum, unique |
| 6 | +from typing import List, Optional |
| 7 | + |
| 8 | +from sqlalchemy import JSON, or_ |
| 9 | +from sqlalchemy.dialects.postgresql import ARRAY, ENUM |
| 10 | + |
| 11 | +from .audit_mixin import AuditDateTimeMixin, AuditUserMixin |
| 12 | +from .base_model import BaseModel |
| 13 | +from .db import db |
| 14 | + |
| 15 | + |
| 16 | +@unique |
| 17 | +class AuthType(Enum): |
| 18 | + """Admin type enum.""" |
| 19 | + |
| 20 | + DASHBOARD = "DASHBOARD" |
| 21 | + FORM = "FORM" |
| 22 | + FILTER = "FILTER" |
| 23 | + |
| 24 | + def __str__(self): |
| 25 | + """To string value.""" |
| 26 | + return self.value |
| 27 | + |
| 28 | + |
| 29 | +class Authorization(AuditDateTimeMixin, AuditUserMixin, BaseModel, db.Model): |
| 30 | + """This class manages authorization.""" |
| 31 | + |
| 32 | + id = db.Column(db.Integer, primary_key=True, comment="Authorization ID") |
| 33 | + tenant = db.Column(db.String, nullable=True, comment="Tenant key") |
| 34 | + auth_type = db.Column( |
| 35 | + ENUM(AuthType), nullable=False, index=True, comment="Auth Type" |
| 36 | + ) |
| 37 | + resource_id = db.Column( |
| 38 | + db.String, nullable=False, index=True, comment="Resource identifier" |
| 39 | + ) |
| 40 | + resource_details = db.Column(JSON, nullable=True, comment="Resource details") |
| 41 | + roles = db.Column(ARRAY(db.String), nullable=True, comment="Applicable roles") |
| 42 | + user_name = db.Column(db.String, nullable=True, comment="Applicable user") |
| 43 | + |
| 44 | + @classmethod |
| 45 | + def find_user_authorizations( |
| 46 | + cls, |
| 47 | + auth_type: AuthType, |
| 48 | + roles: List[str] = None, |
| 49 | + user_name: str = None, |
| 50 | + tenant: str = None, |
| 51 | + ) -> List[Authorization]: |
| 52 | + """Find authorizations.""" |
| 53 | + query = cls._auth_query(auth_type, roles, tenant, user_name) |
| 54 | + return query.all() |
| 55 | + |
| 56 | + @classmethod |
| 57 | + def find_all_authorizations( |
| 58 | + cls, auth_type: AuthType, tenant: str = None |
| 59 | + ) -> List[Authorization]: |
| 60 | + """Find authorizations.""" |
| 61 | + query = cls.query.filter(Authorization.auth_type == auth_type) |
| 62 | + |
| 63 | + if tenant: |
| 64 | + query = query.filter(Authorization.tenant == tenant) |
| 65 | + return query.all() |
| 66 | + |
| 67 | + @classmethod |
| 68 | + def _auth_query(cls, auth_type, roles, tenant, user_name): |
| 69 | + role_condition = [Authorization.roles.contains([role]) for role in roles] |
| 70 | + query = ( |
| 71 | + cls.query.filter(Authorization.auth_type == auth_type) |
| 72 | + .filter(or_(*role_condition)) |
| 73 | + .filter( |
| 74 | + or_( |
| 75 | + Authorization.user_name.is_(None), |
| 76 | + Authorization.user_name == user_name, |
| 77 | + ) |
| 78 | + ) |
| 79 | + ) |
| 80 | + |
| 81 | + if tenant: |
| 82 | + query = query.filter(Authorization.tenant == tenant) |
| 83 | + return query |
| 84 | + |
| 85 | + @classmethod |
| 86 | + def find_resource_authorization( # pylint: disable=too-many-arguments |
| 87 | + cls, |
| 88 | + auth_type: AuthType, |
| 89 | + resource_id: str, |
| 90 | + roles: List[str] = None, |
| 91 | + user_name: str = None, |
| 92 | + tenant: str = None, |
| 93 | + ) -> List[Authorization]: |
| 94 | + """Find resource authorization.""" |
| 95 | + query = cls._auth_query(auth_type, roles, tenant, user_name) |
| 96 | + query = query.filter(Authorization.resource_id == str(resource_id)) |
| 97 | + return query.all() |
| 98 | + |
| 99 | + @classmethod |
| 100 | + def find_resource_by_id( |
| 101 | + cls, |
| 102 | + auth_type: AuthType, |
| 103 | + resource_id: str, |
| 104 | + user_name: str = None, |
| 105 | + tenant: str = None, |
| 106 | + ) -> Optional[Authorization]: |
| 107 | + """Find resource authorization by id.""" |
| 108 | + query = ( |
| 109 | + cls.query.filter(Authorization.resource_id == str(resource_id)) |
| 110 | + .filter(Authorization.auth_type == auth_type) |
| 111 | + .filter( |
| 112 | + or_( |
| 113 | + Authorization.user_name.is_(None), |
| 114 | + Authorization.user_name == user_name, |
| 115 | + ) |
| 116 | + ) |
| 117 | + ) |
| 118 | + if tenant: |
| 119 | + query = query.filter(Authorization.tenant == tenant) |
| 120 | + return query.one_or_none() |
0 commit comments