/
login.php
443 lines (388 loc) · 15.4 KB
/
login.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
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
<?php
namespace Concrete\Controller\SinglePage;
use Concrete\Core\Application\UserInterface\Dashboard\Navigation\NavigationCache;
use Concrete\Core\Authentication\AuthenticationType;
use Concrete\Core\Authentication\AuthenticationTypeFailureException;
use Concrete\Core\Http\ResponseFactoryInterface;
use Concrete\Core\Localization\Localization;
use Concrete\Core\Logging\Channels;
use Concrete\Core\Logging\LoggerAwareInterface;
use Concrete\Core\Logging\LoggerAwareTrait;
use Concrete\Core\Routing\RedirectResponse;
use Concrete\Core\User\PostLoginLocation;
use Exception;
use PageController;
use Concrete\Core\User\User;
use UserAttributeKey;
use UserInfo;
class Login extends PageController implements LoggerAwareInterface
{
use LoggerAwareTrait;
public function getLoggerChannel()
{
return Channels::CHANNEL_SECURITY;
}
public $helpers = ['form'];
protected $locales = [];
public function on_before_render()
{
if ($this->error->has()) {
$this->set('error', $this->error);
}
}
/* automagically run by the controller once we're done with the current method */
/* method is passed to this method, the method that we were just finished running */
public function account_deactivated()
{
$config = $this->app->make('config');
$this->error->add(t($config->get('concrete.user.deactivation.message')));
}
public function session_invalidated()
{
$this->error->add(t('Your session has expired. Please sign in again.'));
}
/**
* @param string $type
* @param string $method
* @param null $a
* @param null $b
* @param null $c
* @param null $d
* @param null $e
* @param null $f
* @param null $g
* @param null $h
* @param null $i
* @param null $j
*
* @throws \Concrete\Core\Authentication\AuthenticationTypeFailureException
* @throws \Exception
*/
public function callback($type = null, $method = 'callback', $a = null, $b = null, $c = null, $d = null, $e = null, $f = null, $g = null, $h = null, $i = null, $j = null)
{
if (!$type) {
return $this->view();
}
$at = AuthenticationType::getByHandle($type);
if (!$at || !$at->isEnabled()) {
throw new AuthenticationTypeFailureException(t('Invalid authentication type.'));
}
$this->set('authType', $at);
if (!method_exists($at->controller, $method)) {
return $this->view();
}
if ($method != 'callback') {
if (!is_array($at->controller->apiMethods) || !in_array($method, $at->controller->apiMethods)) {
return $this->view();
}
}
try {
$params = func_get_args();
array_shift($params);
array_shift($params);
$this->view();
$this->set('authTypeParams', $params);
$this->set('authTypeElement', $method);
} catch (Exception $e) {
if ($e instanceof AuthenticationTypeFailureException) {
// Throw again if this is a big`n
throw $e;
}
$this->error->add($e->getMessage());
}
}
/**
* Authenticate the user using a specific authentication type.
*
* @param $type AuthenticationType handle
*/
public function authenticate($type = '')
{
$valt = $this->app->make('token');
if (!$valt->validate('login_' . $type)) {
$this->error->add($valt->getErrorMessage());
} else {
try {
$at = AuthenticationType::getByHandle($type);
if (!$at->isEnabled()) {
throw new AuthenticationTypeFailureException(t('Invalid authentication type.'));
}
$user = $at->controller->authenticate();
if ($user && $user->isRegistered()) {
return $this->finishAuthentication($at, $user);
}
} catch (Exception $e) {
$this->error->add($e->getMessage());
}
}
if (isset($at)) {
$this->set('lastAuthType', $at);
}
$this->view();
}
/**
* @param AuthenticationType $type Required
*
* @throws Exception
*/
public function finishAuthentication(
AuthenticationType $type,
User $u
) {
if (!$type || !($type instanceof AuthenticationType)) {
return $this->view();
}
$config = $this->app->make('config');
if ($config->get('concrete.i18n.choose_language_login')) {
$userLocale = $this->post('USER_LOCALE');
if (is_string($userLocale) && ($userLocale !== '')) {
if ($userLocale !== Localization::BASE_LOCALE) {
$availableLocales = Localization::getAvailableInterfaceLanguages();
if (!in_array($userLocale, $availableLocales)) {
$userLocale = '';
}
}
if ($userLocale !== '') {
if (Localization::activeLocale() !== $userLocale) {
Localization::changeLocale($userLocale);
}
$u->setUserDefaultLanguage($userLocale);
}
}
}
$ui = UserInfo::getByID($u->getUserID());
$aks = UserAttributeKey::getRegistrationList();
$unfilled = array_values(
array_filter(
$aks,
function ($ak) use ($ui) {
return $ak->isAttributeKeyRequiredOnRegister() && !is_object($ui->getAttributeValueObject($ak));
}));
if (count($unfilled)) {
$u->logout(false);
if (!$this->error) {
$this->on_start();
}
$this->set('required_attributes', $unfilled);
$this->set('u', $u);
$session = $this->app->make('session');
$session->set('uRequiredAttributeUser', $u->getUserID());
$session->set('uRequiredAttributeUserAuthenticationType', $type->getAuthenticationTypeHandle());
$this->view();
return $this->getViewObject()->render();
}
$u->setLastAuthType($type);
$ue = new \Concrete\Core\User\Event\User($u);
$this->app->make('director')->dispatch('on_user_login', $ue);
/** @var NavigationCache $navigationCache */
$navigationCache = $this->app->make(NavigationCache::class);
$navigationCache->clear();
return new RedirectResponse(
$this->app->make('url/manager')->resolve(['/login', 'login_complete'])
);
}
public function login_complete()
{
// Move this functionality to a redirected endpoint rather than from within the previous method because
// session isn't set until we redirect and reload.
$u = $this->app->make(User::class);
if (!$this->error) {
$this->error = $this->app->make('helper/validation/error');
}
if ($u->isRegistered()) {
$pll = $this->app->make(PostLoginLocation::class);
$response = $pll->getPostLoginRedirectResponse(true);
// Expire the site cache in the logged in user's browser to avoid
// their full page cache serving local cached versions of the pages
// as they are now logged in and should probably see extra elements
// on the pages.
//
// Unfortunately this does not work in all browsers for insecure
// origins by default and you may see an error in the browser
// console. To get it to work, see the following e.g. in Chrome:
// chrome://flags/#unsafely-treat-insecure-origin-as-secure
// ----
// Update (from andrew): This slows down sites significantly on login. Sometimes it takes 20-30 seconds
// to login, which is unacceptable. If browsers figure out how to do this asynchronously at some point
// we can look to re-enable this.
// $response->headers->set('Clear-Site-Data', '"cache"');
return $response;
} else {
$session = $this->app->make('session');
$this->logger->notice(
t('Session made it to login_complete but was not attached to an authenticated session.'),
['session' => $session->getId(), 'ip_address' => $_SERVER['REMOTE_ADDR']]
);
$this->error->add(t('User is not registered. Check your authentication controller.'));
$u->logout();
}
}
public function on_start()
{
$config = $this->app->make('config');
$this->error = $this->app->make('helper/validation/error');
$this->set('valt', $this->app->make('helper/validation/token'));
$txt = $this->app->make('helper/text');
if (isset($_GET['uName']) && strlen($_GET['uName'])
) { // pre-populate the username if supplied, if its an email address with special characters the email needs to be urlencoded first,
$this->set('uName', trim($txt->email($_GET['uName'])));
}
$loc = Localization::getInstance();
$loc->pushActiveContext(Localization::CONTEXT_SITE);
if ($config->get('concrete.user.registration.email_registration')) {
$this->set('uNameLabel', t('Email Address'));
} else {
$this->set('uNameLabel', t('Username'));
}
$languages = [];
$locales = [];
if ($config->get('concrete.i18n.choose_language_login')) {
$languages = Localization::getAvailableInterfaceLanguages();
if (count($languages) > 0) {
array_unshift($languages, Localization::BASE_LOCALE);
}
$locales = [];
foreach ($languages as $lang) {
$locales[$lang] = \Punic\Language::getName($lang, $lang);
}
asort($locales);
$locales = array_merge(['' => tc('Default locale', '** Default')], $locales);
}
$loc->popActiveContext();
$this->locales = $locales;
$this->set('locales', $locales);
}
/**
* @deprecated Use the getPostLoginUrl method of \Concrete\Core\User\PostLoginLocation
* @see \Concrete\Core\User\PostLoginLocation::getPostLoginUrl()
*
* @return string
*/
public function getRedirectUrl()
{
$pll = $this->app->make(PostLoginLocation::class);
$url = $pll->getPostLoginUrl(true);
return $url;
}
/**
* @deprecated Use the getSessionPostLoginUrl method of \Concrete\Core\User\PostLoginLocation
* @see \Concrete\Core\User\PostLoginLocation::getSessionPostLoginUrl()
*
* @return string|false
*/
public function getRedirectUrlFromSession()
{
$pll = $this->app->make(PostLoginLocation::class);
$url = $pll->getSessionPostLoginUrl(true);
return $url === '' ? false : $url;
}
public function view($type = null, $element = 'form')
{
$this->set('authTypeParams', $this->getSets());
$user = $this->app->make(User::class);
$this->set('user', $user);
if (strlen($type)) {
try {
$at = AuthenticationType::getByHandle($type);
if ($at->isEnabled()) {
$this->set('authType', $at);
$this->set('authTypeElement', $element);
}
} catch (\Exception $e) {
// Don't fail loudly
}
}
}
public function fill_attributes()
{
try {
$session = $this->app->make('session');
if (!$session->has('uRequiredAttributeUser') ||
intval($session->get('uRequiredAttributeUser')) < 1 ||
!$session->has('uRequiredAttributeUserAuthenticationType') ||
!$session->get('uRequiredAttributeUserAuthenticationType')
) {
$session->remove('uRequiredAttributeUser');
$session->remove('uRequiredAttributeUserAuthenticationType');
throw new Exception(t('Invalid Request, please attempt login again.'));
}
User::loginByUserID($session->get('uRequiredAttributeUser'));
$session->remove('uRequiredAttributeUser');
$u = $this->app->make(User::class);
$at = AuthenticationType::getByHandle($session->get('uRequiredAttributeUserAuthenticationType'));
$session->remove('uRequiredAttributeUserAuthenticationType');
if (!$at || !$at->isEnabled()) {
throw new Exception(t('Invalid Authentication Type'));
}
$ui = UserInfo::getByID($u->getUserID());
$aks = UserAttributeKey::getRegistrationList();
$unfilled = array_values(
array_filter(
$aks,
function ($ak) use ($ui) {
return $ak->isAttributeKeyRequiredOnRegister() && !is_object($ui->getAttributeValueObject($ak));
}));
$saveAttributes = [];
foreach ($unfilled as $attribute) {
$controller = $attribute->getController();
$validator = $controller->getValidator();
$response = $validator->validateSaveValueRequest($controller, $this->request);
/* @var \Concrete\Core\Validation\ResponseInterface $response */
if ($response->isValid()) {
$saveAttributes[] = $attribute;
} else {
$error = $response->getErrorObject();
$this->error->add($error);
}
}
if (count($saveAttributes) > 0) {
$ui->saveUserAttributesForm($saveAttributes);
}
return $this->finishAuthentication($at, $u);
} catch (Exception $e) {
$this->error->add($e->getMessage());
}
}
/**
* @deprecated
*/
public function logout($token = false)
{
if ($this->app->make('token')->validate('logout', $token)) {
$u = $this->app->make(User::class);
$u->logout();
$this->redirect('/');
}
}
/**
* @param $token
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function do_logout($token = false)
{
$factory = $this->app->make(ResponseFactoryInterface::class);
/* @var ResponseFactoryInterface $factory */
$valt = $this->app->make('token');
/* @var \Concrete\Core\Validation\CSRF\Token $valt */
if ($valt->validate('do_logout', $token)) {
// Resolve the current logged in user and log them out
$this->app->make(User::class)->logout();
// Determine the destination URL
$url = $this->app->make('url/manager')->resolve(['/']);
// Return a new redirect to the homepage.
return $factory->redirect((string) $url, 302);
}
return $factory->error($valt->getErrorMessage());
}
public function forward($cID = 0)
{
$nh = $this->app->make('helper/validation/numbers');
if ($nh->integer($cID, 1)) {
$rcID = (int) $cID;
$this->set('rcID', $rcID);
$pll = $this->app->make(PostLoginLocation::class);
$pll->setSessionPostLoginUrl($rcID);
}
}
}