forked from CakeDC/users
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserHelper.php
More file actions
258 lines (230 loc) · 7.9 KB
/
Copy pathUserHelper.php
File metadata and controls
258 lines (230 loc) · 7.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
<?php
/**
* Copyright 2010 - 2017, Cake Development Corporation (https://www.cakedc.com)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2010 - 2017, Cake Development Corporation (https://www.cakedc.com)
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace CakeDC\Users\View\Helper;
use CakeDC\Users\Controller\Component\UsersAuthComponent;
use Cake\Core\Configure;
use Cake\Event\Event;
use Cake\Utility\Hash;
use Cake\Utility\Inflector;
use Cake\View\Helper;
/**
* User helper
*/
class UserHelper extends Helper
{
public $helpers = ['Html', 'Form', 'CakeDC/Users.AuthLink'];
/**
* Default configuration.
*
* @var array
*/
protected $_defaultConfig = [];
/**
* Social login link
*
* @param string $name name
* @param array $options options
* @return string
*/
public function socialLogin($name, $options = [])
{
if (empty($options['label'])) {
$options['label'] = __d('CakeDC/Users', 'Sign in with');
}
$icon = $this->Html->tag('i', '', [
'class' => __d('CakeDC/Users', 'fa fa-{0}', strtolower($name)),
]);
if (isset($options['title'])) {
$providerTitle = $options['title'];
} else {
$providerTitle = __d('CakeDC/Users', '{0} {1}', Hash::get($options, 'label'), Inflector::camelize($name));
}
$providerClass = __d(
'CakeDC/Users',
'btn btn-social btn-{0} ' . Hash::get($options, 'class') ?: '',
strtolower($name)
);
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 = [])
{
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 null $message logout message info.
* @param array $options Array with option data.
* @return string
*/
public function logout($message = null, $options = [])
{
return $this->AuthLink->link(empty($message) ? __d('CakeDC/Users', 'Logout') : $message, [
'plugin' => 'CakeDC/Users', 'controller' => 'Users', 'action' => 'logout'
], $options);
}
/**
* Welcome display
* @return mixed
*/
public function welcome()
{
$userId = $this->request->session()->read('Auth.User.id');
if (empty($userId)) {
return;
}
$profileUrl = Configure::read('Users.Profile.route');
$label = __d('CakeDC/Users', 'Welcome, {0}', $this->AuthLink->link($this->request->session()->read('Auth.User.first_name') ?: $this->request->session()->read('Auth.User.username'), $profileUrl));
return $this->Html->tag('span', $label, ['class' => 'welcome']);
}
/**
* Add reCaptcha script
* @return void
*/
public function addReCaptchaScript()
{
$this->Html->script('https://www.google.com/recaptcha/api.js', [
'block' => 'script',
]);
}
/**
* Add reCaptcha to the form
* @return mixed
*/
public function addReCaptcha()
{
if (!Configure::read('Users.reCaptcha.key')) {
return $this->Html->tag('p', __d('CakeDC/Users', 'reCaptcha is not configured! Please configure Users.reCaptcha.key'));
}
$this->addReCaptchaScript();
$this->Form->unlockField('g-recaptcha-response');
return $this->Html->tag('div', '', [
'class' => 'g-recaptcha',
'data-sitekey' => Configure::read('Users.reCaptcha.key')
]);
}
/**
* 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 string|array|null $url url that the user is making request.
* @param array $options Array with option data.
* @return string
*/
public function link($title, $url = null, array $options = [])
{
trigger_error(
'UserHelper::link() deprecated since 3.2.1. Use AuthLinkHelper::link() instead',
E_USER_DEPRECATED
);
return $this->AuthLink->link($title, $url, $options);
}
/**
* Returns true if the target url is authorized for the logged in user
*
* @deprecated Since 3.2.1. Use AuthLinkHelper::link() instead
*
* @param string|array|null $url url that the user is making request.
* @return bool
*/
public function isAuthorized($url = null)
{
trigger_error(
'UserHelper::isAuthorized() deprecated since 3.2.1. Use AuthLinkHelper::isAuthorized() instead',
E_USER_DEPRECATED
);
return $this->AuthLink->isAuthorized($url);
}
/**
* 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($name, $provider, $isConnected = false)
{
$linkClass = __d(
'CakeDC/Users',
'btn btn-social btn-{0}' . Hash::get($provider['options'], 'class') ?: '',
strtolower($name)
);
if ($isConnected) {
$title = __d('CakeDC/Users', 'Connected with {0}', Inflector::camelize($name));
return "<a class=\"$linkClass disabled\"><span class=\"fa fa-$name\"></span> $title</a>";
}
$title = __d('CakeDC/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($socialAccounts = [])
{
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;
}
}