1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org) |
4
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) |
5
|
|
|
* |
6
|
|
|
* Licensed under The MIT License |
7
|
|
|
* For full copyright and license information, please see the LICENSE.txt |
8
|
|
|
* Redistributions of files must retain the above copyright notice. |
9
|
|
|
* |
10
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) |
11
|
|
|
* @link http://cakephp.org CakePHP(tm) Project |
12
|
|
|
* @since 0.2.9 |
13
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License |
14
|
|
|
*/ |
15
|
|
|
namespace App\Controller; |
16
|
|
|
|
17
|
|
|
use Cake\Controller\Controller; |
18
|
|
|
use Cake\Event\Event; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Application Controller |
22
|
|
|
* |
23
|
|
|
* Add your application-wide methods in the class below, your controllers |
24
|
|
|
* will inherit them. |
25
|
|
|
* |
26
|
|
|
* @link http://book.cakephp.org/3.0/en/controllers.html#the-app-controller |
27
|
|
|
*/ |
28
|
|
|
class AppController extends Controller |
29
|
|
|
{ |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Initialization hook method. |
33
|
|
|
* |
34
|
|
|
* Use this method to add common initialization code like loading components. |
35
|
|
|
* |
36
|
|
|
* e.g. `$this->loadComponent('Security');` |
37
|
|
|
* |
38
|
|
|
* @return void |
39
|
|
|
*/ |
40
|
|
|
public function initialize() |
41
|
|
|
{ |
42
|
|
|
parent::initialize(); |
43
|
|
|
|
44
|
|
|
$this->loadComponent('RequestHandler'); |
45
|
|
|
$this->loadComponent('Flash'); |
46
|
|
|
$this->loadComponent('Security'); |
47
|
|
|
$this->loadComponent('Csrf'); |
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(Event $event) |
92
|
|
|
{ |
93
|
|
|
if (!array_key_exists('_serialize', $this->viewVars) && |
94
|
|
|
in_array($this->response->type(), ['application/json', 'application/xml']) |
|
|
|
|
95
|
|
|
) { |
96
|
|
|
$this->set('_serialize', true); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.