capabilities rework
This commit is contained in:
parent
730c7ab966
commit
04a5798258
2 changed files with 17 additions and 28 deletions
|
@ -13,9 +13,11 @@ class Capability(Enum):
|
||||||
issue = "issue"
|
issue = "issue"
|
||||||
renew = "renew"
|
renew = "renew"
|
||||||
|
|
||||||
|
def __repr__(self) -> str:
|
||||||
|
return self.value
|
||||||
|
|
||||||
|
|
||||||
class UserCapabilityBase(SQLModel):
|
class UserCapabilityBase(SQLModel):
|
||||||
user_name: str = Field(primary_key=True, foreign_key="user.name")
|
|
||||||
capability_name: str = Field(primary_key=True)
|
capability_name: str = Field(primary_key=True)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
@ -27,6 +29,8 @@ class UserCapabilityBase(SQLModel):
|
||||||
|
|
||||||
|
|
||||||
class UserCapability(UserCapabilityBase, table=True):
|
class UserCapability(UserCapabilityBase, table=True):
|
||||||
|
user_name: str = Field(primary_key=True, foreign_key="user.name")
|
||||||
|
|
||||||
user: "User" = Relationship(
|
user: "User" = Relationship(
|
||||||
back_populates="capabilities"
|
back_populates="capabilities",
|
||||||
)
|
)
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from typing import Any, Sequence
|
from typing import Any
|
||||||
|
|
||||||
from pydantic import root_validator
|
from pydantic import root_validator
|
||||||
from sqlalchemy.exc import IntegrityError
|
from sqlalchemy.exc import IntegrityError
|
||||||
|
@ -109,34 +109,19 @@ class User(UserBase, table=True):
|
||||||
db.delete(self)
|
db.delete(self)
|
||||||
db.commit()
|
db.commit()
|
||||||
|
|
||||||
def extend_capabilities(self, capabilities: Sequence[Capability]) -> None:
|
def get_capabilities(self) -> set[Capability]:
|
||||||
"""
|
return set(
|
||||||
Extend this user's capabilities
|
capability._
|
||||||
"""
|
for capability in self.capabilities
|
||||||
|
)
|
||||||
|
|
||||||
for capability in capabilities:
|
def set_capabilities(self, capabilities: set[Capability]) -> None:
|
||||||
user_capability = UserCapability(
|
self.capabilities = [
|
||||||
|
UserCapability(
|
||||||
user_name=self.name,
|
user_name=self.name,
|
||||||
capability_name=capability.value,
|
capability_name=capability.value,
|
||||||
)
|
) for capability in capabilities
|
||||||
|
]
|
||||||
if user_capability not in self.capabilities:
|
|
||||||
self.capabilities.append(user_capability)
|
|
||||||
|
|
||||||
def remove_capabilities(self, capabilities: Sequence[Capability]) -> None:
|
|
||||||
"""
|
|
||||||
Remove from this user's capabilities
|
|
||||||
"""
|
|
||||||
|
|
||||||
for capability in capabilities:
|
|
||||||
try:
|
|
||||||
self.capabilities.remove(UserCapability(
|
|
||||||
user_name=self.name,
|
|
||||||
capability_name=capability.value,
|
|
||||||
))
|
|
||||||
|
|
||||||
except ValueError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
class UserCreate(UserBase):
|
class UserCreate(UserBase):
|
||||||
|
|
Loading…
Reference in a new issue