|
@@ -1,26 +1,14 @@
|
1
|
1
|
# -*- coding: utf-8 -*-
|
2
|
|
-import uuid
|
3
|
2
|
import random
|
4
|
3
|
|
5
|
4
|
import pytz
|
6
|
|
-from tracim import model as pm
|
|
5
|
+from tracim import model as pm
|
7
|
6
|
|
8
|
|
-from sprox.tablebase import TableBase
|
9
|
|
-from sprox.formbase import EditableForm, AddRecordForm
|
10
|
|
-from sprox.fillerbase import TableFiller, EditFormFiller
|
11
|
|
-from tracim.config.app_cfg import CFG
|
12
|
|
-from tw2 import forms as tw2f
|
13
|
7
|
import tg
|
14
|
8
|
from tg import predicates
|
15
|
9
|
from tg import tmpl_context
|
16
|
10
|
from tg.i18n import ugettext as _
|
17
|
11
|
|
18
|
|
-from sprox.widgets import PropertyMultipleSelectField
|
19
|
|
-from sprox._compat import unicode_text
|
20
|
|
-
|
21
|
|
-from formencode import Schema
|
22
|
|
-from formencode.validators import FieldsMatch
|
23
|
|
-
|
24
|
12
|
from tracim.controllers import TIMRestController
|
25
|
13
|
from tracim.controllers.user import UserWorkspaceRestController
|
26
|
14
|
|
|
@@ -28,19 +16,20 @@ from tracim.lib import CST
|
28
|
16
|
from tracim.lib import helpers as h
|
29
|
17
|
from tracim.lib.base import logger
|
30
|
18
|
from tracim.lib.email import get_email_manager
|
31
|
|
-from tracim.lib.user import UserApi
|
32
|
19
|
from tracim.lib.group import GroupApi
|
|
20
|
+from tracim.lib.user import UserApi
|
33
|
21
|
from tracim.lib.userworkspace import RoleApi
|
34
|
22
|
from tracim.lib.workspace import WorkspaceApi
|
35
|
23
|
|
36
|
24
|
from tracim.model import DBSession
|
37
|
|
-from tracim.model.auth import Group, User
|
38
|
|
-from tracim.model.serializers import Context, CTX, DictLikeClass
|
|
25
|
+from tracim.model.auth import Group
|
|
26
|
+from tracim.model.serializers import CTX
|
|
27
|
+from tracim.model.serializers import Context
|
|
28
|
+from tracim.model.serializers import DictLikeClass
|
|
29
|
+
|
39
|
30
|
|
40
|
31
|
class UserProfileAdminRestController(TIMRestController):
|
41
|
|
- """
|
42
|
|
- CRUD Controller allowing to manage groups of a user
|
43
|
|
- """
|
|
32
|
+ """CRUD Controller allowing to manage groups of a user."""
|
44
|
33
|
|
45
|
34
|
allow_only = predicates.in_group(Group.TIM_ADMIN_GROUPNAME)
|
46
|
35
|
|
|
@@ -51,14 +40,15 @@ class UserProfileAdminRestController(TIMRestController):
|
51
|
40
|
@property
|
52
|
41
|
def allowed_profiles(self):
|
53
|
42
|
return [
|
54
|
|
- UserProfileAdminRestController._ALLOWED_PROFILE_USER,
|
55
|
|
- UserProfileAdminRestController._ALLOWED_PROFILE_MANAGER,
|
56
|
|
- UserProfileAdminRestController._ALLOWED_PROFILE_ADMIN
|
57
|
|
- ]
|
|
43
|
+ UserProfileAdminRestController._ALLOWED_PROFILE_USER,
|
|
44
|
+ UserProfileAdminRestController._ALLOWED_PROFILE_MANAGER,
|
|
45
|
+ UserProfileAdminRestController._ALLOWED_PROFILE_ADMIN,
|
|
46
|
+ ]
|
58
|
47
|
|
59
|
48
|
def _before(self, *args, **kw):
|
60
|
49
|
"""
|
61
|
|
- Instantiate the current workspace in tg.tmpl_context
|
|
50
|
+ Instantiate the current workspace in tg.tmpl_context.
|
|
51
|
+
|
62
|
52
|
:param args:
|
63
|
53
|
:param kw:
|
64
|
54
|
:return:
|
|
@@ -72,10 +62,14 @@ class UserProfileAdminRestController(TIMRestController):
|
72
|
62
|
tg.tmpl_context.user = user
|
73
|
63
|
|
74
|
64
|
@tg.expose()
|
75
|
|
- def switch(self, new_role):
|
|
65
|
+ def switch(self, new_role) -> None:
|
76
|
66
|
"""
|
77
|
|
- :param new_role: value should be 'tracim-user', 'tracim-manager' (allowed to create workspaces) or 'tracim-admin' (admin the whole system)
|
78
|
|
- :return:
|
|
67
|
+ Switch to the given new role.
|
|
68
|
+
|
|
69
|
+ :param new_role: value should be:
|
|
70
|
+ 'tracim-user',
|
|
71
|
+ 'tracim-manager' (allowed to create workspaces) or
|
|
72
|
+ 'tracim-admin' (admin the whole system)
|
79
|
73
|
"""
|
80
|
74
|
return self.put(new_role)
|
81
|
75
|
|
|
@@ -87,11 +81,10 @@ class UserProfileAdminRestController(TIMRestController):
|
87
|
81
|
|
88
|
82
|
group_api = GroupApi(current_user)
|
89
|
83
|
|
90
|
|
- if current_user.user_id==user.user_id:
|
|
84
|
+ if current_user.user_id == user.user_id:
|
91
|
85
|
tg.flash(_('You can\'t change your own profile'), CST.STATUS_ERROR)
|
92
|
86
|
tg.redirect(self.parent_controller.url())
|
93
|
87
|
|
94
|
|
-
|
95
|
88
|
redirect_url = self.parent_controller.url(skip_id=True)
|
96
|
89
|
|
97
|
90
|
if new_profile not in self.allowed_profiles:
|
|
@@ -102,9 +95,10 @@ class UserProfileAdminRestController(TIMRestController):
|
102
|
95
|
pod_manager_group = group_api.get_one(Group.TIM_MANAGER)
|
103
|
96
|
pod_admin_group = group_api.get_one(Group.TIM_ADMIN)
|
104
|
97
|
|
105
|
|
- flash_message = _('User updated.') # this is the default value ; should never appear
|
|
98
|
+ # this is the default value ; should never appear
|
|
99
|
+ flash_message = _('User updated.')
|
106
|
100
|
|
107
|
|
- if new_profile==UserProfileAdminRestController._ALLOWED_PROFILE_USER:
|
|
101
|
+ if new_profile == UserProfileAdminRestController._ALLOWED_PROFILE_USER:
|
108
|
102
|
if pod_user_group not in user.groups:
|
109
|
103
|
user.groups.append(pod_user_group)
|
110
|
104
|
|
|
@@ -120,7 +114,7 @@ class UserProfileAdminRestController(TIMRestController):
|
120
|
114
|
|
121
|
115
|
flash_message = _('User {} is now a basic user').format(user.get_display_name())
|
122
|
116
|
|
123
|
|
- elif new_profile==UserProfileAdminRestController._ALLOWED_PROFILE_MANAGER:
|
|
117
|
+ elif new_profile == UserProfileAdminRestController._ALLOWED_PROFILE_MANAGER:
|
124
|
118
|
if pod_user_group not in user.groups:
|
125
|
119
|
user.groups.append(pod_user_group)
|
126
|
120
|
if pod_manager_group not in user.groups:
|
|
@@ -133,8 +127,7 @@ class UserProfileAdminRestController(TIMRestController):
|
133
|
127
|
|
134
|
128
|
flash_message = _('User {} can now workspaces').format(user.get_display_name())
|
135
|
129
|
|
136
|
|
-
|
137
|
|
- elif new_profile==UserProfileAdminRestController._ALLOWED_PROFILE_ADMIN:
|
|
130
|
+ elif new_profile == UserProfileAdminRestController._ALLOWED_PROFILE_ADMIN:
|
138
|
131
|
if pod_user_group not in user.groups:
|
139
|
132
|
user.groups.append(pod_user_group)
|
140
|
133
|
if pod_manager_group not in user.groups:
|
|
@@ -145,7 +138,9 @@ class UserProfileAdminRestController(TIMRestController):
|
145
|
138
|
flash_message = _('User {} is now an administrator').format(user.get_display_name())
|
146
|
139
|
|
147
|
140
|
else:
|
148
|
|
- logger.error(self, 'Trying to change user {} profile with unexpected profile {}'.format(user.user_id, new_profile))
|
|
141
|
+ error_msg = \
|
|
142
|
+ 'Trying to change user {} profile with unexpected profile {}'
|
|
143
|
+ logger.error(self, error_msg.format(user.user_id, new_profile))
|
149
|
144
|
tg.flash(_('Unknown profile'), CST.STATUS_ERROR)
|
150
|
145
|
tg.redirect(redirect_url)
|
151
|
146
|
|
|
@@ -163,17 +158,18 @@ class UserProfileAdminRestController(TIMRestController):
|
163
|
158
|
pass
|
164
|
159
|
|
165
|
160
|
|
166
|
|
-
|
167
|
161
|
class UserPasswordAdminRestController(TIMRestController):
|
168
|
|
- """
|
169
|
|
- CRUD Controller allowing to manage password of a given user
|
170
|
|
- """
|
|
162
|
+ """CRUD Controller allowing to manage password of a given user."""
|
171
|
163
|
|
172
|
|
- allow_only = predicates.in_any_group(Group.TIM_MANAGER_GROUPNAME, Group.TIM_ADMIN_GROUPNAME)
|
|
164
|
+ allow_only = predicates.in_any_group(
|
|
165
|
+ Group.TIM_MANAGER_GROUPNAME,
|
|
166
|
+ Group.TIM_ADMIN_GROUPNAME,
|
|
167
|
+ )
|
173
|
168
|
|
174
|
169
|
def _before(self, *args, **kw):
|
175
|
170
|
"""
|
176
|
|
- Instantiate the current workspace in tg.tmpl_context
|
|
171
|
+ Instantiate the current workspace in tg.tmpl_context.
|
|
172
|
+
|
177
|
173
|
:param args:
|
178
|
174
|
:param kw:
|
179
|
175
|
:return:
|
|
@@ -186,13 +182,12 @@ class UserPasswordAdminRestController(TIMRestController):
|
186
|
182
|
tg.tmpl_context.user_id = user_id
|
187
|
183
|
tg.tmpl_context.user = user
|
188
|
184
|
|
189
|
|
-
|
190
|
185
|
@tg.expose('tracim.templates.admin.user_password_edit')
|
191
|
186
|
def edit(self):
|
192
|
187
|
current_user = tmpl_context.current_user
|
193
|
188
|
api = UserApi(current_user)
|
194
|
189
|
dictified_user = Context(CTX.USER).toDict(tmpl_context.user, 'user')
|
195
|
|
- return DictLikeClass(result = dictified_user)
|
|
190
|
+ return DictLikeClass(result=dictified_user)
|
196
|
191
|
|
197
|
192
|
@tg.expose()
|
198
|
193
|
def put(self, new_password1, new_password2, next_url=''):
|
|
@@ -207,12 +202,11 @@ class UserPasswordAdminRestController(TIMRestController):
|
207
|
202
|
tg.flash(_('Empty password is not allowed.'), CST.STATUS_ERROR)
|
208
|
203
|
tg.redirect(next_url)
|
209
|
204
|
|
210
|
|
- if new_password1!=new_password2:
|
|
205
|
+ if new_password1 != new_password2:
|
211
|
206
|
tg.flash(_('New passwords do not match.'), CST.STATUS_ERROR)
|
212
|
207
|
tg.redirect(next_url)
|
213
|
208
|
|
214
|
209
|
user.password = new_password1
|
215
|
|
- user.update_webdav_digest_auth(new_password1)
|
216
|
210
|
pm.DBSession.flush()
|
217
|
211
|
|
218
|
212
|
tg.flash(_('The password has been changed'), CST.STATUS_OK)
|
|
@@ -223,7 +217,8 @@ class UserWorkspaceRestController(TIMRestController):
|
223
|
217
|
|
224
|
218
|
def _before(self, *args, **kw):
|
225
|
219
|
"""
|
226
|
|
- Instantiate the current workspace in tg.tmpl_context
|
|
220
|
+ Instantiate the current workspace in tg.tmpl_context.
|
|
221
|
+
|
227
|
222
|
:param args:
|
228
|
223
|
:param kw:
|
229
|
224
|
:return:
|
|
@@ -266,10 +261,12 @@ class UserWorkspaceRestController(TIMRestController):
|
266
|
261
|
|
267
|
262
|
|
268
|
263
|
class UserRestController(TIMRestController):
|
269
|
|
- """
|
270
|
|
- CRUD Controller allowing to manage Users
|
271
|
|
- """
|
272
|
|
- allow_only = predicates.in_any_group(Group.TIM_MANAGER_GROUPNAME, Group.TIM_ADMIN_GROUPNAME)
|
|
264
|
+ """CRUD Controller allowing to manage Users."""
|
|
265
|
+
|
|
266
|
+ allow_only = predicates.in_any_group(
|
|
267
|
+ Group.TIM_MANAGER_GROUPNAME,
|
|
268
|
+ Group.TIM_ADMIN_GROUPNAME,
|
|
269
|
+ )
|
273
|
270
|
|
274
|
271
|
password = UserPasswordAdminRestController()
|
275
|
272
|
profile = UserProfileAdminRestController()
|
|
@@ -284,7 +281,6 @@ class UserRestController(TIMRestController):
|
284
|
281
|
def current_item_id_key_in_context(cls):
|
285
|
282
|
return 'user_id'
|
286
|
283
|
|
287
|
|
-
|
288
|
284
|
@tg.require(predicates.in_group(Group.TIM_MANAGER_GROUPNAME))
|
289
|
285
|
@tg.expose('tracim.templates.admin.user_getall')
|
290
|
286
|
def get_all(self, *args, **kw):
|
|
@@ -297,7 +293,7 @@ class UserRestController(TIMRestController):
|
297
|
293
|
fake_api = Context(CTX.USERS).toDict({'current_user': current_user_content})
|
298
|
294
|
|
299
|
295
|
dictified_users = Context(CTX.USERS).toDict(users, 'users', 'user_nb')
|
300
|
|
- return DictLikeClass(result = dictified_users, fake_api=fake_api)
|
|
296
|
+ return DictLikeClass(result=dictified_users, fake_api=fake_api)
|
301
|
297
|
|
302
|
298
|
@tg.require(predicates.in_group(Group.TIM_MANAGER_GROUPNAME))
|
303
|
299
|
@tg.expose()
|
|
@@ -336,8 +332,6 @@ class UserRestController(TIMRestController):
|
336
|
332
|
password = self.generate_password()
|
337
|
333
|
user.password = password
|
338
|
334
|
|
339
|
|
- user.webdav_left_digest_response_hash = '%s:/:%s' % (email, password)
|
340
|
|
-
|
341
|
335
|
api.save(user)
|
342
|
336
|
|
343
|
337
|
# Now add the user to related groups
|
|
@@ -361,14 +355,13 @@ class UserRestController(TIMRestController):
|
361
|
355
|
@classmethod
|
362
|
356
|
def generate_password(
|
363
|
357
|
cls,
|
364
|
|
- password_length = PASSWORD_LENGTH,
|
365
|
|
- password_chars = PASSWORD_CHARACTERS
|
366
|
|
- ):
|
367
|
|
-
|
|
358
|
+ password_length=PASSWORD_LENGTH,
|
|
359
|
+ password_chars=PASSWORD_CHARACTERS,
|
|
360
|
+ ):
|
368
|
361
|
# character list that will be contained into the password
|
369
|
362
|
char_list = []
|
370
|
363
|
|
371
|
|
- for j in range(0, password_length):
|
|
364
|
+ for _unused in range(password_length):
|
372
|
365
|
# This puts a random char from the list above inside
|
373
|
366
|
# the list of chars and then merges them into a String
|
374
|
367
|
char_list.append(random.choice(password_chars))
|
|
@@ -378,11 +371,11 @@ class UserRestController(TIMRestController):
|
378
|
371
|
@tg.expose('tracim.templates.admin.user_getone')
|
379
|
372
|
def get_one(self, user_id):
|
380
|
373
|
current_user = tmpl_context.current_user
|
381
|
|
- api = UserApi(current_user )
|
|
374
|
+ api = UserApi(current_user)
|
382
|
375
|
# role_api = RoleApi(tg.tmpl_context.current_user)
|
383
|
376
|
# user_api = UserApi(tg.tmpl_context.current_user)
|
384
|
377
|
|
385
|
|
- user = api.get_one(user_id) # FIXME
|
|
378
|
+ user = api.get_one(user_id) # FIXME
|
386
|
379
|
|
387
|
380
|
role_api = RoleApi(tg.tmpl_context.current_user)
|
388
|
381
|
role_list = role_api.get_roles_for_select_field()
|
|
@@ -393,8 +386,7 @@ class UserRestController(TIMRestController):
|
393
|
386
|
role_types=role_list)
|
394
|
387
|
fake_api = Context(CTX.ADMIN_USER).toDict(fake_api_content)
|
395
|
388
|
|
396
|
|
- return DictLikeClass(result = dictified_user, fake_api=fake_api)
|
397
|
|
-
|
|
389
|
+ return DictLikeClass(result=dictified_user, fake_api=fake_api)
|
398
|
390
|
|
399
|
391
|
@tg.expose('tracim.templates.admin.user_edit')
|
400
|
392
|
def edit(self, id):
|
|
@@ -422,7 +414,6 @@ class UserRestController(TIMRestController):
|
422
|
414
|
tg.redirect(next_url)
|
423
|
415
|
tg.redirect(self.url())
|
424
|
416
|
|
425
|
|
-
|
426
|
417
|
@tg.require(predicates.in_group(Group.TIM_ADMIN_GROUPNAME))
|
427
|
418
|
@tg.expose()
|
428
|
419
|
def enable(self, id, next_url=None):
|
|
@@ -434,7 +425,7 @@ class UserRestController(TIMRestController):
|
434
|
425
|
api.save(user)
|
435
|
426
|
|
436
|
427
|
tg.flash(_('User {} enabled.').format(user.get_display_name()), CST.STATUS_OK)
|
437
|
|
- if next_url=='user':
|
|
428
|
+ if next_url == 'user':
|
438
|
429
|
tg.redirect(self.url(id=user.user_id))
|
439
|
430
|
tg.redirect(self.url())
|
440
|
431
|
|
|
@@ -445,7 +436,7 @@ class UserRestController(TIMRestController):
|
445
|
436
|
current_user = tmpl_context.current_user
|
446
|
437
|
api = UserApi(current_user)
|
447
|
438
|
|
448
|
|
- if current_user.user_id==id:
|
|
439
|
+ if current_user.user_id == id:
|
449
|
440
|
tg.flash(_('You can\'t de-activate your own account'), CST.STATUS_ERROR)
|
450
|
441
|
else:
|
451
|
442
|
user = api.get_one(id)
|
|
@@ -453,6 +444,6 @@ class UserRestController(TIMRestController):
|
453
|
444
|
api.save(user)
|
454
|
445
|
tg.flash(_('User {} disabled').format(user.get_display_name()), CST.STATUS_OK)
|
455
|
446
|
|
456
|
|
- if next_url=='user':
|
|
447
|
+ if next_url == 'user':
|
457
|
448
|
tg.redirect(self.url(id=user.user_id))
|
458
|
449
|
tg.redirect(self.url())
|