Exemple #1
0
    def post(self, request, largs, name):
        """Add a sub-group."""

        if not request.user.has_perm('Groups.group_add_group'):
            return HttpResponseForbidden()
        if backend.SUPPORTS_SUBGROUPS is False:
            return HttpResponseNotImplemented()

        # If BadRequest: 400 Bad Request
        subname = stringprep(self._parse_post(request))

        # If GroupNotFound: 404 Not Found
        backend.add_subgroup(group=name, service=request.user, subgroup=subname,
                             subservice=request.user)

        self.log.info('Add subgroup "%s"', subname, extra=largs)
        return HttpResponseNoContent()
def save_groups(groups, args, parser):
    if groups:
        print("Groups:")

    subgroups = {}
    for name, data in six.iteritems(groups):
        service = data.pop('service', None)
        if service:
            service = Service.objects.get(username=service)

        try:
            backend.create_group(group=name, service=service)
            print("* %s: created." % name)
        except GroupExists:
            if args.skip_existing_groups:
                print("* %s: Already exists, skipping." % name)
                continue
            else:
                print("* %s: Already exists, adding memberships." % name)

        for username in data.get('users', []):
            backend.add_member(group=name, service=service, user=username)

        if 'subgroups' in data:
            subgroups[(name, service)] = data.pop('subgroups')

    # add group-memberships *after* we created all groups to make sure
    # groups already exist.
    if backend.SUPPORTS_SUBGROUPS:
        for group, subgroups_data in six.iteritems(subgroups):
            for subgroup_data in subgroups_data:
                name = subgroup_data['name']
                service = subgroup_data.get('service')
                if service:
                    service = Service.objects.get(username=service)

                backend.add_subgroup(group=group[0], service=group[1], subgroup=name,
                                     subservice=service)
    else:
        print('Warning: Backend does not support subgroups, subgroups discarded.')
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))