Ejemplo n.º 1
0
    def get(self, request, largs):
        """Get a list of groups or a list of groups where the user is a member of."""

        username = request.GET.get('user', None)
        if username is None or username == '':
            if not request.user.has_perm('Groups.groups_list'):
                return HttpResponseForbidden()

            groups = backend.list_groups(service=request.user)
        else:
            if not request.user.has_perm('Groups.groups_for_user'):
                return HttpResponseForbidden()

            # Get all groups of a user
            groups = backend.list_groups(service=request.user, user=username)

        groups = [g.lower() for g in groups]
        return HttpRestAuthResponse(request, groups)
Ejemplo n.º 2
0
def _main(args):
    # Actions that do not act on an existing group:
    if args.action == 'add':
        try:
            backend.create_group(group=args.group, service=args.service)
        except GroupExists:
            parser.error('Group already exists.')
    elif args.action in ['list', 'ls']:
        groups = backend.list_groups(service=args.service)

        for name in sorted(groups):
            print(name)
    elif args.action == 'view':
        explicit_users = sorted(backend.members(group=args.group, service=args.service, depth=0))
        effective_users = sorted(backend.members(group=args.group, service=args.service))

        if explicit_users:
            print('* Explicit members: %s' % ', '.join(explicit_users))
        else:
            print('* No explicit members')
        if backend.SUPPORTS_SUBGROUPS:
            if effective_users:
                print('* Effective members: %s' % ', '.join(effective_users))
            else:
                print('* No effective members')

        if backend.SUPPORTS_SUBGROUPS is True:
            parents = backend.parents(group=args.group, service=args.service)
            sub_groups = backend.subgroups(group=args.group, service=args.service, filter=False)

            if parents:
                if backend.SUPPORTS_GROUP_VISIBILITY:
                    print('* Parent groups:')
                    parents = sorted(parents, key=lambda g: g[1].username if g[1] else '')
                    print_by_service(parents, '    ')
                else:
                    print('* Parent groups: %s' % ', '.join([g[0] for g in parents]))
            else:
                print('* No parent groups')

            if sub_groups:
                if backend.SUPPORTS_GROUP_VISIBILITY:
                    print('* Subgroups:')
                    sub_groups = sorted(sub_groups, key=lambda g: g[1].username if g[1] else '')
                    print_by_service(sub_groups, '    ')
                else:
                    print('* Subgroups: %s' % ', '.join([g[0] for g in sub_groups]))
            else:
                print('* No subgroups')
    elif args.action == 'set-service':
        if backend.SUPPORTS_GROUP_VISIBILITY is False:
            parser.error('Backend does not support group visiblity.')
        backend.set_service(group=args.group, service=args.service,
                                  new_service=args.new_service)
    elif args.action == 'add-user':
        backend.add_member(group=args.group, service=args.service, user=args.user)
    elif args.action == 'add-group':
        if backend.SUPPORTS_SUBGROUPS is False:
            parser.error('Backend does not support subgroups.')
        backend.add_subgroup(group=args.group, service=args.service, subgroup=args.subgroup,
                             subservice=args.sub_service)
    elif args.action in ['delete', 'del', 'rm']:
        backend.remove_group(group=args.group, service=args.service)
    elif args.action in ['remove-user', 'rm-user', 'del-user']:
        try:
            backend.remove_member(group=args.group, service=args.service, user=args.user)
        except UserNotFound:
            parser.error('User "%s" not member of group "%s".' % (args.user, args.group))
    elif args.action == 'rename':
        backend.rename_group(args.group, args.name, service=args.service)
    elif args.action in ['remove-group', 'rm-group', 'del-group']:  # pragma: no branch
        if backend.SUPPORTS_SUBGROUPS is False:
            parser.error('Backend does not support subgroups.')
        try:
            backend.remove_subgroup(group=args.group, service=args.service, subgroup=args.subgroup,
                                    subservice=args.sub_service)
        except GroupNotFound:
            parser.error('Group "%s" is not a subgroup of "%s".' % (args.subgroup, args.group))
Ejemplo n.º 3
0
 def test_dry_run_create_invalid_group(self):
     resp = self.post('/test/groups/', {'group': 'foo\nbar'})
     self.assertEqual(resp.status_code, http_client.PRECONDITION_FAILED)
     self.assertFalse(backend.list_groups(service=self.service))
Ejemplo n.º 4
0
    def test_dry_run_create_existing_group(self):
        backend.create_group(service=self.service, group=groupname1)

        resp = self.post('/test/groups/', {'group': groupname1})
        self.assertEqual(resp.status_code, http_client.CONFLICT)
        self.assertCountEqual([groupname1], backend.list_groups(service=self.service))
Ejemplo n.º 5
0
 def test_dry_run_create_group(self):
     resp = self.post('/test/groups/', {'group': groupname1})
     self.assertEqual(resp.status_code, http_client.CREATED)
     self.assertFalse(backend.list_groups(service=self.service))
Ejemplo n.º 6
0
def main(args=None):
    # parse arguments
    args = parser.parse_args(args=args)

    if args.action == 'add':
        password = args.get_password(args)
        if args.password_generated:
            print(args.pwd)

        backend.set_password(user=args.user, password=password)
    elif args.action in ['ls', 'list']:
        for username in sorted(backend.list_users()):
            if six.PY3:  # pragma: py3
                print(username)
            else:   # pragma: py2
                print(username.encode('utf-8'))
    elif args.action == 'verify':
        if not args.pwd:  # pragma: no cover
            args.pwd = getpass.getpass('password: '******'Ok.')
        else:
            print('Failed.')
            sys.exit(1)
    elif args.action == 'set-password':
        password = args.get_password(args)
        if args.password_generated:
            print(args.pwd)

        backend.set_password(user=args.user, password=args.pwd)
    elif args.action == 'view':
        props = backend.get_properties(user=args.user)

        if 'date joined' in props:
            print('Joined: %s' % props['date joined'])

        if 'last login' in props:
            print('Last login: %s' % props['last login'])

        if args.service:
            groups = backend.list_groups(service=args.service, user=args.user)
            if groups:
                print('Groups: %s' % ', '.join(sorted(groups)))
            else:
                print('No groups.')
        else:
            if backend.SUPPORTS_GROUP_VISIBILITY:
                groups = {}
                none_groups = backend.list_groups(service=None, user=args.user)

                for service in Service.objects.all():
                    subgroups = backend.list_groups(service=service, user=args.user)
                    if subgroups:
                        groups[service.username] = subgroups

                if groups or none_groups:
                    print('Groups:')
                    if none_groups:
                        print('* no service: %s' % ', '.join(sorted(none_groups)))

                    for service, groups in sorted(groups.items(), key=lambda t: t[0]):
                        print('* %s: %s' % (service, ', '.join(sorted(groups))))
                else:
                    print('No groups.')
            else:
                groups = backend.list_groups(service=None, user=args.user)
                if groups:
                    print('Groups: %s' % ', '.join(sorted(groups)))
                else:
                    print('No groups.')
    elif args.action == 'rename':
        try:
            backend.rename_user(user=args.user, name=args.name)
        except UserNotFound as e:
            parser.error("%s: %s" % (args.name, e))
        except UserExists as e:
            parser.error("%s: %s" % (args.name, e))
    elif args.action in ['delete', 'rm', 'remove']:  # pragma: no branch
        backend.remove_user(user=args.user)