def test_enforce_groups(self, pre_existing, regular_should_be, external_should_be, groups, expected): # delete all groups for gr in UserGroup.query(): fixture.destroy_user_group(gr) Session().commit() user = User.get_by_username(TEST_USER_REGULAR_LOGIN) for gr in pre_existing: gr = fixture.create_user_group(gr) Session().commit() # make sure use is just in those groups for gr in regular_should_be: gr = fixture.create_user_group(gr) Session().commit() UserGroupModel().add_user_to_group(gr, user) Session().commit() # now special external groups created by auth plugins for gr in external_should_be: gr = fixture.create_user_group(gr, user_group_data={'extern_type': 'container'}) Session().commit() UserGroupModel().add_user_to_group(gr, user) Session().commit() UserGroupModel().enforce_groups(user, groups, 'container') Session().commit() user = User.get_by_username(TEST_USER_REGULAR_LOGIN) in_groups = user.group_member assert expected == [x.users_group.users_group_name for x in in_groups]
def test_enforce_groups(self, pre_existing, regular_should_be, external_should_be, groups, expected): # delete all groups for gr in UserGroup.query(): fixture.destroy_user_group(gr) Session().commit() user = User.get_by_username(base.TEST_USER_REGULAR_LOGIN) for gr in pre_existing: gr = fixture.create_user_group(gr) Session().commit() # make sure use is just in those groups for gr in regular_should_be: gr = fixture.create_user_group(gr) Session().commit() UserGroupModel().add_user_to_group(gr, user) Session().commit() # now special external groups created by auth plugins for gr in external_should_be: gr = fixture.create_user_group( gr, user_group_data={'extern_type': 'container'}) Session().commit() UserGroupModel().add_user_to_group(gr, user) Session().commit() UserGroupModel().enforce_groups(user, groups, 'container') Session().commit() user = User.get_by_username(base.TEST_USER_REGULAR_LOGIN) in_groups = user.group_member assert sorted(expected) == sorted(x.users_group.users_group_name for x in in_groups)
def get_user_groups_js(self): user_groups = UserGroup.query() \ .filter(UserGroup.users_group_active == True) \ .order_by(UserGroup.users_group_name) \ .options(subqueryload(UserGroup.members)) \ .all() user_groups = UserGroupList(user_groups, perm_level='read') return [{ 'id': gr.users_group_id, 'grname': gr.users_group_name, 'grmembers': len(gr.members), } for gr in user_groups]
def get_user_groups_js(self): user_groups = UserGroup.query() \ .filter(UserGroup.users_group_active == True) \ .order_by(UserGroup.users_group_name) \ .options(subqueryload(UserGroup.members)) \ .all() user_groups = UserGroupList(user_groups, perm_level='read') return [ { 'id': gr.users_group_id, 'grname': gr.users_group_name, 'grmembers': len(gr.members), } for gr in user_groups]
def index(self, format='html'): """GET /users_groups: All items in the collection""" # url('users_groups') _list = UserGroup.query()\ .order_by(func.lower(UserGroup.users_group_name))\ .all() group_iter = UserGroupList(_list, perm_set=['usergroup.admin']) user_groups_data = [] total_records = len(group_iter) _tmpl_lookup = kallithea.CONFIG['pylons.app_globals'].mako_lookup template = _tmpl_lookup.get_template('data_table/_dt_elements.html') user_group_name = lambda user_group_id, user_group_name: ( template.get_def("user_group_name").render( user_group_id, user_group_name, _=_, h=h, c=c)) user_group_actions = lambda user_group_id, user_group_name: ( template.get_def("user_group_actions").render( user_group_id, user_group_name, _=_, h=h, c=c)) for user_gr in group_iter: user_groups_data.append({ "raw_name": user_gr.users_group_name, "group_name": user_group_name(user_gr.users_group_id, user_gr.users_group_name), "desc": user_gr.user_group_description, "members": len(user_gr.members), "active": h.boolicon(user_gr.users_group_active), "owner": h.person(user_gr.user.username), "action": user_group_actions(user_gr.users_group_id, user_gr.users_group_name) }) c.data = json.dumps({ "totalRecords": total_records, "startIndex": 0, "sort": None, "dir": "asc", "records": user_groups_data }) return render('admin/user_groups/user_groups.html')
def index(self, format='html'): _list = UserGroup.query() \ .order_by(func.lower(UserGroup.users_group_name)) \ .all() group_iter = UserGroupList(_list, perm_level='admin') user_groups_data = [] total_records = len(group_iter) _tmpl_lookup = app_globals.mako_lookup template = _tmpl_lookup.get_template('data_table/_dt_elements.html') user_group_name = lambda user_group_id, user_group_name: ( template.get_def("user_group_name").render( user_group_id, user_group_name, _=_, h=h, c=c)) user_group_actions = lambda user_group_id, user_group_name: ( template.get_def("user_group_actions").render( user_group_id, user_group_name, _=_, h=h, c=c)) for user_gr in group_iter: user_groups_data.append({ "raw_name": user_gr.users_group_name, "group_name": user_group_name(user_gr.users_group_id, user_gr.users_group_name), "desc": h.escape(user_gr.user_group_description), "members": len(user_gr.members), "active": h.boolicon(user_gr.users_group_active), "owner": h.person(user_gr.owner.username), "action": user_group_actions(user_gr.users_group_id, user_gr.users_group_name) }) c.data = { "totalRecords": total_records, "startIndex": 0, "sort": None, "dir": "asc", "records": user_groups_data } return render('admin/user_groups/user_groups.html')
def index(self, format='html'): """GET /users_groups: All items in the collection""" # url('users_groups') _list = UserGroup.query()\ .order_by(func.lower(UserGroup.users_group_name))\ .all() group_iter = UserGroupList(_list, perm_set=['usergroup.admin']) user_groups_data = [] total_records = len(group_iter) _tmpl_lookup = kallithea.CONFIG['pylons.app_globals'].mako_lookup template = _tmpl_lookup.get_template('data_table/_dt_elements.html') user_group_name = lambda user_group_id, user_group_name: ( template.get_def("user_group_name") .render(user_group_id, user_group_name, _=_, h=h, c=c) ) user_group_actions = lambda user_group_id, user_group_name: ( template.get_def("user_group_actions") .render(user_group_id, user_group_name, _=_, h=h, c=c) ) for user_gr in group_iter: user_groups_data.append({ "raw_name": user_gr.users_group_name, "group_name": user_group_name(user_gr.users_group_id, user_gr.users_group_name), "desc": user_gr.user_group_description, "members": len(user_gr.members), "active": h.boolicon(user_gr.users_group_active), "owner": h.person(user_gr.user.username), "action": user_group_actions(user_gr.users_group_id, user_gr.users_group_name) }) c.data = json.dumps({ "totalRecords": total_records, "startIndex": 0, "sort": None, "dir": "asc", "records": user_groups_data }) return render('admin/user_groups/user_groups.html')
def index(self, format='html'): _list = UserGroup.query() \ .order_by(func.lower(UserGroup.users_group_name)) \ .all() group_iter = UserGroupList(_list, perm_level='admin') user_groups_data = [] total_records = len(group_iter) _tmpl_lookup = app_globals.mako_lookup template = _tmpl_lookup.get_template('data_table/_dt_elements.html') user_group_name = lambda user_group_id, user_group_name: ( template.get_def("user_group_name") .render(user_group_id, user_group_name, _=_, h=h, c=c) ) user_group_actions = lambda user_group_id, user_group_name: ( template.get_def("user_group_actions") .render(user_group_id, user_group_name, _=_, h=h, c=c) ) for user_gr in group_iter: user_groups_data.append({ "raw_name": user_gr.users_group_name, "group_name": user_group_name(user_gr.users_group_id, user_gr.users_group_name), "desc": h.escape(user_gr.user_group_description), "members": len(user_gr.members), "active": h.boolicon(user_gr.users_group_active), "owner": h.person(user_gr.owner.username), "action": user_group_actions(user_gr.users_group_id, user_gr.users_group_name) }) c.data = { "totalRecords": total_records, "startIndex": 0, "sort": None, "dir": "asc", "records": user_groups_data } return render('admin/user_groups/user_groups.html')
def to_python(self, value, state): perms_update = OrderedSet() perms_new = OrderedSet() # build a list of permission to update and new permission to create #CLEAN OUT ORG VALUE FROM NEW MEMBERS, and group them using new_perms_group = defaultdict(dict) for k, v in value.copy().iteritems(): if k.startswith('perm_new_member'): del value[k] _type, part = k.split('perm_new_member_') args = part.split('_') if len(args) == 1: new_perms_group[args[0]]['perm'] = v elif len(args) == 2: _key, pos = args new_perms_group[pos][_key] = v # fill new permissions in order of how they were added for k in sorted(map(int, new_perms_group.keys())): perm_dict = new_perms_group[str(k)] new_member = perm_dict.get('name') new_perm = perm_dict.get('perm') new_type = perm_dict.get('type') if new_member and new_perm and new_type: perms_new.add((new_member, new_perm, new_type)) for k, v in value.iteritems(): if k.startswith('u_perm_') or k.startswith('g_perm_'): member = k[7:] t = {'u': 'user', 'g': 'users_group' }[k[0]] if member == User.DEFAULT_USER: if str2bool(value.get('repo_private')): # set none for default when updating to # private repo protects against form manipulation v = EMPTY_PERM perms_update.add((member, v, t)) value['perms_updates'] = list(perms_update) value['perms_new'] = list(perms_new) # update permissions for k, v, t in perms_new: try: if t is 'user': self.user_db = User.query() \ .filter(User.active == True) \ .filter(User.username == k).one() if t is 'users_group': self.user_db = UserGroup.query() \ .filter(UserGroup.users_group_active == True) \ .filter(UserGroup.users_group_name == k).one() except Exception: log.exception('Updated permission failed') msg = M(self, 'perm_new_member_type', state) raise formencode.Invalid(msg, value, state, error_dict=dict(perm_new_member_name=msg) ) return value
def to_python(self, value, state): perms_update = OrderedSet() perms_new = OrderedSet() # build a list of permission to update and new permission to create #CLEAN OUT ORG VALUE FROM NEW MEMBERS, and group them using new_perms_group = defaultdict(dict) for k, v in value.copy().iteritems(): if k.startswith('perm_new_member'): del value[k] _type, part = k.split('perm_new_member_') args = part.split('_') if len(args) == 1: new_perms_group[args[0]]['perm'] = v elif len(args) == 2: _key, pos = args new_perms_group[pos][_key] = v # fill new permissions in order of how they were added for k in sorted(map(int, new_perms_group.keys())): perm_dict = new_perms_group[str(k)] new_member = perm_dict.get('name') new_perm = perm_dict.get('perm') new_type = perm_dict.get('type') if new_member and new_perm and new_type: perms_new.add((new_member, new_perm, new_type)) for k, v in value.iteritems(): if k.startswith('u_perm_') or k.startswith('g_perm_'): member = k[7:] t = {'u': 'user', 'g': 'users_group'}[k[0]] if member == User.DEFAULT_USER: if str2bool(value.get('repo_private')): # set none for default when updating to # private repo protects against form manipulation v = EMPTY_PERM perms_update.add((member, v, t)) value['perms_updates'] = list(perms_update) value['perms_new'] = list(perms_new) # update permissions for k, v, t in perms_new: try: if t is 'user': self.user_db = User.query() \ .filter(User.active == True) \ .filter(User.username == k).one() if t is 'users_group': self.user_db = UserGroup.query() \ .filter(UserGroup.users_group_active == True) \ .filter(UserGroup.users_group_name == k).one() except Exception: log.exception('Updated permission failed') msg = self.message('perm_new_member_type', state) raise formencode.Invalid( msg, value, state, error_dict=dict(perm_new_member_name=msg)) return value
def teardown_method(self, method): # delete all groups for gr in UserGroup.query(): fixture.destroy_user_group(gr) Session().commit()
def teardown_method(self, method): # delete all groups for gr in UserGroup.query(): fixture.destroy_user_group(gr) Session().commit()
def users_and_groups_data(self): """ Returns 'results' with a list of users and user groups. You can either use the 'key' GET parameter to get a user by providing the exact user key or you can use the 'query' parameter to search for users by user key, first name and last name. 'types' defaults to just 'users' but can be set to 'users,groups' to get both users and groups. No more than 500 results (of each kind) will be returned. """ types = request.GET.get('types', 'users').split(',') key = request.GET.get('key', '') query = request.GET.get('query', '') results = [] if 'users' in types: user_list = [] if key: u = User.get_by_username(key) if u: user_list = [u] elif query: user_list = User.query() \ .filter(User.is_default_user == False) \ .filter(User.active == True) \ .filter(or_( User.username.ilike("%%" + query + "%%"), User.name.ilike("%%" + query + "%%"), User.lastname.ilike("%%" + query + "%%"), )) \ .order_by(User.username) \ .limit(500) \ .all() for u in user_list: results.append({ 'type': 'user', 'id': u.user_id, 'nname': u.username, 'fname': u.name, 'lname': u.lastname, 'gravatar_lnk': h.gravatar_url(u.email, size=28, default='default'), 'gravatar_size': 14, }) if 'groups' in types: grp_list = [] if key: grp = UserGroup.get_by_group_name(key) if grp: grp_list = [grp] elif query: grp_list = UserGroup.query() \ .filter(UserGroup.users_group_name.ilike("%%" + query + "%%")) \ .filter(UserGroup.users_group_active == True) \ .order_by(UserGroup.users_group_name) \ .limit(500) \ .all() for g in UserGroupList(grp_list, perm_level='read'): results.append({ 'type': 'group', 'id': g.users_group_id, 'grname': g.users_group_name, }) return dict(results=results)