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

StaffPermissionLogic   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 122
Duplicated Lines 51.64 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 63
loc 122
rs 10
wmc 24

2 Methods

Rating   Name   Duplication   Size   Complexity  
F has_perm() 63 63 19
B __init__() 0 54 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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