[Answered ]-Authentication User Django Depending on the User

1👍

something like (maybe reinventing the wheel)

class require_permission:
     def __init__(self,permission,redirect="index/denied_access"):
       self.perm = permission
       self.redirect = redirect

    @require_login
    def __call__(self,f):
        def fn(self,request,*args,**kwargs):
           if not request.user.has_perm(self.perm):
               redirect(self.redirect)
           return f(request,*args,**kwargs)
        return fn

 ...

 @require_permission(PERMISSION.CAN_VIEW_ADMIN,'index/capture_info')
 def admin_view(request,*args,**kw):
     return HttpResponse(...)

 @require_permission(PERMISSION.CAN_CAPTURE_INFO,'index/basic_user')
 def capture_view(request,*args,**kw):
      return HttpRespone(...)

 @require_login
 def basic_view(request,*args,**kw):
      return HttpResponse(...)
đŸ‘€Joran Beasley

1👍

What do I need to give permissions to each user?

What you’re going to want here is a Group for each type of user (https://docs.djangoproject.com/en/dev/topics/auth/default/#groups). E.G. ‘Superuser’, ‘Normal User’, and ‘Restricted User’. These Groups can be created in the Django Admin Console and then each user can be assigned to their respective groups using the same Django Admin Console (https://docs.djangoproject.com/en/dev/topics/auth/default/#managing-users-in-the-admin).

How can I redirect each panel to their respective user?

You will want to define a RedirectView that forwards to user’s request to the expected “panel” view based on the user’s assigned group (https://docs.djangoproject.com/en/dev/ref/class-based-views/base/#redirectview).

from django.core.urlresolvers import reverse
from django.views.generic.base import RedirectView

from articles.models import Article

class PanelRedirectView(RedirectView):

    def get_redirect_url(self, *args, **kwargs):
        user = self.request.user
        if user.groups.filter(name='superuser').count():
            return reverse('superuser-panel')
        elif user.groups.filter(name='regular_user').count():
            return reverse('regular-user-panel')
        else:
            return reverse('restricted-user-panel')

You can then use the annotations described by Joran to restricted access to the particular views being redirected to. The example I described uses restrictions based User Groups, whereas his answer uses restrictions based on User Permissions. This is more of a personal preference when dealing with a simple permission scheme like the one you are describing.

* More Details *

To redirect the user on login


  1. Create a named url for your redirect view in urls.py

    url(r’^panel-redirect/$’ , PanelRedirectView.as_view() , name=”panel-redirect”),

  2. change LOGIN_REDIRECT_URL in settings.py to point to this redirect view on login

    LOGIN_REDIRECT_URL = reverse(‘panel-redirect’)

When the person logs in they will be redirected to the RedirectView, which in turn will redirect the person to the correct panel based on his/her assigned group

đŸ‘€Jeff Miller

Leave a comment