forked from CakeDC/users
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUsersAuthComponent.php
More file actions
161 lines (146 loc) · 4.77 KB
/
Copy pathUsersAuthComponent.php
File metadata and controls
161 lines (146 loc) · 4.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
/**
* Copyright 2010 - 2015, Cake Development Corporation (http://cakedc.com)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2010 - 2015, Cake Development Corporation (http://cakedc.com)
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace CakeDC\Users\Controller\Component;
use CakeDC\Users\Exception\BadConfigurationException;
use Cake\Controller\Component;
use Cake\Core\Configure;
use Cake\Event\Event;
use Cake\Network\Request;
use Cake\Routing\Router;
use Cake\Utility\Hash;
class UsersAuthComponent extends Component
{
const EVENT_IS_AUTHORIZED = 'Users.Component.UsersAuth.isAuthorized';
const EVENT_BEFORE_LOGIN = 'Users.Component.UsersAuth.beforeLogin';
const EVENT_AFTER_LOGIN = 'Users.Component.UsersAuth.afterLogin';
const EVENT_FAILED_SOCIAL_LOGIN = 'Users.Component.UsersAuth.failedSocialLogin';
const EVENT_AFTER_COOKIE_LOGIN = 'Users.Component.UsersAuth.afterCookieLogin';
const EVENT_BEFORE_REGISTER = 'Users.Component.UsersAuth.beforeRegister';
const EVENT_AFTER_REGISTER = 'Users.Component.UsersAuth.afterRegister';
const EVENT_BEFORE_LOGOUT = 'Users.Component.UsersAuth.beforeLogout';
const EVENT_AFTER_LOGOUT = 'Users.Component.UsersAuth.afterLogout';
/**
* Initialize method, setup Auth if not already done passing the $config provided and
* setup the default table to Users.Users if not provided
*
* @param array $config config options
* @return void
*/
public function initialize(array $config)
{
parent::initialize($config);
$this->_validateConfig();
$this->_initAuth();
if (Configure::read('Users.Social.login')) {
$this->_loadSocialLogin();
}
if (Configure::read('Users.RememberMe.active')) {
$this->_loadRememberMe();
}
$this->_attachPermissionChecker();
}
/**
* Load Social Auth object
*
* @return void
*/
protected function _loadSocialLogin()
{
$this->_registry->getController()->Auth->config('authenticate', [
'CakeDC/Users.Social'
], true);
}
/**
* Load RememberMe component and Auth objects
*
* @return void
*/
protected function _loadRememberMe()
{
$this->_registry->getController()->loadComponent('CakeDC/Users.RememberMe');
}
/**
* Attach the isUrlAuthorized event to allow using the Auth authorize from the UserHelper
*
* @return void
*/
protected function _attachPermissionChecker()
{
$this->_registry->getController()->eventManager()->on(self::EVENT_IS_AUTHORIZED, [], [$this, 'isUrlAuthorized']);
}
/**
* Initialize the AuthComponent and configure allowed actions
*
* @return void
*/
protected function _initAuth()
{
if (Configure::read('Users.auth')) {
//initialize Auth
$this->_registry->getController()->loadComponent('Auth', Configure::read('Auth'));
}
$this->_registry->getController()->Auth->allow([
'register',
'validateEmail',
'resendTokenValidation',
'login',
'twitterLogin',
'socialEmail',
'resetPassword',
'requestResetPassword',
'changePassword',
'endpoint',
'authenticated'
]);
}
/**
* Check if a given url is authorized
*
* @param Event $event event
*
* @return bool
*/
public function isUrlAuthorized(Event $event)
{
$user = $this->_registry->getController()->Auth->user();
if (empty($user)) {
return false;
}
$url = Hash::get((array)$event->data, 'url');
if (empty($url)) {
return false;
}
if (is_array($url)) {
$requestUrl = Router::reverse($url);
$requestParams = Router::parse($requestUrl);
} else {
$requestParams = Router::parse($url);
$requestUrl = $url;
}
$request = new Request($requestUrl);
$request->params = $requestParams;
$isAuthorized = $this->_registry->getController()->Auth->isAuthorized(null, $request);
return $isAuthorized;
}
/**
* Validate if the passed configuration makes sense
*
* @throws BadConfigurationException
* @return void
*/
protected function _validateConfig()
{
if (!Configure::read('Users.Email.required') && Configure::read('Users.Email.validate')) {
$message = __d('Users', 'You can\'t enable email validation workflow if use_email is false');
throw new BadConfigurationException($message);
}
}
}