2022-03-28 20:58:40 +00:00
|
|
|
"""
|
2022-03-28 21:54:39 +00:00
|
|
|
Python representation of `user` table.
|
2022-03-28 20:58:40 +00:00
|
|
|
"""
|
|
|
|
|
2022-03-27 01:17:48 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2022-03-28 22:17:31 +00:00
|
|
|
from typing import Any, Sequence
|
2022-03-27 01:17:48 +00:00
|
|
|
|
|
|
|
from pydantic import root_validator
|
|
|
|
from sqlalchemy.exc import IntegrityError
|
2022-03-27 13:47:38 +00:00
|
|
|
from sqlmodel import Field, Relationship, SQLModel
|
2022-03-27 01:17:48 +00:00
|
|
|
|
|
|
|
from ..config import Config
|
|
|
|
from .connection import Connection
|
2022-03-28 00:43:28 +00:00
|
|
|
from .device import Device
|
2022-03-29 19:57:33 +00:00
|
|
|
from .tag import Tag, TagValue
|
2022-03-27 01:17:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
class UserBase(SQLModel):
|
2022-03-28 20:58:40 +00:00
|
|
|
"""
|
|
|
|
Common to all representations of users
|
|
|
|
"""
|
|
|
|
|
2022-03-27 01:17:48 +00:00
|
|
|
name: str = Field(primary_key=True)
|
2022-03-28 01:00:07 +00:00
|
|
|
email: str | None = Field(default=None)
|
2022-03-27 01:17:48 +00:00
|
|
|
|
|
|
|
country: str | None = Field(default=None)
|
|
|
|
state: str | None = Field(default=None)
|
|
|
|
city: str | None = Field(default=None)
|
|
|
|
organization: str | None = Field(default=None)
|
|
|
|
organizational_unit: str | None = Field(default=None)
|
|
|
|
|
|
|
|
|
2022-03-28 20:58:40 +00:00
|
|
|
class UserCreate(UserBase):
|
|
|
|
"""
|
|
|
|
Representation of a newly created user
|
|
|
|
"""
|
|
|
|
|
|
|
|
password: str | None = Field(default=None)
|
|
|
|
password_clear: str | None = Field(default=None)
|
|
|
|
|
|
|
|
@root_validator
|
|
|
|
@classmethod
|
|
|
|
def hash_password(cls, values: dict[str, Any]) -> dict[str, Any]:
|
|
|
|
"""
|
|
|
|
Ensure the `password` value of this user gets set.
|
|
|
|
"""
|
|
|
|
|
|
|
|
if (values.get("password")) is not None:
|
|
|
|
# password is set
|
|
|
|
return values
|
|
|
|
|
|
|
|
if (password_clear := values.get("password_clear")) is None:
|
|
|
|
raise ValueError("No password to hash")
|
|
|
|
|
|
|
|
if (current_config := Config._) is None:
|
|
|
|
raise ValueError("Not configured")
|
|
|
|
|
|
|
|
values["password"] = current_config.crypto.crypt_context.hash(
|
|
|
|
password_clear)
|
|
|
|
|
|
|
|
return values
|
|
|
|
|
|
|
|
|
|
|
|
class UserRead(UserBase):
|
|
|
|
"""
|
|
|
|
Representation of a user read via the API
|
|
|
|
"""
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2022-03-27 01:17:48 +00:00
|
|
|
class User(UserBase, table=True):
|
2022-03-28 20:58:40 +00:00
|
|
|
"""
|
2022-03-28 21:54:39 +00:00
|
|
|
Representation of `user` table
|
2022-03-28 20:58:40 +00:00
|
|
|
"""
|
|
|
|
|
2022-03-27 01:17:48 +00:00
|
|
|
password: str
|
|
|
|
|
2022-03-29 19:57:33 +00:00
|
|
|
tags: list[Tag] = Relationship(
|
2022-03-27 13:47:38 +00:00
|
|
|
back_populates="user",
|
|
|
|
sa_relationship_kwargs={
|
|
|
|
"lazy": "joined",
|
|
|
|
"cascade": "all, delete-orphan",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2022-03-28 00:43:28 +00:00
|
|
|
devices: list[Device] = Relationship(
|
|
|
|
back_populates="owner",
|
|
|
|
)
|
|
|
|
|
2022-03-27 01:17:48 +00:00
|
|
|
@classmethod
|
2022-03-29 00:01:28 +00:00
|
|
|
def create(
|
|
|
|
cls,
|
|
|
|
*,
|
|
|
|
user: UserCreate,
|
|
|
|
) -> User | None:
|
2022-03-27 01:17:48 +00:00
|
|
|
"""
|
|
|
|
Create a new user in the database.
|
|
|
|
"""
|
|
|
|
|
|
|
|
try:
|
|
|
|
with Connection.session as db:
|
2022-03-29 00:01:28 +00:00
|
|
|
new_user = cls.from_orm(user)
|
2022-03-27 01:17:48 +00:00
|
|
|
|
2022-03-29 00:01:28 +00:00
|
|
|
db.add(new_user)
|
2022-03-27 01:17:48 +00:00
|
|
|
db.commit()
|
2022-03-29 00:01:28 +00:00
|
|
|
db.refresh(new_user)
|
2022-03-27 01:17:48 +00:00
|
|
|
|
2022-03-29 00:01:28 +00:00
|
|
|
return new_user
|
2022-03-27 01:17:48 +00:00
|
|
|
|
|
|
|
except IntegrityError:
|
|
|
|
# user already existed
|
|
|
|
return None
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get(cls, name: str) -> User | None:
|
2022-03-27 13:47:18 +00:00
|
|
|
"""
|
|
|
|
Load user from database by name.
|
|
|
|
"""
|
|
|
|
|
2022-03-27 01:17:48 +00:00
|
|
|
with Connection.session as db:
|
|
|
|
return db.get(cls, name)
|
|
|
|
|
2022-03-27 13:47:18 +00:00
|
|
|
@classmethod
|
|
|
|
def authenticate(
|
|
|
|
cls,
|
|
|
|
name: str,
|
|
|
|
password: str,
|
|
|
|
) -> User | None:
|
|
|
|
"""
|
|
|
|
Authenticate with name/password against users in database.
|
|
|
|
"""
|
|
|
|
|
2022-03-28 02:23:00 +00:00
|
|
|
crypt_context = Config._.crypto.crypt_context
|
2022-03-27 13:47:18 +00:00
|
|
|
|
|
|
|
if (user := cls.get(name)) is None:
|
|
|
|
# nonexistent user, fake doing password verification
|
|
|
|
crypt_context.dummy_verify()
|
|
|
|
return None
|
|
|
|
|
|
|
|
if not crypt_context.verify(password, user.password):
|
|
|
|
# password hash mismatch
|
|
|
|
return None
|
2022-03-27 01:17:48 +00:00
|
|
|
|
2022-03-27 13:47:18 +00:00
|
|
|
return user
|
|
|
|
|
|
|
|
def update(self) -> None:
|
|
|
|
"""
|
|
|
|
Update this user in the database.
|
|
|
|
"""
|
|
|
|
|
|
|
|
with Connection.session as db:
|
|
|
|
db.add(self)
|
|
|
|
db.commit()
|
|
|
|
db.refresh(self)
|
|
|
|
|
2022-03-28 20:18:00 +00:00
|
|
|
def delete(self) -> None:
|
2022-03-27 13:47:18 +00:00
|
|
|
"""
|
|
|
|
Delete this user from the database.
|
|
|
|
"""
|
|
|
|
|
|
|
|
with Connection.session as db:
|
|
|
|
db.delete(self)
|
|
|
|
db.commit()
|
2022-03-27 13:47:38 +00:00
|
|
|
|
2022-03-29 19:57:33 +00:00
|
|
|
def get_tags(self) -> set[TagValue]:
|
2022-03-28 20:58:40 +00:00
|
|
|
"""
|
2022-03-29 19:57:33 +00:00
|
|
|
Return the tags of this user.
|
2022-03-28 20:58:40 +00:00
|
|
|
"""
|
|
|
|
|
2022-03-28 00:48:44 +00:00
|
|
|
return set(
|
2022-03-29 19:57:33 +00:00
|
|
|
tag._
|
|
|
|
for tag in self.tags
|
2022-03-28 00:48:44 +00:00
|
|
|
)
|
2022-03-27 13:47:38 +00:00
|
|
|
|
2022-03-29 19:57:33 +00:00
|
|
|
def set_tags(
|
2022-03-28 22:17:31 +00:00
|
|
|
self,
|
2022-03-29 19:57:33 +00:00
|
|
|
tags: Sequence[TagValue],
|
2022-03-28 22:17:31 +00:00
|
|
|
) -> None:
|
2022-03-28 20:58:40 +00:00
|
|
|
"""
|
2022-03-29 19:57:33 +00:00
|
|
|
Change the tags of this user.
|
2022-03-28 20:58:40 +00:00
|
|
|
"""
|
|
|
|
|
2022-03-29 19:57:33 +00:00
|
|
|
self.tags = [
|
|
|
|
Tag(
|
2022-03-27 13:47:38 +00:00
|
|
|
user_name=self.name,
|
2022-03-29 19:57:33 +00:00
|
|
|
tag_value=tag.value,
|
|
|
|
) for tag in tags
|
2022-03-28 00:48:44 +00:00
|
|
|
]
|
2022-03-29 15:56:12 +00:00
|
|
|
|
2022-03-29 16:35:41 +00:00
|
|
|
def _can(
|
|
|
|
self,
|
2022-03-29 19:57:33 +00:00
|
|
|
tag: TagValue,
|
2022-03-29 16:35:41 +00:00
|
|
|
) -> bool:
|
|
|
|
"""
|
2022-03-29 19:57:33 +00:00
|
|
|
Check if this user has a tag.
|
2022-03-29 16:35:41 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
return (
|
2022-03-29 19:57:33 +00:00
|
|
|
tag in self.get_tags()
|
2022-03-29 16:35:41 +00:00
|
|
|
# admin can do everything
|
2022-03-29 19:57:33 +00:00
|
|
|
or TagValue.admin in self.get_tags()
|
2022-03-29 16:35:41 +00:00
|
|
|
)
|
|
|
|
|
2022-03-29 16:12:55 +00:00
|
|
|
def can_edit(
|
|
|
|
self,
|
|
|
|
user: User,
|
|
|
|
) -> bool:
|
|
|
|
"""
|
|
|
|
Check if this user can edit another user.
|
|
|
|
"""
|
|
|
|
|
|
|
|
return (
|
|
|
|
user.name == self.name
|
|
|
|
# admin can edit everything
|
2022-03-29 19:57:33 +00:00
|
|
|
or self._can(TagValue.admin)
|
2022-03-29 16:35:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
def is_admin(
|
|
|
|
self,
|
|
|
|
) -> bool:
|
|
|
|
"""
|
|
|
|
Check if this user is an admin.
|
|
|
|
"""
|
|
|
|
|
2022-03-29 19:57:33 +00:00
|
|
|
# is admin with "admin" tag
|
|
|
|
return self._can(TagValue.admin)
|
2022-03-29 16:35:41 +00:00
|
|
|
|
|
|
|
def can_login(
|
|
|
|
self,
|
|
|
|
) -> bool:
|
|
|
|
"""
|
|
|
|
Check if this user can log in.
|
|
|
|
"""
|
|
|
|
|
|
|
|
return (
|
2022-03-29 19:57:33 +00:00
|
|
|
# can login with "login" tag
|
|
|
|
self._can(TagValue.login)
|
2022-03-29 16:35:41 +00:00
|
|
|
# admins can always login
|
|
|
|
or self.is_admin()
|
|
|
|
)
|
|
|
|
|
|
|
|
def can_be_edited_by(
|
|
|
|
self,
|
|
|
|
user: User,
|
|
|
|
) -> bool:
|
|
|
|
"""
|
|
|
|
Check if this user can be edited by another user.
|
|
|
|
"""
|
|
|
|
|
|
|
|
return (
|
|
|
|
# user can edit itself
|
|
|
|
self.name == user.name
|
|
|
|
# admin can edit every user
|
2022-03-29 19:57:33 +00:00
|
|
|
or user._can(TagValue.admin)
|
2022-03-29 16:35:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
def can_be_deleted_by(
|
|
|
|
self,
|
|
|
|
user: User,
|
|
|
|
) -> bool:
|
|
|
|
"""
|
|
|
|
Check if this user can be deleted by another user.
|
|
|
|
"""
|
|
|
|
|
|
|
|
return (
|
|
|
|
# only admin can delete users
|
2022-03-29 19:57:33 +00:00
|
|
|
user._can(TagValue.admin)
|
2022-03-29 16:35:41 +00:00
|
|
|
# even admin cannot delete itself
|
|
|
|
and self.name != user.name
|
2022-03-29 16:12:55 +00:00
|
|
|
)
|
|
|
|
|
2022-03-29 15:56:12 +00:00
|
|
|
def owns(
|
|
|
|
self,
|
|
|
|
device: Device,
|
|
|
|
) -> bool:
|
|
|
|
"""
|
|
|
|
Check if this user owns a device.
|
|
|
|
"""
|
|
|
|
|
|
|
|
return (
|
|
|
|
device.owner_name == self.name
|
|
|
|
# admin owns everything
|
2022-03-29 19:57:33 +00:00
|
|
|
or self._can(TagValue.admin)
|
2022-03-29 15:56:12 +00:00
|
|
|
)
|