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
159 lines (144 loc) · 4.61 KB
/
Copy pathUserHelper.php
File metadata and controls
159 lines (144 loc) · 4.61 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
<?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\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'];
/**
* Default configuration.
*
* @var array
*/
protected $_defaultConfig = [];
/**
* beforeLayout callback loads reCaptcha if enabled
*
* @param Event $event event
* @return void
*/
public function beforeLayout(Event $event)
{
if (Configure::read('Users.Registration.reCaptcha')) {
$this->addReCaptchaScript();
}
}
/**
* Social login link
*
* @param string $name name
* @param array $options options
* @return string
*/
public function socialLogin($name, $options = [])
{
if (empty($options['label'])) {
$options['label'] = 'Sign in with';
}
return $this->Html->link($this->Html->tag('i', '', [
'class' => __d('Users', 'fa fa-{0}', strtolower($name)),
]) . __d('Users', '{0} {1}', Hash::get($options, 'label'), Inflector::camelize($name)), "/auth/$name", [
'escape' => false, 'class' => __d('Users', 'btn btn-social btn-{0} ' . Hash::get($options, 'class') ? :'', strtolower($name))
]);
}
/**
* 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->Html->link(empty($message) ? __d('Users', 'Logout') : $message, [
'plugin' => 'CakeDC/Users', 'controller' => 'Users', 'action' => 'logout'
], $options);
}
/**
* Generate a link if the target url is authorized for the logged in user
*
* @param type $title link's title.
* @param type $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 = [])
{
if ($this->isAuthorized($url)) {
$linkOptions = $options;
unset($linkOptions['before'], $linkOptions['after']);
return Hash::get($options, 'before') . $this->Html->link($title, $url, $linkOptions) . Hash::get($options, 'after');
}
return false;
}
/**
* Retunrs true if the target url is authorized for the logged in user
*
* @param type $url url that the user is making request.
* @return bool
*/
public function isAuthorized($url = null)
{
$event = new Event(UsersAuthComponent::EVENT_IS_AUTHORIZED, $this, ['url' => $url]);
$result = $this->_View->eventManager()->dispatch($event);
return $result->result;
}
/**
* 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('Users', 'Welcome, {0}', $this->Html->link($this->request->session()->read('Auth.User.first_name'), $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.Registration.reCaptcha')) {
return false;
}
if (!Configure::read('reCaptcha.key')) {
return $this->Html->tag('p', __d('Users', 'reCaptcha is not configured! Please configure reCaptcha.key or set Users.Registration.reCaptcha to false'));
}
$this->Form->unlockField('g-recaptcha-response');
return $this->Html->tag('div', '', [
'class' => 'g-recaptcha',
'data-sitekey' => Configure::read('reCaptcha.key')
]);
}
}