extract enum type for tenant account role (#3788)

This commit is contained in:
Bowen Liang
2024-04-25 18:20:08 +08:00
committed by GitHub
parent cde87cb225
commit c54fcfb45d
6 changed files with 36 additions and 12 deletions

View File

@@ -100,10 +100,11 @@ class Account(UserMixin, db.Model):
return db.session.query(ai).filter(
ai.account_id == self.id
).all()
# check current_user.current_tenant.current_role in ['admin', 'owner']
@property
def is_admin_or_owner(self):
return self._current_tenant.current_role in ['admin', 'owner']
return TenantAccountRole.is_privileged_role(self._current_tenant.current_role)
class TenantStatus(str, enum.Enum):
@@ -111,6 +112,16 @@ class TenantStatus(str, enum.Enum):
ARCHIVE = 'archive'
class TenantAccountRole(str, enum.Enum):
OWNER = 'owner'
ADMIN = 'admin'
NORMAL = 'normal'
@staticmethod
def is_privileged_role(role: str) -> bool:
return role and role in {TenantAccountRole.ADMIN, TenantAccountRole.OWNER}
class Tenant(db.Model):
__tablename__ = 'tenants'
__table_args__ = (
@@ -132,11 +143,11 @@ class Tenant(db.Model):
Account.id == TenantAccountJoin.account_id,
TenantAccountJoin.tenant_id == self.id
).all()
@property
def custom_config_dict(self) -> dict:
return json.loads(self.custom_config) if self.custom_config else {}
@custom_config_dict.setter
def custom_config_dict(self, value: dict):
self.custom_config = json.dumps(value)