kiwi-vpn/api/kiwi_vpn_api/db/user_capability.py

59 lines
1.1 KiB
Python
Raw Normal View History

2022-03-28 20:58:40 +00:00
"""
2022-03-28 21:26:47 +00:00
Python representation of `user_capabilities` table.
2022-03-28 20:58:40 +00:00
"""
2022-03-27 13:47:38 +00:00
from enum import Enum
from typing import TYPE_CHECKING
from sqlmodel import Field, Relationship, SQLModel
if TYPE_CHECKING:
from .user import User
class Capability(Enum):
2022-03-28 20:58:40 +00:00
"""
Allowed values for capabilities
"""
2022-03-27 13:47:38 +00:00
admin = "admin"
login = "login"
issue = "issue"
renew = "renew"
2022-03-28 00:48:44 +00:00
def __repr__(self) -> str:
return self.value
2022-03-27 13:47:38 +00:00
class UserCapabilityBase(SQLModel):
2022-03-28 20:58:40 +00:00
"""
Common to all representations of capabilities
"""
2022-03-27 13:47:38 +00:00
capability_name: str = Field(primary_key=True)
@property
def _(self) -> Capability:
2022-03-28 20:58:40 +00:00
"""
Transform into a `Capability`.
"""
2022-03-27 13:47:38 +00:00
return Capability(self.capability_name)
def __repr__(self) -> str:
return self.capability_name
class UserCapability(UserCapabilityBase, table=True):
2022-03-28 20:58:40 +00:00
"""
2022-03-28 21:26:47 +00:00
Representation of `user_capabilities` table
2022-03-28 20:58:40 +00:00
"""
2022-03-28 21:26:47 +00:00
__tablename__ = "user_capabilities"
user_name: str = Field(primary_key=True, foreign_key="users.name")
2022-03-28 00:48:44 +00:00
2022-03-27 13:47:38 +00:00
user: "User" = Relationship(
2022-03-28 00:48:44 +00:00
back_populates="capabilities",
2022-03-27 13:47:38 +00:00
)