Completed
Push — master ( 9a7125...f9bacd )
by Cheren
12:29 queued 10:39
created

UsersController::profile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 0
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 Core\Event\EventManager;
19
use Community\Model\Entity\User;
20
use Community\Model\Table\UsersTable;
21
use Cake\Network\Exception\BadRequestException;
22
23
/**
24
 * Class UsersController
25
 *
26
 * @package     Community\Controller
27
 * @property    UsersTable $Users
28
 */
29
class UsersController extends AppController
30
{
31
32
    /**
33
     * Default profile page action.
34
     *
35
     * @return void
36
     */
37
    public function profile()
38
    {
39
        $userId = $this->Auth->user('id');
40
        if (!$userId) {
41
            $this->Flash->error(__d('community', 'You are not logged in'));
42
            $this->redirect($this->Auth->getConfig('loginAction'));
43
        }
44
45
        $this
46
            ->set('user', $this->Users->get($userId, ['contain' => 'Groups']))
47
            ->set('page_title', __d('community', 'Edit profile'));
48
    }
49
50
    /**
51
     * Activation user profile action.
52
     *
53
     * @param   null|int $id            User id.
54
     * @param   null|string $token      User activation token.
55
     * @return  \Cake\Http\Response|null
56
     */
57
    public function activate($id = null, $token = null)
58
    {
59
        $user = $this->_getUser($id, $token);
60
61
        if ($user === null) {
62
            throw new BadRequestException(__d('community', 'User was not found'));
63
        }
64
65
        if ($user->status) {
66
            $this->Flash->error(__d(
67
                'community',
68
                '«{0}», you have already activated your profile before.',
69
                sprintf('<strong>%s</strong>', $user->name)
70
            ));
71
        } else {
72
            $user
73
                ->set('token', null)
74
                ->set('status', true);
75
76
            $result = $this->Users->save($user);
0 ignored issues
show
Bug introduced by
It seems like $user defined by $this->_getUser($id, $token) on line 59 can also be of type array; however, Community\Model\Table\UsersTable::save() does only seem to accept object<Cake\Datasource\EntityInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
77
            if ($result) {
78
                EventManager::trigger('Controller.Users.successActivate', $this, ['user' => $result]);
79
                $this->Flash->success(__d(
80
                    'community',
81
                    '«{0}», you profile has been activate successfully.',
82
                    sprintf('<strong>%s</strong>', $user->name)
83
                ));
84
            } else {
85
                $this->Flash->error(__d(
86
                    'community',
87
                    'An error has occurred. Please, try again.',
88
                    sprintf('<strong>%s</strong>', $user->name)
89
                ));
90
            }
91
        }
92
93
        return $this->redirect(['action' => 'login']);
94
    }
95
96
    /**
97
     * Login action.
98
     *
99
     * @return  \Cake\Http\Response|null
100
     */
101 View Code Duplication
    public function login()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
102
    {
103
        if ($this->request->is('post')) {
104
            $user = $this->Auth->identify();
105
            if ($user !== false) {
106
                $this->Auth->setUser($user);
0 ignored issues
show
Bug introduced by
It seems like $user defined by $this->Auth->identify() on line 104 can also be of type boolean; however, Cake\Controller\Component\AuthComponent::setUser() does only seem to accept array|object<ArrayAccess>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
107
                return $this->redirect($this->Auth->redirectUrl());
108
            }
109
110
            $this->Flash->error(__d('community', 'Login or password is incorrect'));
111
        }
112
113
        $this->set('page_title', __d('community', 'Authorize profile'));
114
    }
115
116
    /**
117
     * Logout action.
118
     *
119
     * @return  \Cake\Http\Response|null
120
     */
121
    public function logout()
122
    {
123
        return $this->redirect($this->Auth->logout());
124
    }
125
126
    /**
127
     * Setup password action.
128
     *
129
     * @param   null|int $id        User id.
130
     * @param   null|string $token  User activation token.
131
     * @return  \Cake\Http\Response|null
132
     */
133
    public function setupPassword($id = null, $token = null)
134
    {
135
        $user = $this->_getUser($id, $token);
136
137
        if ($user === null) {
138
            throw new BadRequestException(__d('community', 'User was not found'));
139
        }
140
141
        if ($this->request->is(['patch', 'post', 'put'])) {
142
            $entity = $this->Users->patchEntity($user, $this->request->getData());
0 ignored issues
show
Bug introduced by
It seems like $user defined by $this->_getUser($id, $token) on line 135 can also be of type array; however, Cake\ORM\Table::patchEntity() does only seem to accept object<Cake\Datasource\EntityInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
143
            if ($user->status) {
144
                $entity->set('token', null);
145
            }
146
147
            /** @var User $result */
148
            $result = $this->Users->save($user);
0 ignored issues
show
Bug introduced by
It seems like $user defined by $this->_getUser($id, $token) on line 135 can also be of type array; however, Community\Model\Table\UsersTable::save() does only seem to accept object<Cake\Datasource\EntityInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
149
            if ($result) {
150
                $this->Flash->success(__d(
151
                    'community',
152
                    '«{0}», You have successfully changed your password.',
153
                    sprintf('<strong>%s</strong>', $user->get('name'))
154
                ));
155
156
                if (!$user->status && $result->token !== null) {
157
                    return $this->redirect([
158
                        'action' => 'activate',
159
                        $user->id,
160
                        $user->token
161
                    ]);
162
                }
163
164
                return $this->redirect(['action' => 'login']);
165
            } else {
166
                $this->Flash->error(__d('community', 'An error has occurred. Please, try again.'));
167
            }
168
        }
169
170
        $this
171
            ->set('user', $user)
172
            ->set('page_title', __d('community', 'Setup new password'));
173
    }
174
175
    /**
176
     * Get user by id and activation token.
177
     *
178
     * @param   int $id         User id.
179
     * @param   string $token   User activation token.
180
     * @return  array|User|null
181
     */
182
    private function _getUser($id, $token)
183
    {
184
        return $this->Users->find()
185
            ->where([
186
                'id'    => $id,
187
                'token' => $token
188
            ])
189
            ->first();
190
    }
191
}
192