-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInterfaceController.php
More file actions
131 lines (110 loc) · 4.23 KB
/
InterfaceController.php
File metadata and controls
131 lines (110 loc) · 4.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
use JSONms\Controllers\RestfulController;
class InterfaceController extends RestfulController {
public function indexAction() {
$stmt = $this->query('get-all-interfaces', [
'userId' => $this->getCurrentUserId(),
]);
$users = $stmt->fetchAll();
$this->responseJson($users);
}
public function getAction($id) {
$interface = $this->getAccessibleInterface($id);
$this->responseJson($interface);
}
public function createAction($data) {
$hash = $this->getHash();
$this->query('insert-interface', [
'hash' => $hash,
'label' => $data->label,
'logo' => $data->logo,
'content' => $data->content,
'webhook' => $data->webhook,
'created_by' => $this->getCurrentUserId(),
]);
// Get inserted interface
$interface = $this->query('get-interface-by-hash', [
'hash' => $hash,
])->fetch(PDO::FETCH_OBJ);
$this->preparePermissions($interface);
$this->responseJson($interface);
}
public function updateAction($id, $data) {
$currentInterface = $this->getAccessibleInterface($id);
if ($currentInterface) {
// Copy current interface to history table
$this->copyToHistory($currentInterface);
// Update interface
$this->query('update-interface-by-uuid', [
'uuid' => $data->uuid,
'label' => $data->label,
'logo' => $data->logo,
'content' => $data->content,
'webhook' => $data->webhook,
'userId' => $this->getCurrentUserId(),
]);
// Clear all existing permissions (will be added later on)
if ($data->created_by === $this->getCurrentUserId()) {
$this->updatePermissions($data);
}
$this->responseJson($data);
}
}
public function deleteAction($id) {
if ($this->hasAccess($id)) {
$stmt = $this->query('delete-interface', [
'uuid' => $id,
'userId' => $this->getCurrentUserId(),
]);
if ($stmt->rowCount() > 0) {
$this->responseJson(true);
}
}
}
private function hasAccess($uuid, $showError = true): bool {
return (bool) $this->getAccessibleInterface($uuid, $showError);
}
private function getAccessibleInterface($uuid, $showError = true): false | stdClass {
$stmt = $this->query('get-accessible-interface-by-uuid', [
'uuid' => $uuid,
'userId' => $this->getCurrentUserId(),
]);
if ($stmt->rowCount() > 0) {
$interface = $stmt->fetch(PDO::FETCH_OBJ);
$this->preparePermissions($interface);
return $interface;
}
if ($showError) {
throwError(403, 'You don\'t have permission to view this interface');
}
return false;
}
private function preparePermissions(&$interface) {
$interface->permission_admin = array_filter(explode(',', $interface->permission_admin ?? ''));
$interface->permission_interface = array_filter(explode(',', $interface->permission_interface ?? ''));
return $interface;
}
private function copyToHistory(stdClass $interface) {
$this->query('insert-history', [
'uuid' => $interface->uuid,
'content' => $interface->content,
'userId' => $this->getCurrentUserId(),
]);
}
private function updatePermissions($interface) {
// Delete current permissions
$this->query('delete-user-permissions', [
'uuid' => $interface->uuid,
]);
// Update with newest permissions
foreach (['interface', 'admin'] as $type) {
foreach ($interface->{'permission_' . $type} as $email) {
$this->query('insert-permissions', [
'uuid' => $interface->uuid,
'type' => $type,
'email' => $email,
]);
}
}
}
}