/
controller.php
207 lines (169 loc) · 5.67 KB
/
controller.php
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
<?php
namespace Concrete\Authentication\Twitter;
defined('C5_EXECUTE') or die('Access Denied');
use Concrete\Core\Authentication\Type\OAuth\OAuth1a\GenericOauth1aTypeController;
use Concrete\Core\Authentication\Type\Twitter\Factory\TwitterServiceFactory;
use Concrete\Core\Validation\CSRF\Token;
use OAuth\Common\Exception\Exception;
use OAuth\OAuth1\Service\Twitter;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
class Controller extends GenericOauth1aTypeController
{
public $apiMethods = array('handle_error', 'handle_success', 'handle_register');
/**
* @var string
*/
protected $email;
protected $username;
protected $firstName;
protected $lastName;
public function registrationGroupID()
{
return \Config::get('auth.twitter.registration.group');
}
/**
* Twitter doesn't give us the users email.
*
* @return bool
*/
public function supportsRegistration()
{
return \Config::get('auth.twitter.registration.enabled', false);
}
/**
* Twitter doesn't give us the users email.
*
* @return bool
*/
public function supportsEmailResolution()
{
return false;
}
public function getAuthenticationTypeIconHTML()
{
return '<i class="fa fa-twitter"></i>';
}
public function getHandle()
{
return 'twitter';
}
/**
* @return Twitter
*/
public function getService()
{
if (!$this->service) {
/** @var TwitterServiceFactory $factory */
$factory = $this->app->make(TwitterServiceFactory::class);
$this->service = $factory->createService();
}
return $this->service;
}
public function saveAuthenticationType($args)
{
\Config::save('auth.twitter.appid', $args['apikey']);
\Config::save('auth.twitter.secret', $args['apisecret']);
\Config::save('auth.twitter.registration.enabled', (bool) $args['registration_enabled']);
\Config::save('auth.twitter.registration.group', intval($args['registration_group'], 10));
}
public function edit()
{
$this->set('form', \Loader::helper('form'));
$this->set('apikey', \Config::get('auth.twitter.appid', ''));
$this->set('apisecret', \Config::get('auth.twitter.secret', ''));
$list = new \GroupList();
$list->includeAllGroups();
$this->set('groups', $list->getResults());
}
/**
* We override this method because twitter doesn't give us the email, we have to have the user input it before we can create a user.
*
* @return null|\User
*
* @throws Exception
*/
protected function attemptAuthentication()
{
$extractor = $this->getExtractor();
$user_id = $this->getBoundUserID($extractor->getUniqueId());
if ($user_id && $user_id > 0) {
$user = \User::loginByUserID($user_id);
if ($user && !$user->isError()) {
return $user;
}
}
if ($extractor->supportsEmail() && $user = \UserInfo::getByEmail($extractor->getEmail())) {
if ($user && !$user->isError()) {
throw new Exception('A user account already exists for this email, please log in and attach from your account page.');
}
}
if ($this->supportsRegistration()) {
/** @var FlashBagInterface $flashbag */
$flashbag = \Session::getFlashBag();
$flashbag->set('firstname', parent::getFirstName());
$flashbag->set('lastname', parent::getLastName());
$flashbag->set('username', parent::getUsername());
$flashbag->set('token', $this->getToken());
$response = \Redirect::to('/login/callback/twitter/handle_register/', id(new Token())->generate('twitter_register'));
$response->send();
exit;
}
return null;
}
public function handle_register($token = null)
{
/** @var FlashBagInterface $flashbag */
$flashbag = \Session::getFlashBag();
$this->firstName = array_shift($flashbag->peek('firstname'));
$this->lastName = array_shift($flashbag->peek('lastName'));
$this->username = array_shift($flashbag->peek('username'));
$this->token = array_shift($flashbag->peek('token'));
$token_helper = new Token();
if (!$token_helper->validate('twitter_register', $token) && !$token_helper->validate('twitter_register') ||
!$this->token) {
$this->redirect('/login/');
exit;
}
if (\Request::request('uEmail', false)) {
$this->email = \Request::request('uEmail');
$user = $this->createUser();
if ($user && !$user->isError()) {
return $this->completeAuthentication($user);
}
}
$this->set('username', $this->username);
$this->set('show_email', true);
}
public function supportsEmail()
{
return true;
}
public function getEmail()
{
if (!$this->email) {
$this->email = parent::getEmail();
}
return $this->email;
}
public function getFirstName()
{
if (!$this->firstName) {
$this->firstName = parent::getFirstname();
}
return $this->firstName;
}
public function getLastName()
{
if (!$this->lastName) {
$this->lastName = parent::getLastName();
}
return $this->lastName;
}
public function getUsername()
{
if (!$this->username) {
$this->username = parent::getUsername();
}
return $this->username;
}
}