我正在用django-tastypie构建一个IOS应用程序。用户的默认权限只是“获取”某些内容。但是,可以授予用户发布内容的“添加”权限。如何设计tastypie的api?如何使用django-tastypi向用户授予权限?
答案 0 :(得分:0)
首先,我想指出在使用tastypie之前认证和授权之间的细微差别:
现在,考虑到这一点,如果您还没有,请查看very conise examples here。
例如,您可以限制用户只能访问自己的用户信息:
# REST endpoint for authenticating user accounts
class UserResource(ModelResource):
class Meta:
queryset = User.objects.all()
resource_name = 'auth/user'
authentication = BasicAuthentication()
authorization = DjangoAuthorization()
def apply_authorization_limits(self, request, object_list):
return object_list.filter(username=request.user)
以下是您在与视图互动时如何对用户进行身份验证的摘录:
# first check that the user is allowed to post
username = form.cleaned_data['username']
password = form.cleaned_data['password'] # @todo: remove this for production
user = authenticate(username=username, password=password)
if user is None:
raise Http404('Error: new_frame: invalid username / password')
if not user.is_active:
raise Http404('Error: new_frame: your account is inactive')
# username / password is correct + user is active, proceed with posting