def dispatch(self, request, name, subname, **kwargs): """ Adds the 'subname' logging argument, and passes that as extra keyword-argument to the parents dispatch method. """ name = stringprep(name) subname = stringprep(subname) largs = {"name": name, "subname": subname} return super(RestAuthSubResourceView, self).dispatch(request, largs=largs, name=name, subname=subname, **kwargs)
def put(self, request, largs): """Set groups of a user.""" if not request.user.has_perms(['Groups.group_remove_user', 'Groups.group_add_user']): return HttpResponseForbidden() # If BadRequest: 400 Bad Request groups, user = self._parse_put(request) # If PreconditionFailed: 412 Precondition Failed user = stringprep(user) groups = [stringprep(g) for g in groups] backend.set_memberships(user=user, service=request.user, groups=groups) return HttpResponseNoContent()
def put(self, request, largs, name): """Set users of a group.""" if not request.user.has_perms(['Groups.group_add_user', 'Groups.group_remove_user']): return HttpResponseForbidden() # If BadRequest: 400 Bad Request users = [stringprep(u) for u in self._parse_put(request)] # If GroupNotFound: 404 Not Found # If UserNotFound: 404 Not Found backend.set_members(group=name, service=request.user, users=users) self.log.info('Set users for group "%s"', name, extra=largs) return HttpResponseNoContent()
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 user = stringprep(self._parse_post(request)) # If GroupNotFound: 404 Not Found # If UserNotFound: 404 Not Found backend.add_member(group=name, service=request.user, user=user) self.log.info('Add user "%s"', user, extra=largs) return HttpResponseNoContent()
def put(self, request, largs, name): """Set the sub-groups of a group.""" if not request.user.has_perms(['Groups.group_add_group', 'Groups.group_remove_group']): return HttpResponseForbidden() if backend.SUPPORTS_SUBGROUPS is False: return HttpResponseNotImplemented() subgroups = [stringprep(g) for g in self._parse_put(request)] # If GroupNotFound: 404 Not Found backend.set_subgroups(group=name, service=request.user, subgroups=subgroups, subservice=request.user) self.log.info('Set sub-groups', 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 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 __call__(self, parser, namespace, value, option_string): if not six.PY3: # pragma: no branch, pragma: py2 value = value.decode('utf-8') user = stringprep(value) if namespace.create_user: try: user = stringcheck(user) except PreconditionFailed: raise ArgumentError(self, "Username contains invalid characters") try: validate_username(user) backend.create_user(user=user) except UserExists: raise ArgumentError(self, 'User already exists.') except PreconditionFailed as e: raise ArgumentError(self, e) setattr(namespace, self.dest, user)
def test_b1(self): teststr = ''.join(b1_table) self.assertEqual('', stringprep(teststr)) self.assertEqual('', stringcheck(teststr))
def test_bordercases(self): self.assertEqual('', stringprep(''))
def test_ok(self): self.assertEqual(username1, stringprep(username1)) self.assertEqual(username2, stringprep(username2)) self.assertEqual(username1, stringcheck(username1)) self.assertEqual(username2, stringcheck(username2))