GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

PermissionBackend.has_perm()   C
last analyzed

Complexity

Conditions 8

Size

Total Lines 52

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 8
c 2
b 1
f 0
dl 0
loc 52
rs 5.5452

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
# coding=utf-8
2
"""
3
Logical permission backends module
4
"""
5
from permission.conf import settings
6
from permission.utils.handlers import registry
7
from permission.utils.permissions import perm_to_permission
8
9
10
__all__    = ('PermissionBackend',)
11
12
13
class PermissionBackend(object):
14
    """
15
    A handler based permission backend
16
    """
17
    supports_object_permissions = True
18
    supports_anonymous_user = True
19
    supports_inactive_user = True
20
21
    # pylint:disable=unused-argument
22
    def authenticate(self, username, password):
23
        """
24
        Always return ``None`` to prevent authentication within this backend.
25
        """
26
        return None
27
28
    def has_perm(self, user_obj, perm, obj=None):
29
        """
30
        Check if user have permission (of object) based on registered handlers.
31
32
        It will raise ``ObjectDoesNotExist`` exception when the specified
33
        string permission does not exist and
34
        ``PERMISSION_CHECK_PERMISSION_PRESENCE`` is ``True`` in ``settings``
35
        module.
36
37
        Parameters
38
        ----------
39
        user_obj : django user model instance
40
            A django user model instance which be checked
41
        perm : string
42
            `app_label.codename` formatted permission string
43
        obj : None or django model instance
44
            None or django model instance for object permission
45
46
        Returns
47
        -------
48
        boolean
49
            Whether the specified user have specified permission (of specified
50
            object).
51
52
        Raises
53
        ------
54
        django.core.exceptions.ObjectDoesNotExist
55
            If the specified string permission does not exist and
56
            ``PERMISSION_CHECK_PERMISSION_PRESENCE`` is ``True`` in ``settings``
57
            module.
58
        """
59
        if settings.PERMISSION_CHECK_PERMISSION_PRESENCE:
60
            # get permission instance from string permission (perm)
61
            # it raise ObjectDoesNotExists when the permission is not exists
62
            try:
63
                perm_to_permission(perm)
64
            except AttributeError:
65
                # Django 1.2 internally use wrong permission string thus ignore
66
                pass
67
68
        # get permission handlers fot this perm
69
        cache_name = '_%s_cache' % perm
70
        if hasattr(self, cache_name):
71
            handlers = getattr(self, cache_name)
72
        else:
73
            handlers = [h for h in registry.get_handlers()
74
                        if perm in h.get_supported_permissions()]
75
            setattr(self, cache_name, handlers)
76
        for handler in handlers:
77
            if handler.has_perm(user_obj, perm, obj=obj):
78
                return True
79
        return False
80
81
    def has_module_perms(self, user_obj, app_label):
82
        """
83
        Check if user have permission of specified app based on registered
84
        handlers.
85
86
        It will raise ``ObjectDoesNotExist`` exception when the specified
87
        string permission does not exist and
88
        ``PERMISSION_CHECK_PERMISSION_PRESENCE`` is ``True`` in ``settings``
89
        module.
90
91
        Parameters
92
        ----------
93
        user_obj : django user model instance
94
            A django user model instance which is checked
95
        app_label : string
96
            `app_label.codename` formatted permission string
97
98
        Returns
99
        -------
100
        boolean
101
            Whether the specified user have specified permission.
102
103
        Raises
104
        ------
105
        django.core.exceptions.ObjectDoesNotExist
106
            If the specified string permission does not exist and
107
            ``PERMISSION_CHECK_PERMISSION_PRESENCE`` is ``True`` in ``settings``
108
            module.
109
        """
110
        # get permission handlers fot this perm
111
        cache_name = '_%s_cache' % app_label
112
        if hasattr(self, cache_name):
113
            handlers = getattr(self, cache_name)
114
        else:
115
            handlers = [h for h in registry.get_handlers()
116
                        if app_label in h.get_supported_app_labels()]
117
            setattr(self, cache_name, handlers)
118
        for handler in handlers:
119
            if handler.has_module_perms(user_obj, app_label):
120
                return True
121
        return False
122