Test Setup Failed
Push — upgrade-cakephp4 ( dd6513...0ad551 )
by giu
05:07
created

AppController   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Importance

Changes 6
Bugs 0 Features 0
Metric Value
eloc 28
c 6
b 0
f 0
dl 0
loc 68
rs 10
wmc 7

3 Methods

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