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

57 lines
1.1 KiB
Python
Raw Normal View History

2022-03-28 20:58:40 +00:00
"""
2022-03-28 21:54:39 +00:00
Python representation of `user_capability` 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
2022-03-28 21:41:49 +00:00
class UserCapabilityType(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
2022-03-28 21:41:49 +00:00
def _(self) -> UserCapabilityType:
2022-03-28 20:58:40 +00:00
"""
Transform into a `Capability`.
"""
2022-03-28 21:41:49 +00:00
return UserCapabilityType(self.capability_name)
2022-03-27 13:47:38 +00:00
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:54:39 +00:00
Representation of `user_capability` table
2022-03-28 20:58:40 +00:00
"""
2022-03-28 21:54:39 +00:00
user_name: str = Field(primary_key=True, foreign_key="user.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
)