Пример #1
0
class AdminModel(object):
    def __init__(self, db):
        self.db = db
        self.logger = logging.getLogger(__name__)
        self.groups = Groups(db)

    def log(self, message, *args):
        self.logger.debug(message, *args)

    def get_user_options(self):
        return sorted((user.link, user.user_id) for user in Users(self.db))

    def get_subgroup_options(self, group_id):
        return sorted((group.link, group.group_id)
                      for group in self.groups.find(**{'type': 'U'})
                      if group.group_id != group_id)

    def get_role_options(self, group_id):
        return sorted((group.link, group.group_id)
                      for group in self.groups.find(**{'type': 'U'})
                      if group.group_id != group_id)

    def get_app_options(self):
        groups_lookup = {
            group.name: group.group_id
            for group in zoom.system.site.groups.find(type='A')
        }
        return sorted(
            [(app.title, groups_lookup.get('a_' + app.name, 'a_' + app.name))
             for app in zoom.system.site.apps],
            key=repr)

    def update_group_apps(self, record):
        """Post updated group apps"""

        # create groups for uninstalled apps
        new_groups_required = set(
            app_key for app_key in record['apps']
            if isinstance(app_key, str) and app_key.startswith('a_'))
        for group_name in new_groups_required:
            app_name = group_name[2:]
            zoom.system.site.groups.add_app(app_name)

        groups_lookup = {
            group.name: group.group_id
            for group in zoom.system.site.groups.find(type='A')
        }
        new_keys = set(
            groups_lookup.get(app_key, app_key) for app_key in record['apps'])

        group = record
        group.update_apps_by_id(new_keys)

    def update_group_relationships(self, record):
        group = record
        group.update_members_by_id(group['users'])
        group.update_subgroups_by_id(group['subgroups'])
        group.update_roles_by_id(group['roles'])
        self.update_group_apps(record)
Пример #2
0
def get_groups_collection(request):

    def user_group(group):
        return group.type == 'U' and not group.name.startswith('a_')

    def get_fields():
        return group_fields(request)

    db = request.site.db
    users = Groups(db)
    labels = 'Name', 'Description', 'Administrators'
    columns = 'link', 'description', 'administrators'
    return model.GroupsCollection(
        get_fields,
        model=Group,
        controller=GroupCollectionController,
        view=GroupCollectionView,
        store=users,
        item_name='group',
        url='/admin/groups',
        filter=user_group,
        columns=columns,
        labels=labels,
        key_name='id',
        search_engine=RawSearch
    )
Пример #3
0
class TestGroup(unittest.TestCase):
    """Test the Zoom Group and Groups models"""
    def setUp(self):
        self.db = setup_test()
        self.groups = Groups(self.db)

    def tearDown(self):
        self.db.close()

    def test_get_group(self):
        group = self.groups.get(1)
        self.assertEqual(group._id, 1)
        self.assertEqual(group.name, 'administrators')
        self.assertEqual(group.type, 'U')
        self.assertEqual(group.admin_group_id, 1)
        group = self.groups.get(3)
        self.assertEqual(group._id, 3)

    def test_get_group_users(self):
        group = self.groups.first(name='users')
        self.assertSetEqual(group.users, group.get_users())
        self.assertTrue(group.users)

    def test_group_record_store(self):
        group = self.groups.first(name='users')
        self.assertTrue(group['__store'])
        self.assertIsInstance(group['__store'], Groups)

        del group['__store']
        self.assertRaises(
            KeyError,
            lambda: group.apps,
        )
        self.assertRaises(
            KeyError,
            lambda: group.roles,
        )
        self.assertRaises(
            KeyError,
            lambda: group.subgroups,
        )
Пример #4
0
 def setUp(self):
     self.db = setup_test()
     self.users = Users(self.db)
     self.groups = Groups(self.db)
     zoom.system.request = zoom.utils.Bunch(
         app=zoom.utils.Bunch(name=__name__, ),
         session=zoom.utils.Bunch(),
     )
     zoom.system.site = zoom.utils.Bunch(
         url='nosite',
         db=self.db,
         groups=self.groups,
     )
     zoom.system.user = self.users.first(username='******')
Пример #5
0
def main(route, request):

    def user_group(group):
        return group.type == 'U' and not group.name.startswith('a_')

    db = request.site.db
    users = Groups(db)
    fields = group_fields(request)
    columns = 'link', 'description', 'administrators'
    return Collection(
        fields,
        model=Group,
        controller=GroupCollectionController,
        store=users,
        item_name='group',
        url='/admin/groups',
        filter=user_group,
        columns=columns,
    )(route, request)
Пример #6
0
 def __init__(self, db):
     self.db = db
     self.logger = logging.getLogger(__name__)
     self.groups = Groups(db)
Пример #7
0
class AdminModel(object):
    def __init__(self, db):
        self.db = db
        self.logger = logging.getLogger(__name__)
        self.groups = Groups(db)

    def log(self, message, *args):
        self.logger.debug(message, *args)

    def get_user_options(self):
        return sorted((user.link, user._id) for user in Users(self.db))

    def get_subgroup_options(self, group_id):
        return sorted((group.link, group._id)
                      for group in self.groups.find(**{'type': 'U'})
                      if group._id != group_id)

    def get_role_options(self, group_id):
        return sorted((group.link, group._id)
                      for group in self.groups.find(**{'type': 'U'})
                      if group._id != group_id)

    def get_app_options(self, group_id):
        groups_lookup = {
            g.name: g._id
            for g in zoom.system.site.groups.find(type='A')
        }
        return sorted(
            [(app.title, groups_lookup.get('a_' + app.name, 'a_' + app.name))
             for app in zoom.system.site.apps],
            key=repr)

    def update_group_users(self, record):
        """Post updated group users"""

        record_id = int(record['_id'])

        updated_users = set(int(id) for id in record['users'])
        self.log('updated members: %r', updated_users)

        cmd = 'select user_id from members where group_id=%s'
        existing_users = set(user_id for user_id, in self.db(cmd, record_id))
        self.log('existing members: %r', existing_users)

        if updated_users != existing_users:
            if existing_users - updated_users:
                self.log('deleting members: %r',
                         existing_users - updated_users)
                cmd = 'delete from members where group_id=%s and user_id in %s'
                self.db(cmd, record_id, existing_users - updated_users)
            if updated_users - existing_users:
                self.log('inserting members: %r',
                         updated_users - existing_users)
                cmd = 'insert into members (group_id, user_id) values (%s, %s)'
                values = updated_users - existing_users
                sequence = zip([record_id] * len(values), values)
                self.db.execute_many(cmd, sequence)
        else:
            self.log('users unchanged')

    def update_group_subgroups(self, record):
        """Post updated group subgroups"""

        record_id = int(record['_id'])

        updated_subgroups = set(int(id) for id in record['subgroups'])
        self.log('updated subgroups: %r', updated_subgroups)

        cmd = 'select subgroup_id from subgroups where group_id=%s'
        existing_subgroups = set(subgroup_id
                                 for subgroup_id, in self.db(cmd, record_id))
        self.log('existing subgroups: %r', existing_subgroups)

        if updated_subgroups != existing_subgroups:
            if existing_subgroups - updated_subgroups:
                self.log('deleting: %r',
                         existing_subgroups - updated_subgroups)
                cmd = 'delete from subgroups where group_id=%s and subgroup_id in %s'
                self.db(cmd, record_id, existing_subgroups - updated_subgroups)
            if updated_subgroups - existing_subgroups:
                self.log('inserting: %r',
                         updated_subgroups - existing_subgroups)
                cmd = 'insert into subgroups (group_id, subgroup_id) values (%s, %s)'
                values = updated_subgroups - existing_subgroups
                sequence = zip([record_id] * len(values), values)
                self.db.execute_many(cmd, sequence)
        else:
            self.log('subgroups unchanged')

    def update_group_roles(self, record):
        """Post updated group roles"""

        record_id = int(record['_id'])
        group = context.site.groups.get(record_id)
        assert group

        updated_roles = set(int(user) for user in record['roles'])
        self.log('updated roles: %r', updated_roles)

        existing_roles = group.roles
        self.log('existing roles: %r', existing_roles)

        if updated_roles != existing_roles:
            if existing_roles - updated_roles:
                self.log('deleting: %r', existing_roles - updated_roles)
                cmd = 'delete from subgroups where subgroup_id=%s and group_id in %s'
                self.db(cmd, record_id, existing_roles - updated_roles)
            if updated_roles - existing_roles:
                self.log('inserting: %r', updated_roles - existing_roles)
                cmd = 'insert into subgroups (subgroup_id, group_id) values (%s, %s)'
                values = updated_roles - existing_roles
                sequence = zip([record_id] * len(values), values)
                self.db.execute_many(cmd, sequence)
        else:
            self.log('roles unchanged')

    def update_group_apps(self, record):
        """Post updated group apps"""

        record_id = int(record['_id'])
        group = context.site.groups.get(record_id)
        assert group

        updated_apps = set(app_key if isinstance(app_key, str)
                           and app_key.startswith('a_') else int(app_key)
                           for app_key in record['apps'])

        existing_apps = group.apps

        if updated_apps != existing_apps:

            self.log('updated apps: %r', updated_apps)
            self.log('existing apps: %r', existing_apps)

            if existing_apps - updated_apps:
                self.log('deleting: %r', existing_apps - updated_apps)
                cmd = 'delete from subgroups where subgroup_id=%s and group_id in %s'
                self.db(cmd, record_id, existing_apps - updated_apps)

            if updated_apps - existing_apps:

                new_groups_required = [
                    g for g in updated_apps
                    if isinstance(g, str) and g.startswith('a_')
                ]
                for group_name in new_groups_required:
                    zoom.system.site.groups.put(
                        zoom.models.Group(
                            name=group_name,
                            type='A',
                            description='',
                        ))
                    self.log('added required group %s', group_name)

                groups_lookup = {
                    g.name: g._id
                    for g in zoom.system.site.groups.find(type='A')
                }
                updated_apps = set(
                    groups_lookup.get(g, g) for g in updated_apps)

                self.log('inserting: %r', updated_apps - existing_apps)
                cmd = 'insert into subgroups (subgroup_id, group_id) values (%s, %s)'
                values = updated_apps - existing_apps
                sequence = zip([record_id] * len(values), values)
                self.db.execute_many(cmd, sequence)
        else:
            self.log('apps unchanged')

    def update_group_relationships(self, record):
        self.update_group_users(record)
        self.update_group_subgroups(record)
        self.update_group_roles(record)
        self.update_group_apps(record)
Пример #8
0
def get_user_group_options(site):
    groups = Groups(site.db)
    user_groups = list(
        sorted(
            (group.link, group.key) for group in groups.find(**{'type': 'U'})))
    return user_groups
Пример #9
0
class AdminModel(object):

    def __init__(self, db):
        self.db = db
        self.logger = logging.getLogger(__name__)
        self.groups = Groups(db)

    def log(self, message, *args):
        self.logger.debug(message, *args)
        print(message % args)

    def get_user_options(self):
        return sorted(
            (user.link, user._id) for user in Users(self.db)
        )

    def get_subgroup_options(self, group_id):
        return sorted(
            (group.link, group._id)
            for group in self.groups.find(**{'type': 'U'})
            if group._id != group_id
        )

    def get_role_options(self, group_id):
        return sorted(
            (group.link, group._id)
            for group in self.groups.find(**{'type': 'U'})
            if group._id != group_id
        )

    def get_app_options(self, group_id):
        app_titles = {app.name: app.title for app in context.site.apps}
        return sorted([
            (app_titles.get(app.name[2:], app.name[2:]), app._id)
            for app in self.groups.find(type="A")
            if app.name[2:] in app_titles
        ])

    def update_group_users(self, record):
        """Post updated group users"""

        record_id = int(record['_id'])

        updated_users = set(int(id) for id in record['users'])
        self.log('updated members: %r', updated_users)

        cmd = 'select user_id from members where group_id=%s'
        existing_users = set(
            user_id for user_id, in
            self.db(cmd, record_id)
        )
        self.log('existing members: %r', existing_users)

        if updated_users != existing_users:
            if existing_users - updated_users:
                self.log('deleting members: %r', existing_users - updated_users)
                cmd = 'delete from members where group_id=%s and user_id in %s'
                self.db(cmd, record_id, existing_users - updated_users)
            if updated_users - existing_users:
                self.log('inserting members: %r', updated_users - existing_users)
                cmd = 'insert into members (group_id, user_id) values (%s, %s)'
                values = updated_users - existing_users
                sequence = zip([record_id] * len(values), values)
                self.db.execute_many(cmd, sequence)
        else:
            self.log('users unchanged')

    def update_group_subgroups(self, record):
        """Post updated group subgroups"""

        record_id = int(record['_id'])

        updated_subgroups = set(int(id) for id in record['subgroups'])
        self.log('updated subgroups: %r', updated_subgroups)

        cmd = 'select subgroup_id from subgroups where group_id=%s'
        existing_subgroups = set(
            subgroup_id for subgroup_id, in
            self.db(cmd, record_id)
        )
        self.log('existing subgroups: %r', existing_subgroups)

        if updated_subgroups != existing_subgroups:
            if existing_subgroups - updated_subgroups:
                self.log('deleting: %r', existing_subgroups - updated_subgroups)
                cmd = 'delete from subgroups where group_id=%s and subgroup_id in %s'
                self.db(cmd, record_id, existing_subgroups - updated_subgroups)
            if updated_subgroups - existing_subgroups:
                self.log('inserting: %r', updated_subgroups - existing_subgroups)
                cmd = 'insert into subgroups (group_id, subgroup_id) values (%s, %s)'
                values = updated_subgroups - existing_subgroups
                sequence = zip([record_id] * len(values), values)
                self.db.execute_many(cmd, sequence)
        else:
            self.log('subgroups unchanged')

    def update_group_roles(self, record):
        """Post updated group roles"""

        record_id = int(record['_id'])
        group = context.site.groups.get(record_id)
        assert group

        updated_roles = set(int(user) for user in record['roles'])
        self.log('updated roles: %r', updated_roles)

        existing_roles = group.roles
        self.log('existing roles: %r', existing_roles)

        if updated_roles != existing_roles:
            if existing_roles - updated_roles:
                self.log('deleting: %r', existing_roles - updated_roles)
                cmd = 'delete from subgroups where subgroup_id=%s and group_id in %s'
                self.db(cmd, record_id, existing_roles - updated_roles)
            if updated_roles - existing_roles:
                self.log('inserting: %r', updated_roles - existing_roles)
                cmd = 'insert into subgroups (subgroup_id, group_id) values (%s, %s)'
                values = updated_roles - existing_roles
                sequence = zip([record_id] * len(values), values)
                self.db.execute_many(cmd, sequence)
        else:
            self.log('roles unchanged')

    def update_group_apps(self, record):
        """Post updated group apps"""

        record_id = int(record['_id'])
        group = context.site.groups.get(record_id)
        assert group

        updated_apps = set(int(user) for user in record['apps'])
        self.log('updated apps: %r', updated_apps)

        existing_apps = group.apps
        self.log('existing apps: %r', existing_apps)

        if updated_apps != existing_apps:
            if existing_apps - updated_apps:
                self.log('deleting: %r', existing_apps - updated_apps)
                cmd = 'delete from subgroups where subgroup_id=%s and group_id in %s'
                self.db(cmd, record_id, existing_apps - updated_apps)
            if updated_apps - existing_apps:
                self.log('inserting: %r', updated_apps - existing_apps)
                cmd = 'insert into subgroups (subgroup_id, group_id) values (%s, %s)'
                values = updated_apps - existing_apps
                sequence = zip([record_id] * len(values), values)
                self.db.execute_many(cmd, sequence)
        else:
            self.log('apps unchanged')

    def update_group_relationships(self, record):
        self.update_group_users(record)
        self.update_group_subgroups(record)
        self.update_group_roles(record)
        self.update_group_apps(record)
Пример #10
0
 def setUp(self):
     self.db = setup_test()
     self.groups = Groups(self.db)
Пример #11
0
 def setUp(self):
     self.db = setup_test()
     self.groups = Groups(self.db)
     zoom.system.site = zoom.sites.Site()
     zoom.system.user = zoom.system.site.users.get(1)
     zoom.system.request = Bunch(app=Bunch(name=__name__))
Пример #12
0
class TestGroup(unittest.TestCase):
    """Test the Zoom Group and Groups models"""
    def setUp(self):
        self.db = setup_test()
        self.groups = Groups(self.db)
        zoom.system.site = zoom.sites.Site()
        zoom.system.user = zoom.system.site.users.get(1)
        zoom.system.request = Bunch(app=Bunch(name=__name__))

    def tearDown(self):
        self.db.close()

    def test_get_group(self):
        group = self.groups.get(1)
        self.assertEqual(group._id, 1)
        self.assertEqual(group.name, 'administrators')
        self.assertEqual(group.type, 'U')
        self.assertEqual(group.admin_group_id, 1)
        group = self.groups.get(3)
        self.assertEqual(group._id, 3)

    def test_get_group_admin_group_ids(self):
        ids = self.groups.get_group_admin_group_ids()
        self.assertEqual(ids, {1})

    def test_is_group_admin_group(self):
        group = self.groups.get(1)
        self.assertTrue(group.is_group_admin_group)
        group = self.groups.get(2)
        self.assertFalse(group.is_group_admin_group)

    def test_get_group_users(self):
        group = self.groups.first(name='users')
        self.assertSetEqual(group.users, group.get_users())
        self.assertTrue(group.users)

    def test_group_record_store(self):
        group = self.groups.first(name='users')
        self.assertTrue(group['__store'])
        self.assertIsInstance(group['__store'], Groups)

        del group['__store']
        self.assertRaises(
            KeyError,
            lambda: group.apps,
        )
        self.assertRaises(
            KeyError,
            lambda: group.roles,
        )
        self.assertRaises(
            KeyError,
            lambda: group.subgroups,
        )

    def test_add_delete_group(self):
        groups = self.groups
        self.assertFalse(groups.first(name='testgroup1'))
        groups.add('testgroup1')
        self.assertTrue(groups.first(name='testgroup1'))
        groups.delete(name='testgroup1')
        self.assertFalse(groups.first(name='testgroup1'))

    def test_add_remove_subgroup(self):
        users_group = self.groups.first(name='users')
        managers_group = self.groups.first(name='managers')
        self.assertEqual(managers_group.subgroups, {1})

        managers_group.add_subgroup(users_group)
        self.assertEqual(managers_group.subgroups, {1, users_group.group_id})

        managers_group.remove_subgroup(users_group)
        self.assertEqual(managers_group.subgroups, {1})

    def test_locate_group(self):
        groups = self.groups
        group = groups.first(name='users')
        group_id = group.group_id

        self.assertEqual(groups.locate(group).group_id, group_id)
        self.assertEqual(groups.locate(group_id).group_id, group_id)
        self.assertEqual(groups.locate(group.name).group_id, group_id)

    def test_groups_add_remove_app(self):
        groups = self.groups

        app_name = 'ping'

        self.assertNotIn('a_' + app_name, set(g.name for g in groups))

        groups.add_app(app_name)

        self.assertIn('a_' + app_name, set(g.name for g in groups))

        groups.remove_app(app_name)

        self.assertNotIn('a_' + app_name, set(g.name for g in groups))

    def test_groups_add_remove_app_idempotentcy(self):
        groups = self.groups

        app_name = 'ping'

        self.assertNotIn('a_' + app_name, set(g.name for g in groups))

        groups.add_app(app_name)
        groups.add_app(app_name)

        self.assertIn('a_' + app_name, set(g.name for g in groups))

        groups.remove_app(app_name)
        groups.remove_app(app_name)

        self.assertNotIn('a_' + app_name, set(g.name for g in groups))

    def test_add_apps_remove_apps(self):

        groups = self.groups
        group_name = 'my_new_group'
        app_names = {'ping', 'hello'}

        if not groups.first(name=group_name):
            groups.add(group_name)
        group = groups.first(name=group_name)

        self.assertEqual(group.app_names, set())

        group.add_apps(app_names)

        self.assertEqual(group.app_names, app_names)

        group.remove_apps(app_names)

        group = groups.first(name=group_name)
        self.assertEqual(group.app_names, set())

        groups.delete(name=group_name)