|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @package CleverStyle Framework |
|
4
|
|
|
* @subpackage System module |
|
5
|
|
|
* @category modules |
|
6
|
|
|
* @author Nazar Mokrynskyi <[email protected]> |
|
7
|
|
|
* @license 0BSD |
|
8
|
|
|
*/ |
|
9
|
|
|
namespace cs\modules\System\api\Controller\admin; |
|
10
|
|
|
use |
|
11
|
|
|
cs\ExitException, |
|
12
|
|
|
cs\Group, |
|
13
|
|
|
cs\Response; |
|
14
|
|
|
|
|
15
|
|
|
trait groups { |
|
16
|
|
|
/** |
|
17
|
|
|
* Get array of groups data or data of specific group if id specified or data of several specified groups if specified in ids query parameter |
|
18
|
|
|
* |
|
19
|
|
|
* @param \cs\Request $Request |
|
20
|
|
|
* |
|
21
|
|
|
* @return array|array[] |
|
22
|
|
|
* |
|
23
|
|
|
* @throws ExitException |
|
24
|
|
|
*/ |
|
25
|
|
|
public static function admin_groups___get ($Request) { |
|
26
|
|
|
$Group = Group::instance(); |
|
27
|
|
|
$id = $Request->route_ids(0); |
|
28
|
|
|
$ids = $Request->query('ids'); |
|
|
|
|
|
|
29
|
|
|
if ($id) { |
|
30
|
|
|
$result = $Group->get($id); |
|
31
|
|
|
} elseif ($ids) { |
|
32
|
|
|
$result = $Group->get(explode(',', $ids)); |
|
|
|
|
|
|
33
|
|
|
} else { |
|
34
|
|
|
$result = $Group->get($Group->get_all()); |
|
35
|
|
|
} |
|
36
|
|
|
if (!$result) { |
|
37
|
|
|
throw new ExitException(404); |
|
38
|
|
|
} |
|
39
|
|
|
return $result; |
|
40
|
|
|
} |
|
41
|
|
|
/** |
|
42
|
|
|
* Add new group |
|
43
|
|
|
* |
|
44
|
|
|
* @param \cs\Request $Request |
|
45
|
|
|
* |
|
46
|
|
|
* @throws ExitException |
|
47
|
|
|
*/ |
|
48
|
|
|
public static function admin_groups___post ($Request) { |
|
49
|
|
|
$data = $Request->data('title', 'description'); |
|
|
|
|
|
|
50
|
|
|
if (!$data) { |
|
51
|
|
|
throw new ExitException(400); |
|
52
|
|
|
} |
|
53
|
|
|
if (Group::instance()->add(...array_values($data))) { |
|
|
|
|
|
|
54
|
|
|
Response::instance()->code = 201; |
|
|
|
|
|
|
55
|
|
|
} else { |
|
56
|
|
|
throw new ExitException(500); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
/** |
|
60
|
|
|
* Update group's data |
|
61
|
|
|
* |
|
62
|
|
|
* @param \cs\Request $Request |
|
63
|
|
|
* |
|
64
|
|
|
* @throws ExitException |
|
65
|
|
|
*/ |
|
66
|
|
|
public static function admin_groups___put ($Request) { |
|
67
|
|
|
$id = $Request->route_ids(0); |
|
68
|
|
|
$data = $Request->data('title', 'description'); |
|
|
|
|
|
|
69
|
|
|
if (!$id || !$data) { |
|
70
|
|
|
throw new ExitException(400); |
|
71
|
|
|
} |
|
72
|
|
|
if (!Group::instance()->set($id, ...array_values($data))) { |
|
|
|
|
|
|
73
|
|
|
throw new ExitException(500); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
/** |
|
77
|
|
|
* Delete group |
|
78
|
|
|
* |
|
79
|
|
|
* @param \cs\Request $Request |
|
80
|
|
|
* |
|
81
|
|
|
* @throws ExitException |
|
82
|
|
|
*/ |
|
83
|
|
|
public static function admin_groups___delete ($Request) { |
|
84
|
|
|
$id = $Request->route_ids(0); |
|
85
|
|
|
if (!$id) { |
|
86
|
|
|
throw new ExitException(400); |
|
87
|
|
|
} |
|
88
|
|
|
if (!Group::instance()->del($id)) { |
|
89
|
|
|
throw new ExitException(500); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
/** |
|
93
|
|
|
* Get group's permissions |
|
94
|
|
|
* |
|
95
|
|
|
* @param \cs\Request $Request |
|
96
|
|
|
* |
|
97
|
|
|
* @return array |
|
98
|
|
|
* |
|
99
|
|
|
* @throws ExitException |
|
100
|
|
|
*/ |
|
101
|
|
|
public static function admin_groups_permissions_get ($Request) { |
|
102
|
|
|
$id = $Request->route_ids(0); |
|
103
|
|
|
if (!$id) { |
|
104
|
|
|
throw new ExitException(400); |
|
105
|
|
|
} |
|
106
|
|
|
return Group::instance()->get_permissions($id) ?: []; |
|
107
|
|
|
} |
|
108
|
|
|
/** |
|
109
|
|
|
* Update group's permissions |
|
110
|
|
|
* |
|
111
|
|
|
* @param \cs\Request $Request |
|
112
|
|
|
* |
|
113
|
|
|
* @throws ExitException |
|
114
|
|
|
*/ |
|
115
|
|
|
public static function admin_groups_permissions_put ($Request) { |
|
116
|
|
|
$id = $Request->route_ids(0); |
|
117
|
|
|
$permissions = $Request->data('permissions'); |
|
|
|
|
|
|
118
|
|
|
if (!$id || !$permissions) { |
|
119
|
|
|
throw new ExitException(400); |
|
120
|
|
|
} |
|
121
|
|
|
if (!Group::instance()->set_permissions($permissions, $id)) { |
|
122
|
|
|
throw new ExitException(500); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|