##// END OF EJS Templates
api: enable setting sync flag for user groups on create/edit.
marcink -
r2660:499461c1 default
parent child Browse files
Show More
@@ -36,7 +36,9 b' class TestUpdateUserGroup(object):'
36 # ('owner', {'owner': TEST_USER_REGULAR_LOGIN}),
36 # ('owner', {'owner': TEST_USER_REGULAR_LOGIN}),
37 ('owner_email', {'owner_email': TEST_USER_ADMIN_EMAIL}),
37 ('owner_email', {'owner_email': TEST_USER_ADMIN_EMAIL}),
38 ('active', {'active': False}),
38 ('active', {'active': False}),
39 ('active', {'active': True})
39 ('active', {'active': True}),
40 ('sync', {'sync': False}),
41 ('sync', {'sync': True})
40 ])
42 ])
41 def test_api_update_user_group(self, changing_attr, updates, user_util):
43 def test_api_update_user_group(self, changing_attr, updates, user_util):
42 user_group = user_util.create_user_group()
44 user_group = user_util.create_user_group()
@@ -49,6 +51,12 b' class TestUpdateUserGroup(object):'
49 **updates)
51 **updates)
50 response = api_call(self.app, params)
52 response = api_call(self.app, params)
51
53
54 # special case for sync
55 if changing_attr == 'sync' and updates['sync'] is False:
56 expected_api_data['sync'] = None
57 elif changing_attr == 'sync' and updates['sync'] is True:
58 expected_api_data['sync'] = 'manual_api'
59
52 expected = {
60 expected = {
53 'msg': 'updated user group ID:%s %s' % (
61 'msg': 'updated user group ID:%s %s' % (
54 user_group.users_group_id, user_group.users_group_name),
62 user_group.users_group_id, user_group.users_group_name),
@@ -63,7 +71,9 b' class TestUpdateUserGroup(object):'
63 # ('owner', {'owner': TEST_USER_REGULAR_LOGIN}),
71 # ('owner', {'owner': TEST_USER_REGULAR_LOGIN}),
64 ('owner_email', {'owner_email': TEST_USER_ADMIN_EMAIL}),
72 ('owner_email', {'owner_email': TEST_USER_ADMIN_EMAIL}),
65 ('active', {'active': False}),
73 ('active', {'active': False}),
66 ('active', {'active': True})
74 ('active', {'active': True}),
75 ('sync', {'sync': False}),
76 ('sync', {'sync': True})
67 ])
77 ])
68 def test_api_update_user_group_regular_user(
78 def test_api_update_user_group_regular_user(
69 self, changing_attr, updates, user_util):
79 self, changing_attr, updates, user_util):
@@ -72,7 +82,6 b' class TestUpdateUserGroup(object):'
72 expected_api_data = user_group.get_api_data()
82 expected_api_data = user_group.get_api_data()
73 expected_api_data.update(updates)
83 expected_api_data.update(updates)
74
84
75
76 # grant permission to this user
85 # grant permission to this user
77 user = UserModel().get_by_username(self.TEST_USER_LOGIN)
86 user = UserModel().get_by_username(self.TEST_USER_LOGIN)
78
87
@@ -82,6 +91,12 b' class TestUpdateUserGroup(object):'
82 self.apikey_regular, 'update_user_group',
91 self.apikey_regular, 'update_user_group',
83 usergroupid=group_name, **updates)
92 usergroupid=group_name, **updates)
84 response = api_call(self.app, params)
93 response = api_call(self.app, params)
94 # special case for sync
95 if changing_attr == 'sync' and updates['sync'] is False:
96 expected_api_data['sync'] = None
97 elif changing_attr == 'sync' and updates['sync'] is True:
98 expected_api_data['sync'] = 'manual_api'
99
85 expected = {
100 expected = {
86 'msg': 'updated user group ID:%s %s' % (
101 'msg': 'updated user group ID:%s %s' % (
87 user_group.users_group_id, user_group.users_group_name),
102 user_group.users_group_id, user_group.users_group_name),
@@ -168,7 +168,8 b' def get_user_groups(request, apiuser):'
168 @jsonrpc_method()
168 @jsonrpc_method()
169 def create_user_group(
169 def create_user_group(
170 request, apiuser, group_name, description=Optional(''),
170 request, apiuser, group_name, description=Optional(''),
171 owner=Optional(OAttr('apiuser')), active=Optional(True)):
171 owner=Optional(OAttr('apiuser')), active=Optional(True),
172 sync=Optional(None)):
172 """
173 """
173 Creates a new user group.
174 Creates a new user group.
174
175
@@ -188,6 +189,9 b' def create_user_group('
188 :type owner: Optional(str or int)
189 :type owner: Optional(str or int)
189 :param active: Set this group as active.
190 :param active: Set this group as active.
190 :type active: Optional(``True`` | ``False``)
191 :type active: Optional(``True`` | ``False``)
192 :param sync: Set enabled or disabled the automatically sync from
193 external authentication types like ldap.
194 :type sync: Optional(``True`` | ``False``)
191
195
192 Example output:
196 Example output:
193
197
@@ -227,6 +231,15 b' def create_user_group('
227 owner = get_user_or_error(owner)
231 owner = get_user_or_error(owner)
228 active = Optional.extract(active)
232 active = Optional.extract(active)
229 description = Optional.extract(description)
233 description = Optional.extract(description)
234 sync = Optional.extract(sync)
235
236 # set the sync option based on group_data
237 group_data = None
238 if sync:
239 group_data = {
240 'extern_type': 'manual_api',
241 'extern_type_set_by': apiuser.username
242 }
230
243
231 schema = user_group_schema.UserGroupSchema().bind(
244 schema = user_group_schema.UserGroupSchema().bind(
232 # user caller
245 # user caller
@@ -246,7 +259,7 b' def create_user_group('
246 name=schema_data['user_group_name'],
259 name=schema_data['user_group_name'],
247 description=schema_data['user_group_description'],
260 description=schema_data['user_group_description'],
248 owner=owner,
261 owner=owner,
249 active=schema_data['user_group_active'])
262 active=schema_data['user_group_active'], group_data=group_data)
250 Session().flush()
263 Session().flush()
251 creation_data = user_group.get_api_data()
264 creation_data = user_group.get_api_data()
252 audit_logger.store_api(
265 audit_logger.store_api(
@@ -265,7 +278,7 b' def create_user_group('
265 @jsonrpc_method()
278 @jsonrpc_method()
266 def update_user_group(request, apiuser, usergroupid, group_name=Optional(''),
279 def update_user_group(request, apiuser, usergroupid, group_name=Optional(''),
267 description=Optional(''), owner=Optional(None),
280 description=Optional(''), owner=Optional(None),
268 active=Optional(True)):
281 active=Optional(True), sync=Optional(None)):
269 """
282 """
270 Updates the specified `user group` with the details provided.
283 Updates the specified `user group` with the details provided.
271
284
@@ -284,6 +297,9 b' def update_user_group(request, apiuser, '
284 :type owner: Optional(str or int)
297 :type owner: Optional(str or int)
285 :param active: Set the group as active.
298 :param active: Set the group as active.
286 :type active: Optional(``True`` | ``False``)
299 :type active: Optional(``True`` | ``False``)
300 :param sync: Set enabled or disabled the automatically sync from
301 external authentication types like ldap.
302 :type sync: Optional(``True`` | ``False``)
287
303
288 Example output:
304 Example output:
289
305
@@ -329,8 +345,21 b' def update_user_group(request, apiuser, '
329 store_update(updates, description, 'user_group_description')
345 store_update(updates, description, 'user_group_description')
330 store_update(updates, owner, 'user')
346 store_update(updates, owner, 'user')
331 store_update(updates, active, 'users_group_active')
347 store_update(updates, active, 'users_group_active')
348
349 sync = Optional.extract(sync)
350 group_data = None
351 if sync is True:
352 group_data = {
353 'extern_type': 'manual_api',
354 'extern_type_set_by': apiuser.username
355 }
356 if sync is False:
357 group_data = user_group.group_data
358 if group_data and "extern_type" in group_data:
359 del group_data["extern_type"]
360
332 try:
361 try:
333 UserGroupModel().update(user_group, updates)
362 UserGroupModel().update(user_group, updates, group_data=group_data)
334 audit_logger.store_api(
363 audit_logger.store_api(
335 'user_group.edit', action_data={'old_data': old_data},
364 'user_group.edit', action_data={'old_data': old_data},
336 user=apiuser)
365 user=apiuser)
@@ -159,8 +159,7 b' class AdminUserGroupsView(BaseAppView, D'
159 "members": user_gr.member_count,
159 "members": user_gr.member_count,
160 # NOTE(marcink): because of advanced query we
160 # NOTE(marcink): because of advanced query we
161 # need to load it like that
161 # need to load it like that
162 "sync": UserGroup._load_group_data(
162 "sync": UserGroup._load_sync(user_gr.group_data),
163 user_gr.group_data).get('extern_type'),
164 "active": h.bool2icon(user_gr.users_group_active),
163 "active": h.bool2icon(user_gr.users_group_active),
165 "owner": user_profile(user_gr.User.username),
164 "owner": user_profile(user_gr.User.username),
166 "action": user_group_actions(
165 "action": user_group_actions(
@@ -1344,6 +1344,15 b' class UserGroup(Base, BaseModel):'
1344 except Exception:
1344 except Exception:
1345 log.error(traceback.format_exc())
1345 log.error(traceback.format_exc())
1346
1346
1347 @classmethod
1348 def _load_sync(cls, group_data):
1349 if group_data:
1350 return group_data.get('extern_type')
1351
1352 @property
1353 def sync(self):
1354 return self._load_sync(self.group_data)
1355
1347 def __unicode__(self):
1356 def __unicode__(self):
1348 return u"<%s('id:%s:%s')>" % (self.__class__.__name__,
1357 return u"<%s('id:%s:%s')>" % (self.__class__.__name__,
1349 self.users_group_id,
1358 self.users_group_id,
@@ -1453,6 +1462,7 b' class UserGroup(Base, BaseModel):'
1453 'group_description': user_group.user_group_description,
1462 'group_description': user_group.user_group_description,
1454 'active': user_group.users_group_active,
1463 'active': user_group.users_group_active,
1455 'owner': user_group.user.username,
1464 'owner': user_group.user.username,
1465 'sync': user_group.sync,
1456 'owner_email': user_group.user.email,
1466 'owner_email': user_group.user.email,
1457 }
1467 }
1458
1468
@@ -217,7 +217,7 b' class UserGroupModel(BaseModel):'
217 members.append(uid)
217 members.append(uid)
218 return members
218 return members
219
219
220 def update(self, user_group, form_data):
220 def update(self, user_group, form_data, group_data=None):
221 user_group = self._get_user_group(user_group)
221 user_group = self._get_user_group(user_group)
222 if 'users_group_name' in form_data:
222 if 'users_group_name' in form_data:
223 user_group.users_group_name = form_data['users_group_name']
223 user_group.users_group_name = form_data['users_group_name']
@@ -247,6 +247,11 b' class UserGroupModel(BaseModel):'
247 added_user_ids, removed_user_ids = \
247 added_user_ids, removed_user_ids = \
248 self._update_members_from_user_ids(user_group, members_id_list)
248 self._update_members_from_user_ids(user_group, members_id_list)
249
249
250 if group_data:
251 new_group_data = {}
252 new_group_data.update(group_data)
253 user_group.group_data = new_group_data
254
250 self.sa.add(user_group)
255 self.sa.add(user_group)
251 return user_group, added_user_ids, removed_user_ids
256 return user_group, added_user_ids, removed_user_ids
252
257
General Comments 0
You need to be logged in to leave comments. Login now