1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Licensed under The GPL-3.0 License |
4
|
|
|
* For full copyright and license information, please see the LICENSE.txt |
5
|
|
|
* Redistributions of files must retain the above copyright notice. |
6
|
|
|
* |
7
|
|
|
* @since 2.0.0 |
8
|
|
|
* @author Christopher Castro <[email protected]> |
9
|
|
|
* @link http://www.quickappscms.org |
10
|
|
|
* @license http://opensource.org/licenses/gpl-3.0.html GPL-3.0 License |
11
|
|
|
*/ |
12
|
|
|
namespace User\Controller\Admin; |
13
|
|
|
|
14
|
|
|
use Cake\Filesystem\File; |
15
|
|
|
use User\Controller\AppController; |
16
|
|
|
use User\Utility\AcoManager; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Permissions manager controller. |
20
|
|
|
* |
21
|
|
|
* Provides full CRUD for permissions. |
22
|
|
|
*/ |
23
|
|
|
class PermissionsController extends AppController |
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Shows tree list of ACOS grouped by plugin. |
28
|
|
|
* |
29
|
|
|
* @return void |
30
|
|
|
*/ |
31
|
|
|
public function index() |
32
|
|
|
{ |
33
|
|
|
$this->loadModel('User.Acos'); |
34
|
|
|
$tree = $this->Acos |
35
|
|
|
->find('threaded') |
36
|
|
|
->order(['lft' => 'ASC']) |
37
|
|
|
->all(); |
38
|
|
|
|
39
|
|
|
$this->title(__d('user', 'Permissions')); |
40
|
|
|
$this->set(compact('tree')); |
41
|
|
|
$this->Breadcrumb |
42
|
|
|
->push('/admin/user/manage') |
43
|
|
|
->push(__d('user', 'Permissions'), ['plugin' => 'User', 'controller' => 'permissions', 'action' => 'index']); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Shows the permissions table for the given ACO. |
48
|
|
|
* |
49
|
|
|
* @param int $acoId ACO's ID |
50
|
|
|
* @return void |
51
|
|
|
*/ |
52
|
|
|
public function aco($acoId) |
53
|
|
|
{ |
54
|
|
|
$this->loadModel('User.Acos'); |
55
|
|
|
$aco = $this->Acos->get($acoId, ['contain' => ['Roles']]); |
56
|
|
|
$path = $this->Acos->find('path', ['for' => $acoId])->extract('alias')->toArray(); |
57
|
|
|
|
58
|
|
|
if (!empty($this->request->data['roles'])) { |
59
|
|
|
$aco = $this->Acos->patchEntity($aco, $this->request->data); |
60
|
|
|
$save = $this->Acos->save($aco); |
61
|
|
|
|
62
|
|
|
if (!$this->request->isAjax()) { |
63
|
|
|
if ($save) { |
64
|
|
|
$this->Flash->success(__d('user', 'Permissions were successfully saved!')); |
65
|
|
|
} else { |
66
|
|
|
$this->Flash->danger(__d('user', 'Permissions could not be saved')); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$roles = $this->Acos->Roles |
72
|
|
|
->find('list') |
73
|
|
|
->where(['Roles.id <>' => ROLE_ID_ADMINISTRATOR]); |
74
|
|
|
$this->title(__d('user', 'Permissions')); |
75
|
|
|
$this->set(compact('aco', 'roles', 'path')); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Analyzes each plugin and adds any missing ACO path to the tree. It won't |
80
|
|
|
* remove any invalid ACO unless 'sync' GET parameter is present in URL. |
81
|
|
|
* |
82
|
|
|
* @return void Redirects to previous page |
83
|
|
|
*/ |
84
|
|
|
public function update() |
85
|
|
|
{ |
86
|
|
|
$sync = !empty($this->request->query['sync']) ? true : false; |
87
|
|
|
if (AcoManager::buildAcos(null, $sync)) { |
88
|
|
|
$this->Flash->success(__d('user', 'Permissions tree was successfully updated!')); |
89
|
|
|
} else { |
90
|
|
|
$this->Flash->danger(__d('user', 'Some errors occur while updating permissions tree.')); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$this->title(__d('user', 'Update Permissions')); |
94
|
|
|
$this->redirect($this->referer()); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Exports all permissions as a JSON file. |
99
|
|
|
* |
100
|
|
|
* @return \Cake\Network\Response Forces JSON download |
101
|
|
|
*/ |
102
|
|
|
public function export() |
103
|
|
|
{ |
104
|
|
|
$this->loadModel('User.Acos'); |
105
|
|
|
$out = []; |
106
|
|
|
$permissions = $this->Acos->Permissions |
107
|
|
|
->find() |
108
|
|
|
->contain(['Acos', 'Roles']) |
109
|
|
|
->all(); |
110
|
|
|
|
111
|
|
View Code Duplication |
foreach ($permissions as $permission) { |
112
|
|
|
if (!isset($out[$permission->role->slug])) { |
113
|
|
|
$out[$permission->role->slug] = []; |
114
|
|
|
} |
115
|
|
|
$out[$permission->role->slug][] = implode( |
116
|
|
|
'/', |
117
|
|
|
$this->Acos |
118
|
|
|
->find('path', ['for' => $permission->aco->id]) |
119
|
|
|
->extract('alias') |
120
|
|
|
->toArray() |
121
|
|
|
); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
$this->title(__d('user', 'Export Permissions')); |
125
|
|
|
$this->response->body(json_encode($out, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)); |
126
|
|
|
$this->response->type('json'); |
127
|
|
|
$this->response->download('permissions_' . date('Y-m-d@H:i:s-\U\TC', time()) . '.json'); |
128
|
|
|
|
129
|
|
|
return $this->response; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Imports the given permissions given as a JSON file. |
134
|
|
|
* |
135
|
|
|
* @return void Redirects to previous page |
136
|
|
|
*/ |
137
|
|
|
public function import() |
138
|
|
|
{ |
139
|
|
|
if (!empty($this->request->data['json'])) { |
140
|
|
|
$this->loadModel('User.Acos'); |
141
|
|
|
$json = $this->request->data['json']; |
142
|
|
|
$dst = TMP . $json['name']; |
143
|
|
|
if (file_exists($dst)) { |
144
|
|
|
$file = new File($dst); |
145
|
|
|
$file->delete(); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
if (!move_uploaded_file($json['tmp_name'], $dst)) { |
149
|
|
|
$this->Flash->danger(__d('user', 'File could not be uploaded, please check write permissions on /tmp directory.')); |
150
|
|
|
} else { |
151
|
|
|
$file = new File($dst); |
152
|
|
|
$info = json_decode($file->read(), true); |
153
|
|
|
$added = []; |
154
|
|
|
$error = false; |
155
|
|
|
|
156
|
|
|
if (is_array($info) && !empty($info)) { |
157
|
|
|
foreach ($info as $role => $paths) { |
158
|
|
|
if (!is_string($role)) { |
159
|
|
|
$error = true; |
160
|
|
|
$this->Flash->danger(__d('user', 'Given file seems to be corrupt.')); |
161
|
|
|
break; |
162
|
|
|
} |
163
|
|
|
$role = $this->Acos->Roles |
164
|
|
|
->find() |
165
|
|
|
->where(['slug' => $role]) |
166
|
|
|
->limit(1) |
167
|
|
|
->first(); |
168
|
|
|
|
169
|
|
|
if (!$role) { |
170
|
|
|
continue; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
if (is_array($paths)) { |
174
|
|
|
foreach ($paths as $path) { |
175
|
|
|
$nodes = $this->Acos->node($path); |
176
|
|
|
|
177
|
|
|
if ($nodes) { |
178
|
|
|
$leaf = $nodes->first(); |
179
|
|
|
$exists = $this->Acos->Permissions |
180
|
|
|
->exists([ |
181
|
|
|
'aco_id' => $leaf->id, |
182
|
|
|
'role_id' => $role->id |
183
|
|
|
]); |
184
|
|
|
if (!$exists) { |
185
|
|
|
$newPermission = $this->Acos->Permissions->newEntity([ |
186
|
|
|
'aco_id' => $leaf->id, |
187
|
|
|
'role_id' => $role->id |
188
|
|
|
]); |
189
|
|
|
if ($this->Acos->Permissions->save($newPermission)) { |
190
|
|
|
$added[] = "<strong>{$role->name}</strong>: {$path}"; |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
} else { |
196
|
|
|
$error = true; |
197
|
|
|
$this->Flash->danger(__d('user', 'Given file seems to be corrupt.')); |
198
|
|
|
break; |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
} else { |
202
|
|
|
$error = true; |
203
|
|
|
$this->Flash->danger(__d('user', 'Invalid file given.')); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
if (!$error) { |
207
|
|
|
if (!empty($added)) { |
208
|
|
|
$imported = '<br />' . implode('<br />', $added); |
209
|
|
|
$this->Flash->success(__d('user', 'The following entries were imported: {0}', $imported)); |
210
|
|
|
} else { |
211
|
|
|
$this->Flash->success(__d('user', 'Success, but nothing was imported')); |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
$this->title(__d('user', 'Import Permissions')); |
218
|
|
|
$this->redirect($this->referer()); |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|