|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @package CleverStyle CMS |
|
4
|
|
|
* @subpackage System module |
|
5
|
|
|
* @category modules |
|
6
|
|
|
* @author Nazar Mokrynskyi <[email protected]> |
|
7
|
|
|
* @copyright Copyright (c) 2015, Nazar Mokrynskyi |
|
8
|
|
|
* @license MIT License, see license.txt |
|
9
|
|
|
*/ |
|
10
|
|
|
namespace cs\modules\System\api\Controller\admin; |
|
11
|
|
|
use |
|
12
|
|
|
cs\ExitException, |
|
13
|
|
|
cs\Group, |
|
14
|
|
|
cs\Page, |
|
15
|
|
|
cs\Permission, |
|
16
|
|
|
cs\User; |
|
17
|
|
|
trait permissions { |
|
18
|
|
|
/** |
|
19
|
|
|
* Get array of permissions data or data of specific permission if id specified |
|
20
|
|
|
* |
|
21
|
|
|
* @param int[] $route_ids |
|
22
|
|
|
* |
|
23
|
|
|
* @throws ExitException |
|
24
|
|
|
*/ |
|
25
|
|
|
static function admin_permissions___get ($route_ids) { |
|
26
|
|
|
$Permission = Permission::instance(); |
|
27
|
|
|
if (isset($route_ids[0])) { |
|
28
|
|
|
$result = $Permission->get($route_ids[0]); |
|
29
|
|
|
if (!$result) { |
|
30
|
|
|
throw new ExitException(404); |
|
31
|
|
|
} |
|
32
|
|
|
} else { |
|
33
|
|
|
$result = $Permission->get_all(); |
|
34
|
|
|
} |
|
35
|
|
|
Page::instance()->json($result); |
|
36
|
|
|
} |
|
37
|
|
|
/** |
|
38
|
|
|
* Add new permission |
|
39
|
|
|
* |
|
40
|
|
|
* @throws ExitException |
|
41
|
|
|
*/ |
|
42
|
|
|
static function admin_permissions___post () { |
|
43
|
|
|
if (!isset($_POST['group'], $_POST['label'])) { |
|
44
|
|
|
throw new ExitException(400); |
|
45
|
|
|
} |
|
46
|
|
|
if (Permission::instance()->add($_POST['group'], $_POST['label'])) { |
|
47
|
|
|
status_code(201); |
|
48
|
|
|
} else { |
|
49
|
|
|
throw new ExitException(500); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
/** |
|
53
|
|
|
* Update permission's data |
|
54
|
|
|
* |
|
55
|
|
|
* @param int[] $route_ids |
|
56
|
|
|
* |
|
57
|
|
|
* @throws ExitException |
|
58
|
|
|
*/ |
|
59
|
|
|
static function admin_permissions___put ($route_ids) { |
|
60
|
|
|
if (!isset($route_ids[0], $_POST['group'], $_POST['label'])) { |
|
61
|
|
|
throw new ExitException(400); |
|
62
|
|
|
} |
|
63
|
|
|
if (!Permission::instance()->set($route_ids[0], $_POST['group'], $_POST['label'])) { |
|
64
|
|
|
throw new ExitException(500); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
/** |
|
68
|
|
|
* Delete permission |
|
69
|
|
|
* |
|
70
|
|
|
* @param int[] $route_ids |
|
71
|
|
|
* |
|
72
|
|
|
* @throws ExitException |
|
73
|
|
|
*/ |
|
74
|
|
|
static function admin_permissions___delete ($route_ids) { |
|
75
|
|
|
if (!isset($route_ids[0])) { |
|
76
|
|
|
throw new ExitException(400); |
|
77
|
|
|
} |
|
78
|
|
|
if (!Permission::instance()->del($route_ids[0])) { |
|
79
|
|
|
throw new ExitException(500); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
/** |
|
83
|
|
|
* Get permissions for specific item |
|
84
|
|
|
* |
|
85
|
|
|
* @throws ExitException |
|
86
|
|
|
*/ |
|
87
|
|
|
static function admin_permissions_for_item_get () { |
|
88
|
|
|
if (!isset($_GET['group'], $_GET['label'])) { |
|
89
|
|
|
throw new ExitException(400); |
|
90
|
|
|
} |
|
91
|
|
|
$User = User::instance(); |
|
92
|
|
|
$Permission = Permission::instance(); |
|
93
|
|
|
$permission = $Permission->get(null, $_GET['group'], $_GET['label']); |
|
94
|
|
|
$data = [ |
|
95
|
|
|
'groups' => [], |
|
96
|
|
|
'users' => [] |
|
97
|
|
|
]; |
|
98
|
|
|
if ($permission) { |
|
99
|
|
|
$data['groups'] = array_column( |
|
100
|
|
|
$User->db()->qfa( |
|
101
|
|
|
[ |
|
102
|
|
|
"SELECT |
|
103
|
|
|
`id`, |
|
104
|
|
|
`value` |
|
105
|
|
|
FROM `[prefix]groups_permissions` |
|
106
|
|
|
WHERE |
|
107
|
|
|
`permission` = '%s'", |
|
108
|
|
|
$permission[0]['id'] |
|
109
|
|
|
] |
|
110
|
|
|
) ?: [], |
|
111
|
|
|
'value', |
|
112
|
|
|
'id' |
|
113
|
|
|
); |
|
114
|
|
|
$data['users'] = array_column( |
|
115
|
|
|
$User->db()->qfa( |
|
116
|
|
|
[ |
|
117
|
|
|
"SELECT |
|
118
|
|
|
`id`, |
|
119
|
|
|
`value` |
|
120
|
|
|
FROM `[prefix]users_permissions` |
|
121
|
|
|
WHERE |
|
122
|
|
|
`permission` = '%s'", |
|
123
|
|
|
$permission[0]['id'] |
|
124
|
|
|
] |
|
125
|
|
|
) ?: [], |
|
126
|
|
|
'value', |
|
127
|
|
|
'id' |
|
128
|
|
|
); |
|
129
|
|
|
} |
|
130
|
|
|
Page::instance()->json( |
|
131
|
|
|
[ |
|
132
|
|
|
'groups' => (object)$data['groups'], |
|
133
|
|
|
'users' => (object)$data['users'] |
|
134
|
|
|
] |
|
135
|
|
|
); |
|
136
|
|
|
} |
|
137
|
|
|
/** |
|
138
|
|
|
* Get permissions for specific item |
|
139
|
|
|
* |
|
140
|
|
|
* @throws ExitException |
|
141
|
|
|
*/ |
|
142
|
|
|
static function admin_permissions_for_item_post () { |
|
143
|
|
|
if (!isset($_POST['group'], $_POST['label'])) { |
|
144
|
|
|
throw new ExitException(400); |
|
145
|
|
|
} |
|
146
|
|
|
$Group = Group::instance(); |
|
147
|
|
|
$Permission = Permission::instance(); |
|
148
|
|
|
$User = User::instance(); |
|
149
|
|
|
$permission = $Permission->get(null, $_POST['group'], $_POST['label']); |
|
150
|
|
|
// We'll create permission if needed |
|
151
|
|
|
$permission = $permission |
|
152
|
|
|
? $permission[0]['id'] |
|
153
|
|
|
: $Permission->add($_POST['group'], $_POST['label']); |
|
154
|
|
|
if (!$permission) { |
|
155
|
|
|
throw new ExitException(500); |
|
156
|
|
|
} |
|
157
|
|
|
$result = true; |
|
158
|
|
|
if (isset($_POST['groups'])) { |
|
159
|
|
|
foreach ($_POST['groups'] as $group => $value) { |
|
160
|
|
|
$result = $result && $Group->set_permissions([$permission => $value], $group); |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
if (isset($_POST['users'])) { |
|
164
|
|
|
foreach ($_POST['users'] as $user => $value) { |
|
165
|
|
|
$result = $result && $User->set_permissions([$permission => $value], $user); |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
if (!$result) { |
|
169
|
|
|
throw new ExitException(500); |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
|