|
@@ -21,68 +21,44 @@ class UserApi(object):
|
21
|
21
|
def _base_query(self):
|
22
|
22
|
return self._session.query(User)
|
23
|
23
|
|
24
|
|
- def _get_correct_user_type(
|
25
|
|
- self,
|
26
|
|
- user: User,
|
27
|
|
- in_context: bool,
|
28
|
|
- ) -> typing.Union[User, UserInContext]:
|
|
24
|
+ def get_user_with_context(self, user: User) -> UserInContext:
|
29
|
25
|
"""
|
30
|
|
- Choose user type object depending on in_context bool.
|
31
|
|
- :param user:
|
32
|
|
- :param in_context:
|
33
|
|
- :return: user as User or UserInContext if in_context is True
|
|
26
|
+ Return UserInContext object from User
|
34
|
27
|
"""
|
35
|
|
- if in_context:
|
36
|
|
- user = UserInContext(
|
37
|
|
- user=user,
|
38
|
|
- dbsession=self._session,
|
39
|
|
- config=self._config,
|
40
|
|
- )
|
|
28
|
+ user = UserInContext(
|
|
29
|
+ user=user,
|
|
30
|
+ dbsession=self._session,
|
|
31
|
+ config=self._config,
|
|
32
|
+ )
|
41
|
33
|
return user
|
42
|
34
|
|
43
|
35
|
# Getters
|
44
|
36
|
|
45
|
|
- def get_one(
|
46
|
|
- self,
|
47
|
|
- user_id: int,
|
48
|
|
- in_context: bool=False,
|
49
|
|
- ) -> typing.Union[UserInContext, User]:
|
|
37
|
+ def get_one(self, user_id: int) -> User:
|
50
|
38
|
"""
|
51
|
39
|
Get one user by user id
|
52
|
|
- :param user_id:
|
53
|
|
- :param in_context: Return User or UserInContext Object
|
54
|
|
- :return: one user
|
55
|
40
|
"""
|
56
|
|
- user = self._base_query().filter(User.user_id == user_id).one()
|
57
|
|
- return self._get_correct_user_type(user, in_context)
|
|
41
|
+ return self._base_query().filter(User.user_id == user_id).one()
|
58
|
42
|
|
59
|
|
- def get_one_by_email(
|
60
|
|
- self,
|
61
|
|
- email: str,
|
62
|
|
- in_context: bool=False,
|
63
|
|
- ) -> User:
|
|
43
|
+ def get_one_by_email(self, email: str) -> User:
|
64
|
44
|
"""
|
65
|
45
|
Get one user by email
|
66
|
46
|
:param email: Email of the user
|
67
|
|
- :param in_context: Return User or UserInContext Object
|
68
|
47
|
:return: one user
|
69
|
48
|
"""
|
70
|
|
- user = self._base_query().filter(User.email == email).one()
|
71
|
|
- return self._get_correct_user_type(user, in_context)
|
|
49
|
+ return self._base_query().filter(User.email == email).one()
|
72
|
50
|
|
73
|
51
|
# FIXME - G.M - 24-04-2018 - Duplicate method with get_one.
|
74
|
|
- def get_one_by_id(self, id: int, in_context=False) -> User:
|
75
|
|
- return self.get_one(user_id=id, in_context=in_context)
|
|
52
|
+ def get_one_by_id(self, id: int) -> User:
|
|
53
|
+ return self.get_one(user_id=id)
|
76
|
54
|
|
77
|
|
- def get_current(self, in_context: bool=False):
|
|
55
|
+ def get_current_user(self) -> User:
|
78
|
56
|
"""
|
79
|
57
|
Get current_user
|
80
|
|
- :param in_context:
|
81
|
|
- :return:
|
82
|
58
|
"""
|
83
|
59
|
if not self._user:
|
84
|
60
|
raise UserNotExist()
|
85
|
|
- return self._get_correct_user_type(self._user, in_context)
|
|
61
|
+ return self._user
|
86
|
62
|
|
87
|
63
|
def get_all(self) -> typing.Iterable[User]:
|
88
|
64
|
return self._session.query(User).order_by(User.display_name).all()
|
|
@@ -97,23 +73,18 @@ class UserApi(object):
|
97
|
73
|
except:
|
98
|
74
|
return False
|
99
|
75
|
|
100
|
|
- def authenticate_user(self,
|
101
|
|
- email: str,
|
102
|
|
- password: str,
|
103
|
|
- in_context=False
|
104
|
|
- ) -> typing.Union[User, UserInContext]:
|
|
76
|
+ def authenticate_user(self, email: str, password: str) -> User:
|
105
|
77
|
"""
|
106
|
78
|
Authenticate user with email and password, raise AuthenticationFailed
|
107
|
79
|
if uncorrect.
|
108
|
80
|
:param email: email of the user
|
109
|
81
|
:param password: cleartext password of the user
|
110
|
|
- :param in_context:
|
111
|
82
|
:return: User who was authenticated.
|
112
|
83
|
"""
|
113
|
84
|
try:
|
114
|
85
|
user = self.get_one_by_email(email)
|
115
|
86
|
if user.validate_password(password):
|
116
|
|
- return self._get_correct_user_type(user, in_context=in_context)
|
|
87
|
+ return user
|
117
|
88
|
else:
|
118
|
89
|
raise BadUserPassword()
|
119
|
90
|
except (BadUserPassword, NoResultFound):
|