Completed
Push — master ( 8fd89c...76aee5 )
by Cheren
01:58
created

UsersController::setupPassword()   C

Complexity

Conditions 7
Paths 8

Size

Total Lines 47
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 47
rs 6.7272
c 1
b 0
f 0
cc 7
eloc 29
nc 8
nop 2
1
<?php
2
/**
3
 * CakeCMS Community
4
 *
5
 * This file is part of the of the simple cms based on CakePHP 3.
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 *
9
 * @package     Community
10
 * @license     MIT
11
 * @copyright   MIT License http://www.opensource.org/licenses/mit-license.php
12
 * @link        https://github.com/CakeCMS/Community".
13
 * @author      Sergey Kalistratov <[email protected]>
14
 */
15
16
namespace Community\Controller;
17
18
use Community\Model\Entity\User;
19
use Community\Model\Table\UsersTable;
20
use Cake\Network\Exception\BadRequestException;
21
22
/**
23
 * Class UsersController
24
 *
25
 * @package     Community\Controller
26
 * @property    UsersTable $Users
27
 */
28
class UsersController extends AppController
29
{
30
31
    /**
32
     * Setup password action.
33
     *
34
     * @param   null|int $id
35
     * @param   null|string $token
36
     * @return  \Cake\Http\Response|null
37
     */
38
    public function setupPassword($id = null, $token = null)
39
    {
40
        /** @var User $user */
41
        $user = $this->Users->find()
42
            ->where([
43
                'id'    => $id,
44
                'token' => $token
45
            ])
46
            ->first();
47
48
        if ($user === null) {
49
            throw new BadRequestException(__d('community', 'Invalid user id or token'));
50
        }
51
52
        if ($this->request->is(['patch', 'post', 'put'])) {
53
            $entity = $this->Users->patchEntity($user, $this->request->getData());
54
            if ($user->status) {
55
                $entity->set('token', null);
56
            }
57
58
            /** @var User $result */
59
            $result = $this->Users->save($user);
60
            if ($result) {
61
                $this->Flash->success(__d(
62
                    'community',
63
                    '«{0}», You have successfully changed your password.',
64
                    sprintf('<strong>%s</strong>', $user->get('name'))
65
                ));
66
67
                if (!$user->status && $result->token !== null) {
68
                    return $this->redirect([
69
                        'id'     => $user->id,
70
                        'action' => 'activate',
71
                        'token'  => $user->token
72
                    ]);
73
                }
74
75
                return $this->redirect(['action' => 'login']);
76
            } else {
77
                $this->Flash->error(__d('community', 'An error has occurred. Please, try again.'));
78
            }
79
        }
80
81
        $this
82
            ->set('user', $user)
83
            ->set('page_title', __d('community', 'Setup new password'));
84
    }
85
}
86