forked from CakeDC/users
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUsersUrl.php
More file actions
138 lines (123 loc) · 3.96 KB
/
Copy pathUsersUrl.php
File metadata and controls
138 lines (123 loc) · 3.96 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
<?php
declare(strict_types=1);
/**
* Copyright 2010 - 2019, Cake Development Corporation (https://www.cakedc.com)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2010 - 2018, Cake Development Corporation (https://www.cakedc.com)
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace CakeDC\Users\Utility;
use Cake\Core\Configure;
use Cake\Http\ServerRequest;
class UsersUrl
{
/**
* Check if users url uses a custom controller.
*
* @return bool
*/
public static function isCustom()
{
$controller = Configure::read('Users.controller', 'CakeDC/Users.Users');
return $controller !== 'CakeDC/Users.Users';
}
/**
* Get an user action url
*
* @param string $action user action
* @param array $extra extra url attributes
*
* @return array
*/
public static function actionUrl($action, $extra = [])
{
$params = static::actionParams($action);
$params['prefix'] = $params['prefix'] ?: false;
$params['plugin'] = $params['plugin'] ?: false;
return $params + $extra;
}
/**
* Get an user action route. This should not be used for links like HtmlHelper::link
*
* @param string $action user action
* @return array
*/
public static function actionRouteParams($action)
{
return array_filter(static::actionParams($action));
}
/**
* Get an user action route. This should not be used for links like HtmlHelper::link
*
* @param string $action user action
* @return array
*/
public static function actionParams($action)
{
$prefix = null;
$controller = Configure::read('Users.controller', 'CakeDC/Users.Users');
[$plugin, $controller] = pluginSplit($controller);
$parts = explode('/', $controller);
if (isset($parts[1])) {
$controller = $parts[1];
$prefix = $parts[0];
}
return compact('prefix', 'plugin', 'controller', 'action');
}
/**
* Check if the action is the one from a request
*
* @param string $action users action
* @param \Cake\Http\ServerRequest $request the request
*
* @return bool
*/
public static function checkActionOnRequest($action, ServerRequest $request)
{
$route = static::actionParams($action);
foreach ($route as $param => $value) {
if ($request->getParam($param, null) !== $value) {
return false;
}
}
return true;
}
/**
* Setup needed config urls but without overwriting
*
* @return void
*/
public static function setupConfigUrls()
{
$urls = self::getDefaultConfigUrls();
foreach ($urls as $configKey => $url) {
if (!Configure::check($configKey)) {
Configure::write($configKey, $url);
}
}
}
/**
* Get a list of default config urls using static::actionUrl method for users url.
*
* @return array
*/
private static function getDefaultConfigUrls()
{
$loginAction = static::actionUrl('login');
return [
'Users.Profile.route' => static::actionUrl('profile'),
'OneTimePasswordAuthenticator.verifyAction' => static::actionUrl('verify'),
'U2f.startAction' => static::actionUrl('u2f'),
'Auth.AuthenticationComponent.loginAction' => $loginAction,
'Auth.AuthenticationComponent.logoutRedirect' => $loginAction,
'Auth.Authenticators.Form.loginUrl' => $loginAction,
'Auth.Authenticators.Cookie.loginUrl' => $loginAction,
'Auth.Authenticators.SocialPendingEmail.loginUrl' => $loginAction,
'Auth.AuthorizationMiddleware.unauthorizedHandler.url' => $loginAction,
'OAuth.path' => static::actionParams('socialLogin'),
];
}
}