|
@@ -5,7 +5,7 @@ from smtplib import SMTPException
|
5
|
5
|
import transaction
|
6
|
6
|
import typing as typing
|
7
|
7
|
|
8
|
|
-from tracim.exceptions import NotificationNotSend
|
|
8
|
+from tracim.exceptions import NotificationNotSend, EmailValidationFailed
|
9
|
9
|
from tracim.lib.mail_notifier.notifier import get_email_manager
|
10
|
10
|
from sqlalchemy.orm import Session
|
11
|
11
|
|
|
@@ -17,6 +17,7 @@ from tracim.exceptions import UserDoesNotExist
|
17
|
17
|
from tracim.exceptions import WrongUserPassword
|
18
|
18
|
from tracim.exceptions import AuthenticationFailed
|
19
|
19
|
from tracim.models.context_models import UserInContext
|
|
20
|
+from tracim.models.context_models import TypeUser
|
20
|
21
|
|
21
|
22
|
|
22
|
23
|
class UserApi(object):
|
|
@@ -94,19 +95,40 @@ class UserApi(object):
|
94
|
95
|
def get_all(self) -> typing.Iterable[User]:
|
95
|
96
|
return self._session.query(User).order_by(User.display_name).all()
|
96
|
97
|
|
97
|
|
- def find_one(self, user_email_or_public_name: str) -> User:
|
|
98
|
+ def find(
|
|
99
|
+ self,
|
|
100
|
+ user_id: int=None,
|
|
101
|
+ email: str=None,
|
|
102
|
+ public_name: str=None
|
|
103
|
+ ) -> typing.Tuple[TypeUser, User]:
|
|
104
|
+ """
|
|
105
|
+ Find existing user from all theses params.
|
|
106
|
+ Check is made in this order: user_id, email, public_name
|
|
107
|
+ If no user found raise UserDoesNotExist exception
|
|
108
|
+ """
|
98
|
109
|
user = None
|
99
|
|
- try:
|
100
|
|
- user = self.get_one_by_email(email=user_email_or_public_name)
|
101
|
|
- except UserDoesNotExist:
|
102
|
|
-
|
103
|
|
-
|
104
|
|
- pass
|
105
|
110
|
|
106
|
|
- if not user:
|
107
|
|
- user = self.get_one_by_public_name(user_email_or_public_name)
|
|
111
|
+ if user_id:
|
|
112
|
+ try:
|
|
113
|
+ user = self.get_one(user_id)
|
|
114
|
+ return TypeUser.USER_ID, user
|
|
115
|
+ except UserDoesNotExist:
|
|
116
|
+ pass
|
|
117
|
+ if email:
|
|
118
|
+ try:
|
|
119
|
+ user = self.get_one_by_email(email)
|
|
120
|
+ return TypeUser.EMAIL, user
|
|
121
|
+ except UserDoesNotExist:
|
|
122
|
+ pass
|
|
123
|
+ if public_name:
|
|
124
|
+ try:
|
|
125
|
+ user = self.get_one_by_public_name(public_name)
|
|
126
|
+ return TypeUser.PUBLIC_NAME, user
|
|
127
|
+ except UserDoesNotExist:
|
|
128
|
+ pass
|
|
129
|
+
|
|
130
|
+ raise UserDoesNotExist('User not found with any of given params.')
|
108
|
131
|
|
109
|
|
- return user
|
110
|
132
|
|
111
|
133
|
|
112
|
134
|
def user_with_email_exists(self, email: str) -> bool:
|
|
@@ -136,6 +158,15 @@ class UserApi(object):
|
136
|
158
|
|
137
|
159
|
|
138
|
160
|
|
|
161
|
+ def _check_email(self, email: str) -> bool:
|
|
162
|
+
|
|
163
|
+ if not email:
|
|
164
|
+ return False
|
|
165
|
+ email = email.split('@')
|
|
166
|
+ if len(email) != 2:
|
|
167
|
+ return False
|
|
168
|
+ return True
|
|
169
|
+
|
139
|
170
|
def update(
|
140
|
171
|
self,
|
141
|
172
|
user: User,
|
|
@@ -149,6 +180,9 @@ class UserApi(object):
|
149
|
180
|
user.display_name = name
|
150
|
181
|
|
151
|
182
|
if email is not None:
|
|
183
|
+ email_exist = self._check_email(email)
|
|
184
|
+ if not email_exist:
|
|
185
|
+ raise EmailValidationFailed('Email given form {} is uncorrect'.format(email))
|
152
|
186
|
user.email = email
|
153
|
187
|
|
154
|
188
|
if password is not None:
|
|
@@ -200,7 +234,11 @@ class UserApi(object):
|
200
|
234
|
"""Previous create_user method"""
|
201
|
235
|
user = User()
|
202
|
236
|
|
|
237
|
+ email_exist = self._check_email(email)
|
|
238
|
+ if not email_exist:
|
|
239
|
+ raise EmailValidationFailed('Email given form {} is uncorrect'.format(email))
|
203
|
240
|
user.email = email
|
|
241
|
+ user.display_name = email.split('@')[0]
|
204
|
242
|
|
205
|
243
|
for group in groups:
|
206
|
244
|
user.groups.append(group)
|