def delete(self, request, largs, name, subname): """ Remove a subgroup from a group. """ if not request.user.has_perm('Groups.group_remove_group'): return HttpResponseForbidden() # If GroupNotFound: 404 Not Found group = group_backend.get(service=request.user, name=name) subgroup = group_backend.get(service=request.user, name=subname) group_backend.rm_subgroup(group=group, subgroup=subgroup) self.log.info('Remove subgroup %s', subname, extra=largs) return HttpResponseNoContent()
def post(self, request, largs, name): """ Add a sub-group. """ if not request.user.has_perm('Groups.group_add_group'): return HttpResponseForbidden() # If BadRequest: 400 Bad Request subname = self._parse_post(request).lower() # If GroupNotFound: 404 Not Found group = group_backend.get(service=request.user, name=name) subgroup = group_backend.get(service=request.user, name=subname) group_backend.add_subgroup(group=group, subgroup=subgroup) self.log.info('Add subgroup "%s"', subname, extra=largs) return HttpResponseNoContent()
def delete(self, request, largs, name): """ Delete a group. """ if not request.user.has_perm('Groups.group_delete'): return HttpResponseForbidden() group = group_backend.get(service=request.user, name=name) group_backend.remove(group=group) self.log.info("Deleted group", extra=largs) return HttpResponseNoContent()
def get(self, request, largs, name): """ Get a list of sub-groups """ if not request.user.has_perm('Groups.group_groups_list'): return HttpResponseForbidden() # If GroupNotFound: 404 Not Found group = group_backend.get(service=request.user, name=name) groups = group_backend.subgroups(group=group) return HttpRestAuthResponse(request, groups)
def get(self, request, largs, name): """ Get all users in a group. """ if not request.user.has_perm('Groups.group_users'): return HttpResponseForbidden() # If GroupNotFound: 404 Not Found group = group_backend.get(service=request.user, name=name) users = group_backend.members(group=group) return HttpRestAuthResponse(request, users)
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: group = group_backend.create(service=service, name=name) 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) group = group_backend.get(service=service, name=name) for username in data.get('users', []): user = user_backend.get(username=username) group_backend.add_user(group=group, user=user) if 'subgroups' in data: subgroups[group] = data.pop('subgroups') # add group-memberships *after* we created all groups to make sure # groups already exist. 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) subgroup = group_backend.get(name=name, service=service) group_backend.add_subgroup(group=group, subgroup=subgroup)
def delete(self, request, largs, name, subname): """ Remove a user from a group. """ if not request.user.has_perm('Groups.group_remove_user'): return HttpResponseForbidden() # If GroupNotFound: 404 Not Found group = group_backend.get(service=request.user, name=name) # If UserNotFound: 404 Not Found user = user_backend.get(username=subname) group_backend.rm_user(group=group, user=user) self.log.info('Remove user from group', extra=largs) return HttpResponseNoContent()
def get(self, request, largs, name, subname): """ Verify that a user is in a group. """ if not request.user.has_perm('Groups.group_user_in_group'): return HttpResponseForbidden() # If GroupNotFound: 404 Not Found group = group_backend.get(service=request.user, name=name) # If UserNotFound: 404 Not Found user = user_backend.get(username=subname) if group_backend.is_member(group=group, user=user): return HttpResponseNoContent() else: raise UserNotFound(subname) # 404 Not Found
def post(self, request, largs, name): """ Add a user to a group. """ if not request.user.has_perm('Groups.group_add_user'): return HttpResponseForbidden() # If BadRequest: 400 Bad Request username = self._parse_post(request).lower() # If GroupNotFound: 404 Not Found group = group_backend.get(service=request.user, name=name) # If UserNotFound: 404 Not Found user = user_backend.get(username=username) group_backend.add_user(group=group, user=user) self.log.info('Add user "%s"', username, extra=largs) return HttpResponseNoContent()
def get_group(parser, name, service): try: return group_backend.get(name=name, service=service) except GroupNotFound: parser.error('%s at service %s: Group does not exist.' % (name, service))