|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* BEdita, API-first content management framework |
|
6
|
|
|
* Copyright 2025 Chialab Srl |
|
7
|
|
|
* |
|
8
|
|
|
* This file is part of BEdita: you can redistribute it and/or modify |
|
9
|
|
|
* it under the terms of the GNU Lesser General Public License as published |
|
10
|
|
|
* by the Free Software Foundation, either version 3 of the License, or |
|
11
|
|
|
* (at your option) any later version. |
|
12
|
|
|
* |
|
13
|
|
|
* See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details. |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
namespace App\Controller; |
|
17
|
|
|
|
|
18
|
|
|
use BEdita\SDK\BEditaClientException; |
|
19
|
|
|
use Cake\Event\EventInterface; |
|
20
|
|
|
use Cake\Http\Exception\UnauthorizedException; |
|
21
|
|
|
use Cake\Http\Response; |
|
22
|
|
|
use Cake\Utility\Hash; |
|
23
|
|
|
use Psr\Log\LogLevel; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Roles Controller: list |
|
27
|
|
|
*/ |
|
28
|
|
|
class RolesController extends AppController |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* @inheritDoc |
|
32
|
|
|
*/ |
|
33
|
|
|
public function beforeFilter(EventInterface $event): ?Response |
|
34
|
|
|
{ |
|
35
|
|
|
parent::beforeFilter($event); |
|
36
|
|
|
if (!$this->allowed()) { |
|
37
|
|
|
throw new UnauthorizedException(__('You are not authorized to access this resource')); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
return null; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Check if the request is allowed. |
|
45
|
|
|
* |
|
46
|
|
|
* @return bool |
|
47
|
|
|
*/ |
|
48
|
|
|
protected function allowed(): bool |
|
49
|
|
|
{ |
|
50
|
|
|
// block requests from browser address bar |
|
51
|
|
|
$sameOrigin = (string)Hash::get((array)$this->request->getHeader('Sec-Fetch-Site'), 0) === 'same-origin'; |
|
52
|
|
|
$noReferer = empty((array)$this->request->getHeader('Referer')); |
|
53
|
|
|
$isNavigate = in_array('navigate', (array)$this->request->getHeader('Sec-Fetch-Mode')); |
|
54
|
|
|
if (!$sameOrigin || $noReferer || $isNavigate) { |
|
55
|
|
|
return false; |
|
56
|
|
|
} |
|
57
|
|
|
/** @var \Authentication\Identity|null $user */ |
|
58
|
|
|
$user = $this->Authentication->getIdentity(); |
|
59
|
|
|
$roles = empty($user) ? [] : (array)$user->get('roles'); |
|
60
|
|
|
if (empty($roles)) { |
|
61
|
|
|
return false; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
return true; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* List all roles. |
|
69
|
|
|
* |
|
70
|
|
|
* @return \Cake\Http\Response|null |
|
71
|
|
|
*/ |
|
72
|
|
|
public function list(): ?Response |
|
73
|
|
|
{ |
|
74
|
|
|
$this->viewBuilder()->setClassName('Json'); |
|
75
|
|
|
$this->getRequest()->allowMethod(['get']); |
|
76
|
|
|
try { |
|
77
|
|
|
$response = $this->apiClient->get('roles', []); |
|
78
|
|
|
} catch (BEditaClientException $error) { |
|
79
|
|
|
$this->log($error->getMessage(), LogLevel::ERROR); |
|
80
|
|
|
$this->set(compact('error')); |
|
81
|
|
|
$this->setSerialize(['error']); |
|
82
|
|
|
|
|
83
|
|
|
return null; |
|
84
|
|
|
} |
|
85
|
|
|
$this->set((array)$response); |
|
86
|
|
|
$this->setSerialize(array_keys($response)); |
|
87
|
|
|
|
|
88
|
|
|
return null; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|