|
@@ -1,4 +1,6 @@
|
1
|
1
|
# -*- coding: utf-8 -*-
|
|
2
|
+import argparse
|
|
3
|
+from pyramid.scripting import AppEnvironment
|
2
|
4
|
import transaction
|
3
|
5
|
from sqlalchemy.exc import IntegrityError
|
4
|
6
|
|
|
@@ -14,6 +16,7 @@ from tracim.exceptions import CommandAbortedError
|
14
|
16
|
from tracim.lib.core.group import GroupApi
|
15
|
17
|
from tracim.lib.core.user import UserApi
|
16
|
18
|
from tracim.models import User
|
|
19
|
+from tracim.models import Group
|
17
|
20
|
|
18
|
21
|
|
19
|
22
|
class UserCommand(AppContextCommand):
|
|
@@ -23,10 +26,10 @@ class UserCommand(AppContextCommand):
|
23
|
26
|
|
24
|
27
|
action = NotImplemented
|
25
|
28
|
|
26
|
|
- def get_description(self):
|
|
29
|
+ def get_description(self) -> str:
|
27
|
30
|
return '''Create or update user.'''
|
28
|
31
|
|
29
|
|
- def get_parser(self, prog_name):
|
|
32
|
+ def get_parser(self, prog_name: str) -> argparse.ArgumentParser:
|
30
|
33
|
parser = super().get_parser(prog_name)
|
31
|
34
|
|
32
|
35
|
parser.add_argument(
|
|
@@ -77,32 +80,42 @@ class UserCommand(AppContextCommand):
|
77
|
80
|
|
78
|
81
|
return parser
|
79
|
82
|
|
80
|
|
- def _user_exist(self, login):
|
|
83
|
+ def _user_exist(self, login: str) -> User:
|
81
|
84
|
return self._user_api.user_with_email_exists(login)
|
82
|
85
|
|
83
|
|
- def _get_group(self, name):
|
|
86
|
+ def _get_group(self, name: str) -> Group:
|
84
|
87
|
return self._group_api.get_one_with_name(name)
|
85
|
88
|
|
86
|
|
- def _add_user_to_named_group(self, user, group_name):
|
|
89
|
+ def _add_user_to_named_group(
|
|
90
|
+ self,
|
|
91
|
+ user: str,
|
|
92
|
+ group_name: str
|
|
93
|
+ ) -> None:
|
87
|
94
|
group = self._get_group(group_name)
|
88
|
95
|
if user not in group.users:
|
89
|
96
|
group.users.append(user)
|
90
|
97
|
self._session.flush()
|
91
|
98
|
|
92
|
|
- def _remove_user_from_named_group(self, user, group_name):
|
|
99
|
+ def _remove_user_from_named_group(
|
|
100
|
+ self,
|
|
101
|
+ user: User,
|
|
102
|
+ group_name: str
|
|
103
|
+ ) -> None:
|
93
|
104
|
group = self._get_group(group_name)
|
94
|
105
|
if user in group.users:
|
95
|
106
|
group.users.remove(user)
|
96
|
107
|
self._session.flush()
|
97
|
108
|
|
98
|
|
- def _create_user(self, login, password, **kwargs):
|
|
109
|
+ def _create_user(self, login: str, password: str, **kwargs) -> User:
|
99
|
110
|
if not password:
|
100
|
111
|
if self._password_required():
|
101
|
|
- raise CommandAbortedError("You must provide -p/--password parameter")
|
|
112
|
+ raise CommandAbortedError(
|
|
113
|
+ "You must provide -p/--password parameter"
|
|
114
|
+ )
|
102
|
115
|
password = ''
|
103
|
116
|
|
104
|
117
|
try:
|
105
|
|
- user =self._user_api.create_user(email=login)
|
|
118
|
+ user = self._user_api.create_user(email=login)
|
106
|
119
|
user.password = password
|
107
|
120
|
self._user_api.save(user)
|
108
|
121
|
# TODO - G.M - 04-04-2018 - [Caldav] Check this code
|
|
@@ -117,13 +130,17 @@ class UserCommand(AppContextCommand):
|
117
|
130
|
|
118
|
131
|
return user
|
119
|
132
|
|
120
|
|
- def _update_password_for_login(self, login, password):
|
|
133
|
+ def _update_password_for_login(self, login: str, password: str) -> None:
|
121
|
134
|
user = self._user_api.get_one_by_email(login)
|
122
|
135
|
user.password = password
|
123
|
136
|
self._session.flush()
|
124
|
137
|
transaction.commit()
|
125
|
138
|
|
126
|
|
- def take_app_action(self, parsed_args, app_context):
|
|
139
|
+ def take_app_action(
|
|
140
|
+ self,
|
|
141
|
+ parsed_args: argparse.Namespace,
|
|
142
|
+ app_context: AppEnvironment
|
|
143
|
+ ) -> None:
|
127
|
144
|
# TODO - G.M - 05-04-2018 -Refactor this in order
|
128
|
145
|
# to not setup object var outside of __init__ .
|
129
|
146
|
self._session = app_context['request'].dbsession
|
|
@@ -142,7 +159,7 @@ class UserCommand(AppContextCommand):
|
142
|
159
|
|
143
|
160
|
print("User created/updated")
|
144
|
161
|
|
145
|
|
- def _proceed_user(self, parsed_args):
|
|
162
|
+ def _proceed_user(self, parsed_args: argparse.Namespace) -> User:
|
146
|
163
|
self._check_context(parsed_args)
|
147
|
164
|
|
148
|
165
|
if self.action == self.ACTION_CREATE:
|
|
@@ -171,7 +188,11 @@ class UserCommand(AppContextCommand):
|
171
|
188
|
|
172
|
189
|
return user
|
173
|
190
|
|
174
|
|
- def _proceed_groups(self, user, parsed_args):
|
|
191
|
+ def _proceed_groups(
|
|
192
|
+ self,
|
|
193
|
+ user: User,
|
|
194
|
+ parsed_args: argparse.Namespace
|
|
195
|
+ ) -> None:
|
175
|
196
|
# User always in "users" group
|
176
|
197
|
self._add_user_to_named_group(user, 'users')
|
177
|
198
|
|
|
@@ -181,13 +202,13 @@ class UserCommand(AppContextCommand):
|
181
|
202
|
for group_name in parsed_args.remove_from_group:
|
182
|
203
|
self._remove_user_from_named_group(user, group_name)
|
183
|
204
|
|
184
|
|
- def _password_required(self):
|
|
205
|
+ def _password_required(self) -> bool:
|
185
|
206
|
# TODO - G.M - 04-04-2018 - [LDAP] Check this code
|
186
|
207
|
# if config.get('auth_type') == LDAPAuth.name:
|
187
|
208
|
# return False
|
188
|
209
|
return True
|
189
|
210
|
|
190
|
|
- def _check_context(self, parsed_args):
|
|
211
|
+ def _check_context(self, parsed_args: argparse.Namespace) -> None:
|
191
|
212
|
# TODO - G.M - 04-04-2018 - [LDAP] Check this code
|
192
|
213
|
# if config.get('auth_type') == LDAPAuth.name:
|
193
|
214
|
# auth_instance = config.get('auth_instance')
|