Passed
Branch master (e828dd)
by giu
03:47
created

AppController::beforeRender()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3.1406

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 3
c 2
b 0
f 0
nc 2
nop 1
dl 0
loc 5
ccs 3
cts 4
cp 0.75
crap 3.1406
rs 10
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 16
    public function initialize(): void {
43 16
        parent::initialize();
44
45 16
        $this->loadComponent('RequestHandler');
46 16
        $this->loadComponent('Flash');
47 16
        $this->loadComponent('Security');
48 16
        $this->loadComponent('Authentication.Authentication');
49 16
        $this->Authentication->allowUnauthenticated(['login', 'logout']);
50 16
    }
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 13
    public function beforeRender(EventInterface $event) {
69 13
        if (!array_key_exists('_serialize', $this->viewBuilder()->getVars()) &&
70 13
                in_array($this->response->getType(), ['application/json', 'application/xml'])
71
        ) {
72
            $this->set('_serialize', true);
73
        }
74 13
    }
75
76
}
77