2022-11-01 11:25:21 +00:00
|
|
|
import datetime
|
2023-08-17 13:55:41 +00:00
|
|
|
from typing import Optional
|
2022-11-01 11:25:21 +00:00
|
|
|
|
2023-08-13 20:08:28 +00:00
|
|
|
from flask import g
|
2021-12-20 22:57:27 +00:00
|
|
|
from flask import session
|
|
|
|
|
2020-08-14 11:18:08 +00:00
|
|
|
|
2023-04-10 11:45:52 +00:00
|
|
|
class User:
|
2023-08-17 13:55:41 +00:00
|
|
|
"""
|
|
|
|
User model, based on the `SCIM User schema <https://datatracker.ietf.org/doc/html/rfc7643#section-4.1>`_
|
|
|
|
"""
|
|
|
|
|
2021-12-02 17:23:14 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
self.read = set()
|
|
|
|
self.write = set()
|
|
|
|
self.permissions = set()
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
2020-08-19 14:20:57 +00:00
|
|
|
@classmethod
|
2023-08-17 13:55:41 +00:00
|
|
|
def get_from_login(cls, login=None, **kwargs) -> Optional["User"]:
|
2023-04-10 11:45:52 +00:00
|
|
|
raise NotImplementedError()
|
2023-04-14 16:46:42 +00:00
|
|
|
|
2020-08-21 08:23:39 +00:00
|
|
|
def login(self):
|
2023-08-17 13:55:41 +00:00
|
|
|
"""
|
|
|
|
Opens a session for the user.
|
|
|
|
"""
|
2023-08-13 20:08:28 +00:00
|
|
|
g.user = self
|
2020-12-11 10:52:37 +00:00
|
|
|
try:
|
2020-12-29 08:31:46 +00:00
|
|
|
previous = (
|
2022-12-29 00:10:07 +00:00
|
|
|
session["user_id"]
|
|
|
|
if isinstance(session["user_id"], list)
|
|
|
|
else [session["user_id"]]
|
2020-12-29 08:31:46 +00:00
|
|
|
)
|
2023-02-05 18:08:25 +00:00
|
|
|
session["user_id"] = previous + [self.id]
|
2020-12-11 10:52:37 +00:00
|
|
|
except KeyError:
|
2023-02-05 18:08:25 +00:00
|
|
|
session["user_id"] = [self.id]
|
2020-08-21 08:23:39 +00:00
|
|
|
|
2020-12-29 08:31:46 +00:00
|
|
|
@classmethod
|
2020-08-21 08:23:39 +00:00
|
|
|
def logout(self):
|
2023-08-17 13:55:41 +00:00
|
|
|
"""
|
|
|
|
Closes the user session.
|
|
|
|
"""
|
2020-08-21 08:23:39 +00:00
|
|
|
try:
|
2022-12-29 00:10:07 +00:00
|
|
|
session["user_id"].pop()
|
2023-08-13 20:08:28 +00:00
|
|
|
del g.user
|
2022-12-29 00:10:07 +00:00
|
|
|
if not session["user_id"]:
|
|
|
|
del session["user_id"]
|
2023-04-18 18:36:48 +00:00
|
|
|
except (IndexError, KeyError):
|
2020-08-21 08:23:39 +00:00
|
|
|
pass
|
|
|
|
|
2023-08-17 13:55:41 +00:00
|
|
|
def has_password(self) -> bool:
|
2023-06-27 15:41:00 +00:00
|
|
|
"""
|
2023-08-17 13:55:41 +00:00
|
|
|
Checks wether a password has been set for the user.
|
2023-06-27 15:41:00 +00:00
|
|
|
"""
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
2023-08-17 13:55:41 +00:00
|
|
|
def check_password(self, password: str) -> bool:
|
|
|
|
"""
|
|
|
|
Checks if the password matches the user password in the database.
|
|
|
|
"""
|
2023-04-10 11:45:52 +00:00
|
|
|
raise NotImplementedError()
|
2020-08-14 11:18:08 +00:00
|
|
|
|
2023-08-17 13:55:41 +00:00
|
|
|
def set_password(self, password: str):
|
|
|
|
"""
|
|
|
|
Sets a password for the user.
|
|
|
|
"""
|
2023-04-10 11:45:52 +00:00
|
|
|
raise NotImplementedError()
|
2021-12-02 17:23:14 +00:00
|
|
|
|
2023-08-17 13:55:41 +00:00
|
|
|
def can_read(self, field: str):
|
2021-12-08 17:06:50 +00:00
|
|
|
return field in self.read | self.write
|
|
|
|
|
2023-06-22 13:24:13 +00:00
|
|
|
@property
|
|
|
|
def preferred_email(self):
|
|
|
|
return self.emails[0] if self.emails else None
|
|
|
|
|
2023-08-14 14:25:12 +00:00
|
|
|
def __getattr__(self, name):
|
|
|
|
if name.startswith("can_") and name != "can_read":
|
|
|
|
permission = name[4:]
|
|
|
|
return permission in self.permissions
|
2021-12-02 17:23:14 +00:00
|
|
|
|
2023-08-14 14:25:12 +00:00
|
|
|
return super().__getattr__(name)
|
2021-12-02 17:23:14 +00:00
|
|
|
|
2022-11-01 11:25:21 +00:00
|
|
|
@property
|
2023-08-17 13:55:41 +00:00
|
|
|
def locked(self) -> bool:
|
|
|
|
"""
|
|
|
|
Wether the user account has been locked or has expired.
|
|
|
|
"""
|
2022-11-01 11:25:21 +00:00
|
|
|
return bool(self.lock_date) and self.lock_date < datetime.datetime.now(
|
|
|
|
datetime.timezone.utc
|
|
|
|
)
|
|
|
|
|
2021-06-03 13:00:11 +00:00
|
|
|
|
2023-04-10 11:45:52 +00:00
|
|
|
class Group:
|
2023-08-17 13:55:41 +00:00
|
|
|
"""
|
|
|
|
User model, based on the `SCIM Group schema <https://datatracker.ietf.org/doc/html/rfc7643#section-4.2>`_
|
|
|
|
"""
|