|
@@ -5,6 +5,9 @@ import transaction
|
5
|
5
|
import typing as typing
|
6
|
6
|
|
7
|
7
|
from tracim.models.auth import User
|
|
8
|
+from sqlalchemy.orm.exc import NoResultFound
|
|
9
|
+from tracim.exceptions import BadUserPassword
|
|
10
|
+from tracim.exceptions import AuthenticationFailed
|
8
|
11
|
|
9
|
12
|
|
10
|
13
|
class UserApi(object):
|
|
@@ -14,21 +17,46 @@ class UserApi(object):
|
14
|
17
|
self._user = current_user
|
15
|
18
|
self._config = config
|
16
|
19
|
|
17
|
|
- def get_all(self):
|
18
|
|
- return self._session.query(User).order_by(User.display_name).all()
|
19
|
|
-
|
20
|
20
|
def _base_query(self):
|
21
|
21
|
return self._session.query(User)
|
22
|
22
|
|
23
|
|
- def get_one(self, user_id: int):
|
|
23
|
+ def get_one(self, user_id: int) -> User:
|
24
|
24
|
return self._base_query().filter(User.user_id==user_id).one()
|
25
|
25
|
|
26
|
|
- def get_one_by_email(self, email: str):
|
|
26
|
+ def get_one_by_email(self, email: str) -> User:
|
27
|
27
|
return self._base_query().filter(User.email==email).one()
|
28
|
28
|
|
29
|
29
|
def get_one_by_id(self, id: int) -> User:
|
30
|
30
|
return self._base_query().filter(User.user_id==id).one()
|
31
|
31
|
|
|
32
|
+ def get_all(self) -> typing.Iterable[User]:
|
|
33
|
+ return self._session.query(User).order_by(User.display_name).all()
|
|
34
|
+
|
|
35
|
+ def user_with_email_exists(self, email: str):
|
|
36
|
+ try:
|
|
37
|
+ self.get_one_by_email(email)
|
|
38
|
+ return True
|
|
39
|
+ # TODO - G.M - 09-04-2018 - Better exception
|
|
40
|
+ except:
|
|
41
|
+ return False
|
|
42
|
+
|
|
43
|
+ def authenticate_user(self, email, password) -> User:
|
|
44
|
+ """
|
|
45
|
+ Authenticate user with email and password, raise AuthenticationFailed
|
|
46
|
+ if uncorrect.
|
|
47
|
+ :param email: email of the user
|
|
48
|
+ :param password: cleartext password of the user
|
|
49
|
+ :return: User who was authenticated.
|
|
50
|
+ """
|
|
51
|
+ try:
|
|
52
|
+ user = self.get_one_by_email(email)
|
|
53
|
+ if user.validate_password(password):
|
|
54
|
+ return user
|
|
55
|
+ else:
|
|
56
|
+ raise BadUserPassword()
|
|
57
|
+ except (BadUserPassword, NoResultFound):
|
|
58
|
+ raise AuthenticationFailed
|
|
59
|
+
|
32
|
60
|
def update(
|
33
|
61
|
self,
|
34
|
62
|
user: User,
|
|
@@ -48,14 +76,6 @@ class UserApi(object):
|
48
|
76
|
if do_save:
|
49
|
77
|
self.save(user)
|
50
|
78
|
|
51
|
|
- def user_with_email_exists(self, email: str):
|
52
|
|
- try:
|
53
|
|
- self.get_one_by_email(email)
|
54
|
|
- return True
|
55
|
|
- # TODO - G.M - 09-04-2018 - Better exception
|
56
|
|
- except:
|
57
|
|
- return False
|
58
|
|
-
|
59
|
79
|
def create_user(self, email=None, groups=[], save_now=False) -> User:
|
60
|
80
|
user = User()
|
61
|
81
|
|