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.
Completed
Push — master ( 194382...d1a5fb )
by Lambda
01:38
created

GroupInPermissionLogic.has_perm()   F

Complexity

Conditions 19

Size

Total Lines 65

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 19
dl 0
loc 65
rs 2.8123

How to fix   Long Method    Complexity   

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:

Complexity

Complex classes like src.permission.logics.GroupInPermissionLogic.has_perm() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
# coding=utf-8
2
"""
3
Permission logic module for group based permission system
4
"""
5
from permission.conf import settings
6
from permission.logics.base import PermissionLogic
7
8
9
class GroupInPermissionLogic(PermissionLogic):
10
    """
11
    Permission logic class for group based permission system
12
    """
13
    def __init__(self,
14
                 group_names,
15
                 any_permission=None,
16
                 add_permission=None,
17
                 change_permission=None,
18
                 delete_permission=None):   # noqa
19
        """
20
        Constructor
21
22
        Parameters
23
        ----------
24
        group_names : string or list
25
            A group name list of this permission logic treat
26
        any_permission : boolean
27
            True for give any permission to the user_obj
28
            Default value will be taken from
29
            ``PERMISSION_DEFAULT_GIPL_ANY_PERMISSION`` in
30
            settings.
31
        add_permission : boolean
32
            True for give add permission to the user_obj.
33
            It will be ignored if :attr:`any_permission` is True.
34
            Default value will be taken from
35
            ``PERMISSION_DEFAULT_GIPL_ADD_PERMISSION`` in
36
            settings.
37
        change_permission : boolean
38
            True for give change permission of the specified object to the
39
            user_obj.
40
            It will be ignored if :attr:`any_permission` is True.
41
            Default value will be taken from
42
            ``PERMISSION_DEFAULT_GIPL_CHANGE_PERMISSION`` in
43
            settings.
44
        delete_permission : boolean
45
            True for give delete permission of the specified object to the
46
            user_obj.
47
            It will be ignored if :attr:`any_permission` is True.
48
            Default value will be taken from
49
            ``PERMISSION_DEFAULT_GIPL_DELETE_PERMISSION`` in
50
            settings.
51
        """
52
        self.group_names = group_names
53
        if not isinstance(self.group_names, (list, tuple)):
54
            self.group_names = [self.group_names]
55
        self.any_permission = any_permission
56
        self.add_permission = add_permission
57
        self.change_permission = change_permission
58
        self.delete_permission = delete_permission
59
60
        if self.any_permission is None:
61
            self.any_permission = \
62
                settings.PERMISSION_DEFAULT_GIPL_ANY_PERMISSION
63
        if self.add_permission is None:
64
            self.add_permission = \
65
                settings.PERMISSION_DEFAULT_GIPL_ADD_PERMISSION
66
        if self.change_permission is None:
67
            self.change_permission = \
68
                settings.PERMISSION_DEFAULT_GIPL_CHANGE_PERMISSION
69
        if self.delete_permission is None:
70
            self.delete_permission = \
71
                settings.PERMISSION_DEFAULT_GIPL_DELETE_PERMISSION
72
73 View Code Duplication
    def has_perm(self, user_obj, perm, obj=None):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
74
        """
75
        Check if user have permission (of object)
76
77
        If the user_obj is not authenticated, it return ``False``.
78
79
        If no object is specified, it return ``True`` when the corresponding
80
        permission was specified to ``True`` (changed from v0.7.0).
81
        This behavior is based on the django system.
82
        https://code.djangoproject.com/wiki/RowLevelPermissions
83
84
        If an object is specified, it will return ``True`` if the user is
85
        in group specified in ``group_names`` of this instance.
86
        This permission logic is used mainly for group based role permission
87
        system.
88
        You can change this behavior to set ``any_permission``,
89
        ``add_permission``, ``change_permission``, or ``delete_permission``
90
        attributes of this instance.
91
92
        Parameters
93
        ----------
94
        user_obj : django user model instance
95
            A django user model instance which be checked
96
        perm : string
97
            `app_label.codename` formatted permission string
98
        obj : None or django model instance
99
            None or django model instance for object permission
100
101
        Returns
102
        -------
103
        boolean
104
            Whether the specified user have specified permission (of specified
105
            object).
106
        """
107
        if not user_obj.is_authenticated():
108
            return False
109
        # construct the permission full name
110
        add_permission = self.get_full_permission_string('add')
111
        change_permission = self.get_full_permission_string('change')
112
        delete_permission = self.get_full_permission_string('delete')
113
        if obj is None:
114
            if user_obj.groups.filter(name__in=self.group_names):
115
                if self.add_permission and perm == add_permission:
116
                    return True
117
                if self.change_permission and perm == change_permission:
118
                    return True
119
                if self.delete_permission and perm == delete_permission:
120
                    return True
121
                return self.any_permission
122
            return False
123
        elif user_obj.is_active:
124
            if user_obj.groups.filter(name__in=self.group_names):
125
                if self.any_permission:
126
                    # have any kind of permissions to the obj
127
                    return True
128
                if (self.add_permission and
129
                        perm == add_permission):
130
                    return True
131
                if (self.change_permission and
132
                        perm == change_permission):
133
                    return True
134
                if (self.delete_permission and
135
                        perm == delete_permission):
136
                    return True
137
        return False
138