|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +/** |
| 5 | + * Copyright 2010 - 2019, Cake Development Corporation (https://www.cakedc.com) |
| 6 | + * |
| 7 | + * Licensed under The MIT License |
| 8 | + * Redistributions of files must retain the above copyright notice. |
| 9 | + * |
| 10 | + * @copyright Copyright 2010 - 2018, Cake Development Corporation (https://www.cakedc.com) |
| 11 | + * @license MIT License (http://www.opensource.org/licenses/mit-license.php) |
| 12 | + */ |
| 13 | + |
| 14 | +namespace CakeDC\Users\Controller\Traits; |
| 15 | + |
| 16 | +use Cake\Datasource\Exception\RecordNotFoundException; |
| 17 | +use CakeDC\Users\Utility\UsersUrl; |
| 18 | + |
| 19 | +/** |
| 20 | + * Covers the login, logout and social login |
| 21 | + * |
| 22 | + * @property \Cake\Http\ServerRequest $request |
| 23 | + */ |
| 24 | +trait OneTimeTokenTrait |
| 25 | +{ |
| 26 | + /** |
| 27 | + * Request a single token login link. |
| 28 | + * |
| 29 | + * @return \Cake\Http\Response|null |
| 30 | + */ |
| 31 | + public function requestLoginLink() |
| 32 | + { |
| 33 | + if ($this->getRequest()->is('post')) { |
| 34 | + $email = $this->getRequest()->getData('email'); |
| 35 | + try { |
| 36 | + /** @var \CakeDC\Users\Model\Table\UsersTable $Users */ |
| 37 | + $Users = $this->getUsersTable(); |
| 38 | + /** @uses \CakeDC\Users\Model\Behavior\OneTimeLoginLinkBehavior::sendLoginLink() */ |
| 39 | + $Users->sendLoginLink($email); |
| 40 | + } catch (RecordNotFoundException $e) { |
| 41 | + $this->log( |
| 42 | + sprintf('A user is trying to get a login link for the email %s but it does not exist.', $email) |
| 43 | + ); |
| 44 | + } |
| 45 | + $msg = __d( |
| 46 | + 'cake_d_c/users', |
| 47 | + 'If your user is registered in the system you will receive an email ' . |
| 48 | + 'with a link so you can access your user area.' |
| 49 | + ); |
| 50 | + $this->Flash->success($msg); |
| 51 | + $this->setRequest($this->getRequest()->withoutData('email')); |
| 52 | + |
| 53 | + return $this->redirect(UsersUrl::actionUrl('login')); |
| 54 | + } |
| 55 | + |
| 56 | + return null; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Single token login. |
| 61 | + * |
| 62 | + * @return \Cake\Http\Response|null |
| 63 | + */ |
| 64 | + public function singleTokenLogin() |
| 65 | + { |
| 66 | + $errorMessage = null; |
| 67 | + $token = null; |
| 68 | + if ($this->getRequest()->is('get')) { |
| 69 | + $token = $this->getRequest()->getQuery('token'); |
| 70 | + } |
| 71 | + |
| 72 | + if ($this->getRequest()->is('post') || $token) { |
| 73 | + $user = $this->Authentication->getIdentity(); |
| 74 | + $token = $this->getRequest()->getData('token', $token); |
| 75 | + if (is_array($token)) { |
| 76 | + $token = join($token); |
| 77 | + } |
| 78 | + if (!$user && !empty($token)) { |
| 79 | + $errorMessage = __d('cake_d_c/users', 'Invalid or expired token. Please request a new one.'); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + if ($errorMessage) { |
| 84 | + $this->Flash->error($errorMessage); |
| 85 | + |
| 86 | + return $this->redirect(UsersUrl::actionUrl('login')); |
| 87 | + } |
| 88 | + |
| 89 | + return $this->redirect('/'); |
| 90 | + } |
| 91 | +} |
0 commit comments