防止用户在URL中键入其他用户时访问其他用户的数据 [英] Prevent users to access data of another user when typing the slug in the url

查看:138
本文介绍了防止用户在URL中键入其他用户时访问其他用户的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果用户1创建这张票:
mywebsite / manager / ticket / ticket-from-user-1 /



而用户2创建:
mywebsite / manager / ticket / ticket-from-user-2 /



如何防止用户1通过键入从用户2或其他用户访问票证它在url?


views.py




$ _ code>类TicketDisplay(LoginRequiredMixin,DetailView):
model = Ticket
template_name ='ticket_detail.html'
context_object_name ='ticket'
slug_field ='slug'

def get_context_data(self,** kwargs):
context = super(TicketDisplay,self).get_context_data(** kwargs)
context [' form_add_comment'] = CommentForm()
返回上下文




url .py




  url(r'^ manager / tickets /(?P< slug& -\w] +)/ $,views.TicketDetail.as_vie w(),name ='ticket_detail')


解决方案

最近在一个项目中实现了这个功能。可以通过使用自动生成的uuid来完成。 Django为此提供内置模型字段,或您可以使用slug字段并给它一个默认值。以下是一个简单的例子。



在您的models.py文件中,导入uuid库,然后将slug字段的默认值设置为uuid.uuid4。 p>

models.py:

  import uuid 

class Ticket(models.Model):
uuid = models.SlugField(default = uuid.uuid4,editable = False)
...

在urls.py中,只要使用uuid字段,就像是pk一样。这样做:

  url(r'^ manager / tickets /(?P< uuid> [0-9a-z- ] +)/?$',TicketDetail.as_view(),name ='ticket-detail'),

在您的详细信息,更新和删除视图中,您需要确保并设置这两个属性,以便Django知道要用作slug的字段:

  slug_field ='uuid'
slug_url_kwarg ='uuid'

然后在你的模板中,每当你需要检索kwargs的对象时,只需使用uuid而不是pk。



注意除此之外,您还应该通过权限来执行所有操作,以阻止用户看到其他页面。您可能可以阻止某些帐户查看其他人的详细信息。例如,你可以编写一个权限mixin来检查request.user是否与视图正在处理的对象匹配。



tldr 这是假设您与您的机票型号的用户有某种关系:

  class SameUserOnlyMixin(object):

def has_permissions(self):
#假设您的Ticket模型有一个称为user的外键。
return self.get_object()。user == self.request.user

def dispatch(self,request,* args,** kwargs):
if not self。 has_permissions():
raise Http404('你没有权限')
return super(SameUserOnlyMixin,self).dispatch(
request,* args,** kwargs)

最后,把它贴在你的看法上:

  class TicketDisplay(LoginRequiredMixin,SameUserOnlyMixin,DetailView):
...


If user 1 creat this ticket : mywebsite/manager/tickets/ticket-from-user-1/

And user 2 create that : mywebsite/manager/tickets/ticket-from-user-2/

How can I prevent user 1 to access the ticket from user 2 or other users by typing it in the url?

views.py

class TicketDisplay(LoginRequiredMixin, DetailView):
    model = Ticket
    template_name = 'ticket_detail.html'
    context_object_name = 'ticket'
    slug_field = 'slug'

    def get_context_data(self, **kwargs):
        context = super(TicketDisplay, self).get_context_data(**kwargs)
        context['form_add_comment'] = CommentForm()
        return context

url.py

url(r'^manager/tickets/(?P<slug>[-\w]+)/$',views.TicketDetail.as_view(), name='ticket_detail')

解决方案

I recently implemented this functionality in a project. It can be done by using automatically generated uuid's. Django has a built-in model field for this, or you can use a slug field and give it a default value. Here is a quick example.

In your models.py file, import the uuid library and then set the default value of your slug field to be uuid.uuid4.

models.py:

import uuid

class Ticket(models.Model):
    uuid = models.SlugField(default=uuid.uuid4, editable=False)
    ...

In urls.py, just use the uuid field as if it were a pk. Something like this:

url(r'^manager/tickets/(?P<uuid>[0-9a-z-]+)/?$', TicketDetail.as_view(), name='ticket-detail'),

In your detail, update, and delete views, you will need to make sure and set these two attributes so that Django knows which field to use as the slug:

slug_field = 'uuid'
slug_url_kwarg = 'uuid'

Then in your templates and whenever you need to retrieve an object for the kwargs, just use the uuid instead of the pk.

Note that in addition to this, you should also do all you can with permissions to block users from seeing other pages. You may be able to block certain accounts from viewing other peoples details. For instance, you could probably write a permissions mixin to check whether request.user matches up with the object that the view is handling.

tldr This is assuming that you have some kind of relation to a user on your Ticket model:

class SameUserOnlyMixin(object):

    def has_permissions(self):
        # Assumes that your Ticket model has a foreign key called user.
        return self.get_object().user == self.request.user

    def dispatch(self, request, *args, **kwargs):
        if not self.has_permissions():
            raise Http404('You do not have permission.')
        return super(SameUserOnlyMixin, self).dispatch(
            request, *args, **kwargs)

Finally, stick it on to your view like this:

class TicketDisplay(LoginRequiredMixin, SameUserOnlyMixin, DetailView):
    ...

这篇关于防止用户在URL中键入其他用户时访问其他用户的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆