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
|
|
|
protected function cdb () { |
42
|
|
|
return Config::instance()->module('System')->db('users'); |
43
|
|
|
} |
44
|
|
|
protected function construct () { |
45
|
|
|
$this->cache = Cache::prefix('permissions'); |
46
|
|
|
} |
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
|
|
|
function get ($id = null, $group = null, $label = null) { |
59
|
|
|
if ($group !== null || $label !== null) { |
60
|
|
|
return $this->read( |
61
|
|
|
$this->search( |
62
|
|
|
[ |
63
|
|
|
'group' => $group, |
64
|
|
|
'label' => $label |
65
|
|
|
], |
66
|
|
|
1, |
67
|
|
|
PHP_INT_MAX, |
68
|
|
|
'id', |
69
|
|
|
true |
70
|
|
|
) ?: [] |
71
|
|
|
); |
72
|
|
|
} else { |
73
|
|
|
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
|
|
|
function add ($group, $label) { |
85
|
|
|
$id = $this->create($group, $label); |
86
|
|
|
if ($id) { |
87
|
|
|
$this->del_all_cache(); |
88
|
|
|
} |
89
|
|
|
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
|
|
|
function set ($id, $group, $label) { |
101
|
|
|
$result = $this->update($id, $group, $label); |
102
|
|
|
if ($result) { |
103
|
|
|
$this->del_all_cache(); |
104
|
|
|
} |
105
|
|
|
return $result; |
106
|
|
|
} |
107
|
|
|
/** |
108
|
|
|
* Deletion of permission or array of permissions |
109
|
|
|
* |
110
|
|
|
* @param int|int[] $id |
111
|
|
|
* |
112
|
|
|
* @return bool |
113
|
|
|
*/ |
114
|
|
|
function del ($id) { |
115
|
|
|
$id = implode(',', (array)_int($id)); |
116
|
|
|
$result = $this->db_prime()->q( |
117
|
|
|
[ |
118
|
|
|
"DELETE FROM `[prefix]permissions` |
119
|
|
|
WHERE `id` IN ($id)", |
120
|
|
|
"DELETE FROM `[prefix]users_permissions` |
121
|
|
|
WHERE `permission` IN ($id)", |
122
|
|
|
"DELETE FROM `[prefix]groups_permissions` |
123
|
|
|
WHERE `permission` IN ($id)" |
124
|
|
|
] |
125
|
|
|
); |
126
|
|
|
if ($result) { |
127
|
|
|
$Cache = $this->cache; |
128
|
|
|
unset( |
129
|
|
|
$Cache->users, |
130
|
|
|
$Cache->groups |
131
|
|
|
); |
132
|
|
|
$this->del_all_cache(); |
133
|
|
|
} |
134
|
|
|
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
|
|
|
function get_all () { |
142
|
|
|
if ($this->permissions_table === null) { |
143
|
|
|
$this->permissions_table = $this->cache->get( |
144
|
|
|
'all', |
145
|
|
|
function () { |
146
|
|
|
$data = $this->read( |
147
|
|
|
$this->search([], 1, PHP_INT_MAX, 'id', true) ?: [] |
148
|
|
|
); |
149
|
|
|
$all_permissions = []; |
150
|
|
|
foreach ($data as $item) { |
151
|
|
|
$all_permissions[$item['group']][$item['label']] = $item['id']; |
152
|
|
|
} |
153
|
|
|
return $all_permissions; |
154
|
|
|
} |
155
|
|
|
); |
156
|
|
|
} |
157
|
|
|
return $this->permissions_table; |
158
|
|
|
} |
159
|
|
|
/** |
160
|
|
|
* Deletion of permission table (is used after adding, setting or deletion of permission) |
161
|
|
|
*/ |
162
|
|
|
protected function del_all_cache () { |
163
|
|
|
$this->permissions_table = null; |
164
|
|
|
unset($this->cache->all); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|