-
Notifications
You must be signed in to change notification settings - Fork 289
Expand file tree
/
Copy pathUserHelper.php
More file actions
354 lines (317 loc) · 10.9 KB
/
Copy pathUserHelper.php
File metadata and controls
354 lines (317 loc) · 10.9 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
<?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\View\Helper;
use Cake\Core\Configure;
use Cake\Utility\Hash;
use Cake\Utility\Inflector;
use Cake\View\Helper;
use CakeDC\Users\Utility\UsersUrl;
use InvalidArgumentException;
/**
* User helper
*
* @property \CakeDC\Users\View\Helper\AuthLinkHelper $AuthLink
* @property \Cake\View\Helper\HtmlHelper $Html
* @property \Cake\View\Helper\FormHelper $Form
*/
class UserHelper extends Helper
{
protected array $helpers = ['Html', 'Form', 'CakeDC/Users.AuthLink'];
/**
* @inheritDoc
*/
protected array $_defaultConfig = [];
/**
* Social login link
*
* @param string $name name
* @param array $options options
* @return string
*/
public function socialLogin(string $name, array $options = []): string
{
if (empty($options['label'])) {
$options['label'] = __d('cake_d_c/users', 'Sign in with');
}
$icon = $this->Html->tag('i', '', [
'class' => 'fa fa-' . strtolower($name),
]);
if (isset($options['title'])) {
$providerTitle = $options['title'];
} else {
$providerTitle = $options['label'] . ' ' . Inflector::camelize($name);
}
$providerClass = 'btn btn-social btn-' . strtolower($name);
$optionClass = $options['class'] ?? null;
if ($optionClass) {
$providerClass .= " $optionClass";
}
return $this->Html->link($icon . $providerTitle, "/auth/$name", [
'escape' => false, 'class' => $providerClass,
]);
}
/**
* All available Social Login Icons
*
* @param array $providerOptions Provider link options.
* @return array Links to Social Login Urls
*/
public function socialLoginList(array $providerOptions = []): array
{
if (!Configure::read('Users.Social.login')) {
return [];
}
$outProviders = [];
$providers = Configure::read('OAuth.providers');
foreach ($providers as $provider => $options) {
if (
!empty($options['options']['redirectUri']) &&
!empty($options['options']['clientId']) &&
!empty($options['options']['clientSecret'])
) {
if (isset($providerOptions[$provider])) {
$options['options'] = Hash::merge($options['options'], $providerOptions[$provider]);
}
$outProviders[] = $this->socialLogin($provider, $options['options']);
}
}
return $outProviders;
}
/**
* Logout link
*
* @param string|null $message logout message info.
* @param array $options Array with option data.
* @return string
*/
public function logout(?string $message = null, array $options = []): string
{
$url = UsersUrl::actionUrl('logout');
$title = empty($message) ? __d('cake_d_c/users', 'Logout') : $message;
return $this->AuthLink->link($title, $url, $options);
}
/**
* Welcome display
*
* @return string|null
*/
public function welcome(): ?string
{
$identity = $this->getView()->getRequest()->getAttribute('identity');
if (!$identity) {
return null;
}
$profileUrl = Configure::read('Users.Profile.route');
$title = $identity['first_name'] ?? null;
$title = $title ?: ($identity['username'] ?? null);
$title = is_array($title) ? '-' : (string)$title;
$label = __d(
'cake_d_c/users',
'Welcome, {0}',
$this->AuthLink->link($title, $profileUrl),
);
return $this->Html->tag('span', $label, ['class' => 'welcome']);
}
/**
* Add reCaptcha script
*
* @return void
*/
public function addReCaptchaScript(): void
{
$this->Html->script('https://www.google.com/recaptcha/api.js', [
'block' => 'script',
]);
}
/**
* @return void
*/
public function addPasswordMeterScript(): void
{
$this->Html->script('CakeDC/Users.pswmeter', [
'block' => 'script',
]);
}
/**
* @return string
*/
public function addPasswordMeter(): string
{
$this->addPasswordMeterScript();
$requiredScore = Configure::read('Users.passwordMeter.requiredScore', 3);
$messagesList = json_encode(
Configure::read(
'Users.passwordMeter.messagesList',
['Empty password', 'Too simple', 'Simple', 'That\'s OK', 'Great password!'],
),
);
$pswMinLength = Configure::read('Users.passwordMinLength', 8);
$showMessage = Configure::read('Users.passwordMeter.showMessage', true) ? 'true' : 'false';
$script = $this->Html->scriptBlock("
const requiredScore = $requiredScore;
const messagesList = $messagesList;
const pswMinLength = $pswMinLength;
const showMessage = $showMessage;
", ['defer' => true]);
return $this->Html->tag('div', '', ['id' => 'pswmeter']) .
$this->Html->tag('div', '', ['id' => 'pswmeter-message']) . $script;
}
/**
* Add reCaptcha to the form
*
* @return mixed
*/
public function addReCaptcha(): mixed
{
if (!Configure::read('Users.reCaptcha.key')) {
return $this->Html->tag(
'p',
__d(
'cake_d_c/users',
'reCaptcha is not configured! Please configure Users.reCaptcha.key',
),
);
}
$this->addReCaptchaScript();
$version = Configure::read('Users.reCaptcha.version', 2);
$method = "addReCaptchaV$version";
if (method_exists($this, $method)) {
try {
$this->Form->unlockField('g-recaptcha-response');
} catch (\Exception $e) {
}
return $this->{$method}();
}
throw new InvalidArgumentException(
__d('cake_d_c/users', 'reCaptcha version is wrong. Please configure Users.reCaptcha.version as 2 or 3'),
);
}
/**
* Add required element for reCaptcha v2
*
* @return string
*/
private function addReCaptchaV2(): string
{
deprecationWarning('14.2.0', 'reCaptcha version 3 will be used as default in version 15.0.0');
return $this->Html->tag('div', '', [
'class' => 'g-recaptcha',
'data-sitekey' => Configure::read('Users.reCaptcha.key'),
'data-theme' => Configure::read('Users.reCaptcha.theme') ?: 'light',
'data-size' => Configure::read('Users.reCaptcha.size') ?: 'normal',
'data-tabindex' => Configure::read('Users.reCaptcha.tabindex') ?: '3',
]);
}
/**
* Add required script for reCaptcha v3
*/
private function addReCaptchaV3(): void
{
$this->Html->script('CakeDC/Users.reCaptchaV3', [
'block' => 'script',
]);
}
/**
* Add required options for reCaptcha v3
*
* @param string $title
* @param array $options
* @return string
*/
public function button(string $title, array $options = []): string
{
$key = Configure::read('Users.reCaptcha.key');
if ($key && Configure::read('Users.reCaptcha.version', 2) === 3) {
$options = array_merge($options, [
'class' => 'g-recaptcha',
'data-sitekey' => $key,
'data-callback' => 'onSubmit',
'data-action' => 'submit',
]);
}
return $this->Form->button($title, $options);
}
/**
* Generate a link if the target url is authorized for the logged in user
*
* @deprecated Since 3.2.1. Use AuthLinkHelper::link() instead
* @param string $title link's title.
* @param array|string|null $url url that the user is making request.
* @param array $options Array with option data.
* @return string
*/
public function link(string $title, array|string|null $url = null, array $options = []): string
{
trigger_error(
'UserHelper::link() deprecated since 3.2.1. Use AuthLinkHelper::link() instead',
E_USER_DEPRECATED,
);
return $this->AuthLink->link($title, $url, $options);
}
/**
* Create links for all social providers enabled social link (connect)
*
* @param string $name Provider name in lowercase
* @param array $provider Provider configuration
* @param bool $isConnected User is connected with this provider
* @return string
*/
public function socialConnectLink(string $name, array $provider, bool $isConnected = false): string
{
$optionClass = $provider['options']['class'] ?? null;
$linkClass = 'btn btn-social btn-' . strtolower($name) . ($optionClass ? ' ' . $optionClass : '');
if ($isConnected) {
$title = __d('cake_d_c/users', 'Connected with {0}', Inflector::camelize($name));
return "<a class=\"$linkClass disabled\"><span class=\"fa fa-$name\"></span> $title</a>";
}
$title = __d('cake_d_c/users', 'Connect with {0}', Inflector::camelize($name));
return $this->Html->link(
"<span class=\"fa fa-$name\"></span> $title",
"/link-social/$name",
[
'escape' => false,
'class' => $linkClass,
],
);
}
/**
* Create links for all social providers enabled social link (connect)
*
* @param array $socialAccounts All social accounts connected by a user.
* @return string
*/
public function socialConnectLinkList(array $socialAccounts = []): string
{
if (!Configure::read('Users.Social.login')) {
return '';
}
$html = '';
$connectedProviders = array_map(
function ($item) {
return strtolower($item->provider);
},
(array)$socialAccounts,
);
$providers = Configure::read('OAuth.providers');
foreach ($providers as $name => $provider) {
if (
!empty($provider['options']['callbackLinkSocialUri']) &&
!empty($provider['options']['linkSocialUri']) &&
!empty($provider['options']['clientId']) &&
!empty($provider['options']['clientSecret'])
) {
$html .= $this->socialConnectLink($name, $provider, in_array($name, $connectedProviders));
}
}
return $html;
}
}