Permission::isEqual()   B
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 14
c 0
b 0
f 0
ccs 7
cts 7
cp 1
rs 8.8571
cc 5
eloc 7
nc 4
nop 1
crap 5
1
<?php
2
3
/*
4
 * This file is part of the Tinyissue package.
5
 *
6
 * (c) Mohamed Alsharaf <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Tinyissue\Model;
13
14
use Illuminate\Database\Eloquent\Model;
15
16
/**
17
 * Permission is model class for permissions.
18
 *
19
 * @author Mohamed Alsharaf <[email protected]>
20
 *
21
 * @property int    $id
22
 * @property string $permission
23
 * @property string $description
24
 * @property string $auto_has
25
 */
26
class Permission extends Model
27
{
28
    /**
29
     * Permission to view issue.
30
     *
31
     * @var string
32
     */
33
    const PERM_ISSUE_VIEW = 'issue-view';
34
35
    /**
36
     * Permission to create issue.
37
     *
38
     * @var string
39
     */
40
    const PERM_ISSUE_CREATE = 'issue-create';
41
42
    /**
43
     * Permission to add/edit/delete comment in an issue.
44
     *
45
     * @var string
46
     */
47
    const PERM_ISSUE_COMMENT = 'issue-comment';
48
49
    /**
50
     * Permission to modify issue.
51
     *
52
     * @var string
53
     */
54
    const PERM_ISSUE_MODIFY = 'issue-modify';
55
56
    /**
57
     * Permission to modify issue.
58
     *
59
     * @var string
60
     */
61
    const PERM_ISSUE_LOCK_QUOTE = 'issue-lock-quote';
62
63
    /**
64
     * Permission to view issue quote if locked.
65
     *
66
     * @var string
67
     */
68
    const PERM_ISSUE_VIEW_LOCKED_QUOTE = 'view-locked-quote';
69
70
    /**
71
     * Permission to modify all projects.
72
     *
73
     * @var string
74
     */
75
    const PERM_PROJECT_ALL = 'project-all';
76
77
    /**
78
     * Permission to create project.
79
     *
80
     * @var string
81
     */
82
    const PERM_PROJECT_CREATE = 'project-create';
83
84
    /**
85
     * Permission to modify project.
86
     *
87
     * @var string
88
     */
89
    const PERM_PROJECT_MODIFY = 'project-modify';
90
91
    /**
92
     * Permission as administrator.
93
     *
94
     * @var string
95
     */
96
    const PERM_ADMIN = 'administration';
97
98
    /**
99
     * Timestamp enabled.
100
     *
101
     * @var bool
102
     */
103
    public $timestamps = false;
104
105
    /**
106
     * Name of database table.
107
     *
108
     * @var string
109
     */
110
    protected $table = 'permissions';
111
112
    /**
113
     * List of group permissions.
114
     *
115
     * @var array
116
     */
117
    protected $groups = [
118
        self::PERM_PROJECT_ALL => [
119
            self::PERM_PROJECT_CREATE,
120
            self::PERM_PROJECT_MODIFY,
121
        ],
122
    ];
123
124
    /**
125
     * Compare if the permission is match.
126
     *
127
     * @param string $permission
128
     *
129
     * @return bool
130
     */
131 65
    public function isEqual($permission)
132
    {
133 65
        if ($permission === $this->permission) {
134 55
            return true;
135
        }
136
137 64
        foreach ($this->groups as $group => $permissions) {
138 64
            if (in_array($permission, $permissions) && $group === $this->permission) {
139 64
                return true;
140
            }
141
        }
142
143 64
        return false;
144
    }
145
}
146