|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @package CleverStyle Framework |
|
4
|
|
|
* @author Nazar Mokrynskyi <[email protected]> |
|
5
|
|
|
* @copyright Copyright (c) 2013-2016, Nazar Mokrynskyi |
|
6
|
|
|
* @license MIT License, see license.txt |
|
7
|
|
|
*/ |
|
8
|
|
|
namespace cs; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class for permissions manipulating |
|
12
|
|
|
* |
|
13
|
|
|
* @method static $this instance($check = false) |
|
14
|
|
|
*/ |
|
15
|
|
|
class Permission { |
|
16
|
|
|
use |
|
17
|
|
|
CRUD_helpers, |
|
18
|
|
|
Singleton; |
|
19
|
|
|
|
|
20
|
|
|
protected $data_model = [ |
|
21
|
|
|
'id' => 'int:1', |
|
22
|
|
|
'group' => 'text', |
|
23
|
|
|
'label' => 'text' |
|
24
|
|
|
]; |
|
25
|
|
|
protected $table = '[prefix]permissions'; |
|
26
|
|
|
/** |
|
27
|
|
|
* Array of all permissions for quick selecting |
|
28
|
|
|
* |
|
29
|
|
|
* @var array|null |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $permissions_table; |
|
32
|
|
|
/** |
|
33
|
|
|
* @var Cache\Prefix |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $cache; |
|
36
|
|
|
/** |
|
37
|
|
|
* Returns database index |
|
38
|
|
|
* |
|
39
|
|
|
* @return int |
|
40
|
|
|
*/ |
|
41
|
6 |
|
protected function cdb () { |
|
42
|
6 |
|
return Config::instance()->module('System')->db('users'); |
|
43
|
|
|
} |
|
44
|
10 |
|
protected function construct () { |
|
45
|
10 |
|
$this->cache = Cache::prefix('permissions'); |
|
46
|
10 |
|
} |
|
47
|
|
|
/** |
|
48
|
|
|
* Get permission data<br> |
|
49
|
|
|
* If <b>$group</b> or/and <b>$label</b> parameter is specified, <b>$id</b> is ignored. |
|
50
|
|
|
* |
|
51
|
|
|
* @param int|null $id |
|
52
|
|
|
* @param null|string $group |
|
53
|
|
|
* @param null|string $label |
|
54
|
|
|
* |
|
55
|
|
|
* @return array|false If only <b>$id</b> specified - result is array of permission data, in other cases result will be array of arrays of corresponding |
|
56
|
|
|
* permissions data |
|
57
|
|
|
*/ |
|
58
|
4 |
|
function get ($id = null, $group = null, $label = null) { |
|
59
|
4 |
|
if ($group !== null || $label !== null) { |
|
60
|
4 |
|
return $this->read( |
|
61
|
4 |
|
$this->search( |
|
62
|
|
|
[ |
|
63
|
4 |
|
'group' => $group, |
|
64
|
4 |
|
'label' => $label |
|
65
|
|
|
], |
|
66
|
4 |
|
1, |
|
67
|
4 |
|
PHP_INT_MAX, |
|
68
|
4 |
|
'id', |
|
69
|
4 |
|
true |
|
70
|
4 |
|
) ?: [] |
|
71
|
|
|
); |
|
72
|
|
|
} else { |
|
73
|
2 |
|
return $this->read($id); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
/** |
|
77
|
|
|
* Add permission |
|
78
|
|
|
* |
|
79
|
|
|
* @param string $group |
|
80
|
|
|
* @param string $label |
|
81
|
|
|
* |
|
82
|
|
|
* @return false|int Group id or <b>false</b> on failure |
|
83
|
|
|
*/ |
|
84
|
4 |
|
function add ($group, $label) { |
|
85
|
4 |
|
$id = $this->create($group, $label); |
|
86
|
4 |
|
if ($id) { |
|
87
|
4 |
|
$this->del_all_cache(); |
|
88
|
|
|
} |
|
89
|
4 |
|
return $id; |
|
90
|
|
|
} |
|
91
|
|
|
/** |
|
92
|
|
|
* Set permission |
|
93
|
|
|
* |
|
94
|
|
|
* @param int $id |
|
95
|
|
|
* @param string $group |
|
96
|
|
|
* @param string $label |
|
97
|
|
|
* |
|
98
|
|
|
* @return bool |
|
99
|
|
|
*/ |
|
100
|
2 |
|
function set ($id, $group, $label) { |
|
101
|
2 |
|
$result = $this->update($id, $group, $label); |
|
102
|
2 |
|
if ($result) { |
|
103
|
2 |
|
$this->del_all_cache(); |
|
104
|
|
|
} |
|
105
|
2 |
|
return $result; |
|
106
|
|
|
} |
|
107
|
|
|
/** |
|
108
|
|
|
* Deletion of permission or array of permissions |
|
109
|
|
|
* |
|
110
|
|
|
* @param int|int[] $id |
|
111
|
|
|
* |
|
112
|
|
|
* @return bool |
|
113
|
|
|
*/ |
|
114
|
2 |
|
function del ($id) { |
|
115
|
2 |
|
$id = implode(',', (array)_int($id)); |
|
116
|
2 |
|
$result = $this->db_prime()->q( |
|
117
|
|
|
[ |
|
118
|
|
|
"DELETE FROM `[prefix]permissions` |
|
119
|
2 |
|
WHERE `id` IN ($id)", |
|
120
|
|
|
"DELETE FROM `[prefix]users_permissions` |
|
121
|
2 |
|
WHERE `permission` IN ($id)", |
|
122
|
|
|
"DELETE FROM `[prefix]groups_permissions` |
|
123
|
2 |
|
WHERE `permission` IN ($id)" |
|
124
|
|
|
] |
|
125
|
|
|
); |
|
126
|
2 |
|
if ($result) { |
|
127
|
2 |
|
$Cache = $this->cache; |
|
128
|
|
|
unset( |
|
129
|
2 |
|
$Cache->users, |
|
130
|
2 |
|
$Cache->groups |
|
131
|
|
|
); |
|
132
|
2 |
|
$this->del_all_cache(); |
|
133
|
|
|
} |
|
134
|
2 |
|
return (bool)$result; |
|
135
|
|
|
} |
|
136
|
|
|
/** |
|
137
|
|
|
* Returns array of all permissions grouped by permissions groups |
|
138
|
|
|
* |
|
139
|
|
|
* @return array Format of array: ['group']['label'] = <i>permission_id</i> |
|
140
|
|
|
*/ |
|
141
|
10 |
|
function get_all () { |
|
142
|
10 |
|
if ($this->permissions_table === null) { |
|
143
|
10 |
|
$this->permissions_table = $this->cache->get( |
|
144
|
10 |
|
'all', |
|
145
|
10 |
|
function () { |
|
146
|
4 |
|
$data = $this->read( |
|
147
|
4 |
|
$this->search([], 1, PHP_INT_MAX, 'id', true) ?: [] |
|
148
|
|
|
); |
|
149
|
4 |
|
$all_permissions = []; |
|
150
|
4 |
|
foreach ($data as $item) { |
|
151
|
4 |
|
$all_permissions[$item['group']][$item['label']] = $item['id']; |
|
152
|
|
|
} |
|
153
|
4 |
|
return $all_permissions; |
|
154
|
10 |
|
} |
|
155
|
|
|
); |
|
156
|
|
|
} |
|
157
|
10 |
|
return $this->permissions_table; |
|
158
|
|
|
} |
|
159
|
|
|
/** |
|
160
|
|
|
* Deletion of permission table (is used after adding, setting or deletion of permission) |
|
161
|
|
|
*/ |
|
162
|
4 |
|
protected function del_all_cache () { |
|
163
|
4 |
|
$this->permissions_table = null; |
|
164
|
4 |
|
unset($this->cache->all); |
|
165
|
4 |
|
} |
|
166
|
|
|
} |
|
167
|
|
|
|