Passed
Pull Request — master (#38)
by giu
04:13
created

AppController   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Test Coverage

Coverage 72.21%

Importance

Changes 6
Bugs 0 Features 0
Metric Value
eloc 30
c 6
b 0
f 0
dl 0
loc 65
ccs 13
cts 18
cp 0.7221
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A isAuthorized() 0 8 3
A initialize() 0 31 1
A beforeRender() 0 5 3
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('Auth', [
49 16
            'authorize' => ['Controller'],
50
            'authenticate' => [
51
                'Form' => [
52
                    'fields' => [
53
                        'username' => 'email',
54
                        'password' => 'password'
55
                    ]
56
                ]
57
            ],
58
            'loginAction' => [
59
                'prefix' => 'Admin',
60
                'controller' => 'Users',
61
                'action' => 'login',
62
                'plugin' => null,
63
                '_ext' => null,
64
            ],
65
            'logoutAction' => [
66
                'prefix' => 'Admin',
67
                'controller' => 'Users',
68
                'action' => 'logout'
69
            ],
70 16
            'unauthorizedRedirect' => $this->referer()
71
        ]);
72 16
        $this->Auth->allow();
73 16
    }
74
75
    public function isAuthorized($user) {
76
        // Admin can access every action
77
        if (isset($user['role']) && $user['role'] === 'admin') {
78
            return true;
79
        }
80
81
        // Default permit
82
        return true;
83
    }
84
85
    /**
86
     * Before render callback.
87
     *
88
     * @param \Cake\Event\Event $event The beforeRender event.
89
     * @return \Cake\Network\Response|null|void
90
     */
91 13
    public function beforeRender(EventInterface $event) {
92 13
        if (!array_key_exists('_serialize', $this->viewBuilder()->getVars()) &&
93 13
                in_array($this->response->getType(), ['application/json', 'application/xml'])
94
        ) {
95
            $this->set('_serialize', true);
96
        }
97 13
    }
98
99
}
100