api: enable setting sync flag for user groups on create/edit.
This commit is contained in:
parent
89bf42d803
commit
3949688b3d
5 changed files with 68 additions and 10 deletions
|
|
@ -36,7 +36,9 @@ class TestUpdateUserGroup(object):
|
|||
# ('owner', {'owner': TEST_USER_REGULAR_LOGIN}),
|
||||
('owner_email', {'owner_email': TEST_USER_ADMIN_EMAIL}),
|
||||
('active', {'active': False}),
|
||||
('active', {'active': True})
|
||||
('active', {'active': True}),
|
||||
('sync', {'sync': False}),
|
||||
('sync', {'sync': True})
|
||||
])
|
||||
def test_api_update_user_group(self, changing_attr, updates, user_util):
|
||||
user_group = user_util.create_user_group()
|
||||
|
|
@ -49,6 +51,12 @@ class TestUpdateUserGroup(object):
|
|||
**updates)
|
||||
response = api_call(self.app, params)
|
||||
|
||||
# special case for sync
|
||||
if changing_attr == 'sync' and updates['sync'] is False:
|
||||
expected_api_data['sync'] = None
|
||||
elif changing_attr == 'sync' and updates['sync'] is True:
|
||||
expected_api_data['sync'] = 'manual_api'
|
||||
|
||||
expected = {
|
||||
'msg': 'updated user group ID:%s %s' % (
|
||||
user_group.users_group_id, user_group.users_group_name),
|
||||
|
|
@ -63,7 +71,9 @@ class TestUpdateUserGroup(object):
|
|||
# ('owner', {'owner': TEST_USER_REGULAR_LOGIN}),
|
||||
('owner_email', {'owner_email': TEST_USER_ADMIN_EMAIL}),
|
||||
('active', {'active': False}),
|
||||
('active', {'active': True})
|
||||
('active', {'active': True}),
|
||||
('sync', {'sync': False}),
|
||||
('sync', {'sync': True})
|
||||
])
|
||||
def test_api_update_user_group_regular_user(
|
||||
self, changing_attr, updates, user_util):
|
||||
|
|
@ -72,7 +82,6 @@ class TestUpdateUserGroup(object):
|
|||
expected_api_data = user_group.get_api_data()
|
||||
expected_api_data.update(updates)
|
||||
|
||||
|
||||
# grant permission to this user
|
||||
user = UserModel().get_by_username(self.TEST_USER_LOGIN)
|
||||
|
||||
|
|
@ -82,6 +91,12 @@ class TestUpdateUserGroup(object):
|
|||
self.apikey_regular, 'update_user_group',
|
||||
usergroupid=group_name, **updates)
|
||||
response = api_call(self.app, params)
|
||||
# special case for sync
|
||||
if changing_attr == 'sync' and updates['sync'] is False:
|
||||
expected_api_data['sync'] = None
|
||||
elif changing_attr == 'sync' and updates['sync'] is True:
|
||||
expected_api_data['sync'] = 'manual_api'
|
||||
|
||||
expected = {
|
||||
'msg': 'updated user group ID:%s %s' % (
|
||||
user_group.users_group_id, user_group.users_group_name),
|
||||
|
|
|
|||
|
|
@ -168,7 +168,8 @@ def get_user_groups(request, apiuser):
|
|||
@jsonrpc_method()
|
||||
def create_user_group(
|
||||
request, apiuser, group_name, description=Optional(''),
|
||||
owner=Optional(OAttr('apiuser')), active=Optional(True)):
|
||||
owner=Optional(OAttr('apiuser')), active=Optional(True),
|
||||
sync=Optional(None)):
|
||||
"""
|
||||
Creates a new user group.
|
||||
|
||||
|
|
@ -188,6 +189,9 @@ def create_user_group(
|
|||
:type owner: Optional(str or int)
|
||||
:param active: Set this group as active.
|
||||
:type active: Optional(``True`` | ``False``)
|
||||
:param sync: Set enabled or disabled the automatically sync from
|
||||
external authentication types like ldap.
|
||||
:type sync: Optional(``True`` | ``False``)
|
||||
|
||||
Example output:
|
||||
|
||||
|
|
@ -227,6 +231,15 @@ def create_user_group(
|
|||
owner = get_user_or_error(owner)
|
||||
active = Optional.extract(active)
|
||||
description = Optional.extract(description)
|
||||
sync = Optional.extract(sync)
|
||||
|
||||
# set the sync option based on group_data
|
||||
group_data = None
|
||||
if sync:
|
||||
group_data = {
|
||||
'extern_type': 'manual_api',
|
||||
'extern_type_set_by': apiuser.username
|
||||
}
|
||||
|
||||
schema = user_group_schema.UserGroupSchema().bind(
|
||||
# user caller
|
||||
|
|
@ -246,7 +259,7 @@ def create_user_group(
|
|||
name=schema_data['user_group_name'],
|
||||
description=schema_data['user_group_description'],
|
||||
owner=owner,
|
||||
active=schema_data['user_group_active'])
|
||||
active=schema_data['user_group_active'], group_data=group_data)
|
||||
Session().flush()
|
||||
creation_data = user_group.get_api_data()
|
||||
audit_logger.store_api(
|
||||
|
|
@ -265,7 +278,7 @@ def create_user_group(
|
|||
@jsonrpc_method()
|
||||
def update_user_group(request, apiuser, usergroupid, group_name=Optional(''),
|
||||
description=Optional(''), owner=Optional(None),
|
||||
active=Optional(True)):
|
||||
active=Optional(True), sync=Optional(None)):
|
||||
"""
|
||||
Updates the specified `user group` with the details provided.
|
||||
|
||||
|
|
@ -284,6 +297,9 @@ def update_user_group(request, apiuser, usergroupid, group_name=Optional(''),
|
|||
:type owner: Optional(str or int)
|
||||
:param active: Set the group as active.
|
||||
:type active: Optional(``True`` | ``False``)
|
||||
:param sync: Set enabled or disabled the automatically sync from
|
||||
external authentication types like ldap.
|
||||
:type sync: Optional(``True`` | ``False``)
|
||||
|
||||
Example output:
|
||||
|
||||
|
|
@ -329,8 +345,21 @@ def update_user_group(request, apiuser, usergroupid, group_name=Optional(''),
|
|||
store_update(updates, description, 'user_group_description')
|
||||
store_update(updates, owner, 'user')
|
||||
store_update(updates, active, 'users_group_active')
|
||||
|
||||
sync = Optional.extract(sync)
|
||||
group_data = None
|
||||
if sync is True:
|
||||
group_data = {
|
||||
'extern_type': 'manual_api',
|
||||
'extern_type_set_by': apiuser.username
|
||||
}
|
||||
if sync is False:
|
||||
group_data = user_group.group_data
|
||||
if group_data and "extern_type" in group_data:
|
||||
del group_data["extern_type"]
|
||||
|
||||
try:
|
||||
UserGroupModel().update(user_group, updates)
|
||||
UserGroupModel().update(user_group, updates, group_data=group_data)
|
||||
audit_logger.store_api(
|
||||
'user_group.edit', action_data={'old_data': old_data},
|
||||
user=apiuser)
|
||||
|
|
|
|||
|
|
@ -159,8 +159,7 @@ class AdminUserGroupsView(BaseAppView, DataGridAppView):
|
|||
"members": user_gr.member_count,
|
||||
# NOTE(marcink): because of advanced query we
|
||||
# need to load it like that
|
||||
"sync": UserGroup._load_group_data(
|
||||
user_gr.group_data).get('extern_type'),
|
||||
"sync": UserGroup._load_sync(user_gr.group_data),
|
||||
"active": h.bool2icon(user_gr.users_group_active),
|
||||
"owner": user_profile(user_gr.User.username),
|
||||
"action": user_group_actions(
|
||||
|
|
|
|||
|
|
@ -1344,6 +1344,15 @@ class UserGroup(Base, BaseModel):
|
|||
except Exception:
|
||||
log.error(traceback.format_exc())
|
||||
|
||||
@classmethod
|
||||
def _load_sync(cls, group_data):
|
||||
if group_data:
|
||||
return group_data.get('extern_type')
|
||||
|
||||
@property
|
||||
def sync(self):
|
||||
return self._load_sync(self.group_data)
|
||||
|
||||
def __unicode__(self):
|
||||
return u"<%s('id:%s:%s')>" % (self.__class__.__name__,
|
||||
self.users_group_id,
|
||||
|
|
@ -1453,6 +1462,7 @@ class UserGroup(Base, BaseModel):
|
|||
'group_description': user_group.user_group_description,
|
||||
'active': user_group.users_group_active,
|
||||
'owner': user_group.user.username,
|
||||
'sync': user_group.sync,
|
||||
'owner_email': user_group.user.email,
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -217,7 +217,7 @@ class UserGroupModel(BaseModel):
|
|||
members.append(uid)
|
||||
return members
|
||||
|
||||
def update(self, user_group, form_data):
|
||||
def update(self, user_group, form_data, group_data=None):
|
||||
user_group = self._get_user_group(user_group)
|
||||
if 'users_group_name' in form_data:
|
||||
user_group.users_group_name = form_data['users_group_name']
|
||||
|
|
@ -247,6 +247,11 @@ class UserGroupModel(BaseModel):
|
|||
added_user_ids, removed_user_ids = \
|
||||
self._update_members_from_user_ids(user_group, members_id_list)
|
||||
|
||||
if group_data:
|
||||
new_group_data = {}
|
||||
new_group_data.update(group_data)
|
||||
user_group.group_data = new_group_data
|
||||
|
||||
self.sa.add(user_group)
|
||||
return user_group, added_user_ids, removed_user_ids
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue