【发布时间】:2017-07-11 04:11:49
【问题描述】:
我希望对 django 网络应用程序进行更改,该应用程序通过美味派提供 REST 接口。该应用程序位于:
https://github.com/OWASP/django-DefectDojo/
在应用内,用户拥有他们有权查看的产品和属于产品的端点 .
模型定义在:
https://github.com/OWASP/django-DefectDojo/blob/master/dojo/models.py#L177
https://github.com/OWASP/django-DefectDojo/blob/master/dojo/models.py#L417
我已将 EndpointResource 添加到dojo/api.py:
class EndpointResource(BaseModelResource):
class Meta:
queryset = Endpoint.objects.all()
resource_name = 'endpoints'
fields = ['product', 'protocol', 'host', 'path', 'query', 'fragment']
list_allowed_methods = ['get']
detail_allowed_methods = ['get']
include_resource_uri = True
filtering = {
'product': ALL,
'protocol': ALL,
'host': ALL,
}
authorization = DjangoAuthorization()
authentication = DojoApiKeyAuthentication()
serializer = Serializer(formats=['json'])
类产品包含:authorized_users = models.ManyToManyField(User, blank=True)
类端点包含:product = models.ForeignKey(Product, null=True, blank=True, )
目前,用户可以通过/api/v1/endpoints/ 进行身份验证,他们将看到所有端点。
curl -v --header 'Authorization: ApiKey sue:5b632d76ef1a38b8375383e3498d063515b356d4' http://example.com/api/v1/endpoints/
但是,期望的行为是用户应该只能访问他们获得授权的产品,以及这些产品的相关实体。
在 python 会话中我可以做到:
>>> from dojo.models import User, Product, Endpoint
>>> User.objects.get(username='sue').product_set.all().get().endpoint_set.all()
[<Endpoint: goliath.sue.local>, <Endpoint: goliath.suelimited.co>, <Endpoint: 192.168.10.11>]
这些与“sue”关联的对象是我希望 API 返回的对象。
用美味的派来解决这个问题的最佳方法是什么?
非常感谢任何帮助,如果我需要发布更多信息,请告诉我。
【问题讨论】:
标签: python django rest tastypie