Django How PermissionRequireMixin class works in code level?(I am even looking into mixins.py in auth folder)
<p>I would like to know how PermissionRequireMixin works in Django. (I couldn't find any question explaining how PermissionRequireMixin works in very detail. I am looking into code 'mixins.py' in path of 'django.contrib.auth'.)</p>

<p>For example, if write codes like below, it will check if login-user has permission named 'test_permission'. if the login-user has this permission, it moves to a template which is written in template_name attribute. if not, it moves to login page(or another page depend on what you set up). so PermissionRequiredMixin check permission to move on in a class type view.</p>

<pre><code>from django.contrib.auth.mixins import PermissionRequiredMixin
from django.views.generic import ListView

class ViewTest(PermissionRequiredMixin, ListView):
    permission_required = ('pertest.test_permission',)
    model = Test
    template_name = 'pertest/pertest_check_list.html'
</code></pre>

<p>what I want to know is...</p>

<p>I looked into 'django.contrib.auth.mixin.py code to understand how PermissionRequiredMixin class works. I can see it is subclass of AccessMixin and understand what kind of methods it has. but I can't understand how it proceed to check login's permission. I would like to understand flow in code base. where should i check to understand process for checking permission fully?</p>

<p>I even want to know this.</p>

<p>Let's say a login-user 'A' has permission named 'test_permission' and the 'A' user is in a group which has permission named 'group_test_permission'.</p>

<pre><code>case A : permission_required = ('pertest.test_permission',)
case B : permission_required = ('pertest.group_test_permission',)
</code></pre>

<p>in case A, a user A can move on because it has 'test_permission'.
in case B, a user A can move on because it is in a group which has 'group_test_permission'.</p>

<p>does it check a login user has the permission in auth_user_user_permission table and then check in auth_user_group table and auth_group_permissions? or in opposite direction? I am confused because group, user, permission are in individual tables and are connected to each other by ForiegnKey(ManyToMany or ForiegnKey).</p>

<p>In a nutshell, I would like to know process of how PermissionRequiredMixin class checks permissions and which part(what codes) I should check to understand this process.</p>

<p>Thank you in advance. Let me know if my question is not clear.</p>

<p>In the end the authentication backends are responsible to grant or deny access.</p>

<p>As you might have noticed, the <code>PermissionRequiredMixin</code> in the end calls <code>has_perm</code> on the user object with the defined permissions. This method just wraps an internal method called <a href="https://github.com/django/django/blob/master/django/contrib/auth/models.py#L180" rel="nofollow">_user_has_perm</a> in auth models.py.</p>

<p>Finally you'll might want to read the <a href="https://github.com/django/django/blob/master/django/contrib/auth/backends.py" rel="nofollow">backends.py</a> shipped by <code>django.contrib.auth</code> to see how access is granted by default.</p>

<p>As far as i can see, by default it just checks if you have either a grant through your user, or through your group and let you in or not.</p>

<p>HTH</p>

python django