1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
|
|
|
4
|
|
|
/** |
|
|
|
|
5
|
|
|
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org) |
6
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org) |
|
|
|
|
7
|
|
|
* |
8
|
|
|
* Licensed under The MIT License |
9
|
|
|
* For full copyright and license information, please see the LICENSE.txt |
10
|
|
|
* Redistributions of files must retain the above copyright notice. |
11
|
|
|
* |
12
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org) |
|
|
|
|
13
|
|
|
* @link https://cakephp.org CakePHP(tm) Project |
|
|
|
|
14
|
|
|
* @since 0.2.9 |
|
|
|
|
15
|
|
|
* @license https://opensource.org/licenses/mit-license.php MIT License |
|
|
|
|
16
|
|
|
*/ |
|
|
|
|
17
|
|
|
|
18
|
|
|
namespace App\Controller; |
19
|
|
|
|
20
|
|
|
use Cake\Controller\Controller; |
21
|
|
|
use Cake\Event\EventInterface; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Application Controller |
25
|
|
|
* |
26
|
|
|
* Add your application-wide methods in the class below, your controllers |
27
|
|
|
* will inherit them. |
28
|
|
|
* |
29
|
|
|
* @link https://book.cakephp.org/4/en/controllers.html#the-app-controller |
30
|
|
|
*/ |
|
|
|
|
31
|
|
|
class AppController extends Controller { |
|
|
|
|
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Initialization hook method. |
35
|
|
|
* |
36
|
|
|
* Use this method to add common initialization code like loading components. |
|
|
|
|
37
|
|
|
* |
38
|
|
|
* e.g. `$this->loadComponent('FormProtection');` |
39
|
|
|
* |
40
|
|
|
* @return void |
|
|
|
|
41
|
|
|
*/ |
42
|
19 |
|
public function initialize(): void { |
|
|
|
|
43
|
19 |
|
parent::initialize(); |
44
|
|
|
|
45
|
19 |
|
$this->loadComponent('RequestHandler'); |
46
|
19 |
|
$this->loadComponent('Flash'); |
47
|
19 |
|
$this->loadComponent('Security'); |
48
|
19 |
|
$this->loadComponent('Authentication.Authentication'); |
49
|
19 |
|
$this->Authentication->allowUnauthenticated(['login', 'logout']); |
50
|
19 |
|
} |
51
|
|
|
|
52
|
|
|
public function isAuthorized($user) { |
|
|
|
|
53
|
|
|
// Admin can access every action |
|
|
|
|
54
|
|
|
if (isset($user['role']) && $user['role'] === 'admin') { |
55
|
|
|
return true; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
// Default permit |
|
|
|
|
59
|
|
|
return true; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Before render callback. |
64
|
|
|
* |
65
|
|
|
* @param \Cake\Event\Event $event The beforeRender event. |
|
|
|
|
66
|
|
|
* @return \Cake\Network\Response|null|void |
|
|
|
|
67
|
|
|
*/ |
68
|
15 |
|
public function beforeRender(EventInterface $event) { |
|
|
|
|
69
|
15 |
|
if (!array_key_exists('_serialize', $this->viewBuilder()->getVars()) && |
|
|
|
|
70
|
15 |
|
in_array($this->response->getType(), ['application/json', 'application/xml']) |
|
|
|
|
71
|
|
|
) { |
72
|
|
|
$this->set('_serialize', true); |
73
|
|
|
} |
74
|
15 |
|
} |
75
|
|
|
|
76
|
|
|
} |
77
|
|
|
|