canaille-globuzma/canaille/core/models.py

97 lines
2.5 KiB
Python
Raw Normal View History

2022-11-01 11:25:21 +00:00
import datetime
2023-08-23 13:18:43 +00:00
from typing import List
2023-08-17 13:55:41 +00:00
from typing import Optional
2022-11-01 11:25:21 +00:00
2020-08-14 11:18:08 +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>`_
"""
2023-08-23 13:18:43 +00:00
id: str
user_name: Optional[str]
password: Optional[str]
preferred_language: Optional[str]
family_name: Optional[str]
given_name: Optional[str]
formatted_name: Optional[str]
display_name: Optional[str]
emails: List[str]
phone_numbers: List[str]
formatted_address: Optional[str]
street: Optional[str]
postal_code: Optional[str]
locality: Optional[str]
region: Optional[str]
photo: Optional[str]
profile_url: Optional[str]
employee_number: Optional[str]
department: Optional[str]
title: Optional[str]
organization: Optional[str]
last_modified: Optional[datetime.datetime]
groups: List["Group"]
lock_date: Optional[datetime.datetime]
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"]:
raise NotImplementedError()
2023-08-17 13:55:41 +00:00
def has_password(self) -> bool:
"""
2023-08-17 13:55:41 +00:00
Checks wether a password has been set for the user.
"""
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.
"""
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.
"""
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
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>`_
"""
2023-08-23 13:18:43 +00:00
id: str
display_name: str
members: List["User"]
description: Optional[str]