|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of Jitamin. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright (C) Jitamin Team |
|
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 Jitamin\Model; |
|
13
|
|
|
|
|
14
|
|
|
use Jitamin\Foundation\Database\Model; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Class ColumnRestrictionModel. |
|
18
|
|
|
*/ |
|
19
|
|
|
class ColumnRestrictionModel extends Model |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* SQL table name. |
|
23
|
|
|
* |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
const TABLE = 'column_has_restrictions'; |
|
27
|
|
|
|
|
28
|
|
|
const RULE_ALLOW_TASK_CREATION = 'allow.task_creation'; |
|
29
|
|
|
const RULE_ALLOW_TASK_OPEN_CLOSE = 'allow.task_open_close'; |
|
30
|
|
|
const RULE_BLOCK_TASK_CREATION = 'block.task_creation'; |
|
31
|
|
|
const RULE_BLOCK_TASK_OPEN_CLOSE = 'block.task_open_close'; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Get rules. |
|
35
|
|
|
* |
|
36
|
|
|
* @return array |
|
37
|
|
|
*/ |
|
38
|
|
View Code Duplication |
public function getRules() |
|
|
|
|
|
|
39
|
|
|
{ |
|
40
|
|
|
return [ |
|
41
|
|
|
self::RULE_ALLOW_TASK_CREATION => t('Task creation is permitted for this column'), |
|
42
|
|
|
self::RULE_ALLOW_TASK_OPEN_CLOSE => t('Closing or opening a task is permitted for this column'), |
|
43
|
|
|
self::RULE_BLOCK_TASK_CREATION => t('Task creation is blocked for this column'), |
|
44
|
|
|
self::RULE_BLOCK_TASK_OPEN_CLOSE => t('Closing or opening a task is blocked for this column'), |
|
45
|
|
|
]; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Fetch one restriction. |
|
50
|
|
|
* |
|
51
|
|
|
* @param int $project_id |
|
52
|
|
|
* @param int $restriction_id |
|
53
|
|
|
* |
|
54
|
|
|
* @return array|null |
|
55
|
|
|
*/ |
|
56
|
|
|
public function getById($project_id, $restriction_id) |
|
57
|
|
|
{ |
|
58
|
|
|
return $this->db |
|
|
|
|
|
|
59
|
|
|
->table(self::TABLE) |
|
60
|
|
|
->columns( |
|
61
|
|
|
self::TABLE.'.restriction_id', |
|
62
|
|
|
self::TABLE.'.project_id', |
|
63
|
|
|
self::TABLE.'.role_id', |
|
64
|
|
|
self::TABLE.'.column_id', |
|
65
|
|
|
self::TABLE.'.rule', |
|
66
|
|
|
'pr.role', |
|
67
|
|
|
'c.title as column_title' |
|
68
|
|
|
) |
|
69
|
|
|
->left(ColumnModel::TABLE, 'c', 'id', self::TABLE, 'column_id') |
|
70
|
|
|
->left(ProjectRoleModel::TABLE, 'pr', 'role_id', self::TABLE, 'role_id') |
|
71
|
|
|
->eq(self::TABLE.'.project_id', $project_id) |
|
72
|
|
|
->eq(self::TABLE.'.restriction_id', $restriction_id) |
|
73
|
|
|
->findOne(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Get all project column restrictions. |
|
78
|
|
|
* |
|
79
|
|
|
* @param int $project_id |
|
80
|
|
|
* |
|
81
|
|
|
* @return array |
|
82
|
|
|
*/ |
|
83
|
|
|
public function getAll($project_id) |
|
84
|
|
|
{ |
|
85
|
|
|
$rules = $this->getRules(); |
|
86
|
|
|
$restrictions = $this->db |
|
|
|
|
|
|
87
|
|
|
->table(self::TABLE) |
|
88
|
|
|
->columns( |
|
89
|
|
|
self::TABLE.'.restriction_id', |
|
90
|
|
|
self::TABLE.'.project_id', |
|
91
|
|
|
self::TABLE.'.role_id', |
|
92
|
|
|
self::TABLE.'.column_id', |
|
93
|
|
|
self::TABLE.'.rule', |
|
94
|
|
|
'pr.role', |
|
95
|
|
|
'c.title as column_title' |
|
96
|
|
|
) |
|
97
|
|
|
->left(ColumnModel::TABLE, 'c', 'id', self::TABLE, 'column_id') |
|
98
|
|
|
->left(ProjectRoleModel::TABLE, 'pr', 'role_id', self::TABLE, 'role_id') |
|
99
|
|
|
->eq(self::TABLE.'.project_id', $project_id) |
|
100
|
|
|
->findAll(); |
|
101
|
|
|
|
|
102
|
|
|
foreach ($restrictions as &$restriction) { |
|
103
|
|
|
$restriction['title'] = $rules[$restriction['rule']]; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
return $restrictions; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Get restrictions. |
|
111
|
|
|
* |
|
112
|
|
|
* @param int $project_id |
|
113
|
|
|
* @param string $role |
|
114
|
|
|
* |
|
115
|
|
|
* @return array |
|
116
|
|
|
*/ |
|
117
|
|
View Code Duplication |
public function getAllByRole($project_id, $role) |
|
|
|
|
|
|
118
|
|
|
{ |
|
119
|
|
|
return $this->db |
|
|
|
|
|
|
120
|
|
|
->table(self::TABLE) |
|
121
|
|
|
->columns( |
|
122
|
|
|
self::TABLE.'.restriction_id', |
|
123
|
|
|
self::TABLE.'.project_id', |
|
124
|
|
|
self::TABLE.'.role_id', |
|
125
|
|
|
self::TABLE.'.column_id', |
|
126
|
|
|
self::TABLE.'.rule', |
|
127
|
|
|
'pr.role' |
|
128
|
|
|
) |
|
129
|
|
|
->eq(self::TABLE.'.project_id', $project_id) |
|
130
|
|
|
->eq('pr.role', $role) |
|
131
|
|
|
->left(ProjectRoleModel::TABLE, 'pr', 'role_id', self::TABLE, 'role_id') |
|
132
|
|
|
->findAll(); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Create a new column restriction. |
|
137
|
|
|
* |
|
138
|
|
|
* @param int $project_id |
|
139
|
|
|
* @param int $role_id |
|
140
|
|
|
* @param int $column_id |
|
141
|
|
|
* @param int $rule |
|
142
|
|
|
* |
|
143
|
|
|
* @return bool|int |
|
144
|
|
|
*/ |
|
145
|
|
View Code Duplication |
public function create($project_id, $role_id, $column_id, $rule) |
|
|
|
|
|
|
146
|
|
|
{ |
|
147
|
|
|
return $this->db |
|
|
|
|
|
|
148
|
|
|
->table(self::TABLE) |
|
149
|
|
|
->persist([ |
|
150
|
|
|
'project_id' => $project_id, |
|
151
|
|
|
'role_id' => $role_id, |
|
152
|
|
|
'column_id' => $column_id, |
|
153
|
|
|
'rule' => $rule, |
|
154
|
|
|
]); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Remove a permission. |
|
159
|
|
|
* |
|
160
|
|
|
* @param int $restriction_id |
|
161
|
|
|
* |
|
162
|
|
|
* @return bool |
|
163
|
|
|
*/ |
|
164
|
|
|
public function remove($restriction_id) |
|
165
|
|
|
{ |
|
166
|
|
|
return $this->db->table(self::TABLE)->eq('restriction_id', $restriction_id)->remove(); |
|
|
|
|
|
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.