-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathuser.py
More file actions
69 lines (61 loc) · 2.54 KB
/
user.py
File metadata and controls
69 lines (61 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from app.domain.entities.user import User
from app.domain.enums.user_role import UserRole
from app.domain.exceptions.user import (
ActivationChangeNotPermittedError,
RoleAssignmentNotPermittedError,
RoleChangeNotPermittedError,
)
from app.domain.ports.password_hasher import PasswordHasher
from app.domain.ports.user_id_generator import UserIdGenerator
from app.domain.value_objects.raw_password import RawPassword
from app.domain.value_objects.user_id import UserId
from app.domain.value_objects.user_password_hash import UserPasswordHash
from app.domain.value_objects.username import Username
class UserService:
def __init__(
self,
user_id_generator: UserIdGenerator,
password_hasher: PasswordHasher,
) -> None:
self._user_id_generator = user_id_generator
self._password_hasher = password_hasher
def create_user(
self,
username: Username,
raw_password: RawPassword,
role: UserRole = UserRole.USER,
is_active: bool = True,
) -> User:
"""
:raises RoleAssignmentNotPermittedError:
:raises DomainFieldError:
"""
if not role.is_assignable:
raise RoleAssignmentNotPermittedError(role)
user_id = UserId(self._user_id_generator())
password_hash = UserPasswordHash(self._password_hasher.hash(raw_password))
return User(
id_=user_id,
username=username,
password_hash=password_hash,
role=role,
is_active=is_active,
)
def is_password_valid(self, user: User, raw_password: RawPassword) -> bool:
return self._password_hasher.verify(
raw_password=raw_password,
hashed_password=user.password_hash.value,
)
def change_password(self, user: User, raw_password: RawPassword) -> None:
hashed_password = UserPasswordHash(self._password_hasher.hash(raw_password))
user.password_hash = hashed_password
def toggle_user_activation(self, user: User, *, is_active: bool) -> None:
""":raises ActivationChangeNotPermittedError:"""
if not user.role.is_changeable:
raise ActivationChangeNotPermittedError(user.username, user.role)
user.is_active = is_active
def toggle_user_admin_role(self, user: User, *, is_admin: bool) -> None:
""":raises RoleChangeNotPermittedError:"""
if not user.role.is_changeable:
raise RoleChangeNotPermittedError(user.username, user.role)
user.role = UserRole.ADMIN if is_admin else UserRole.USER