UsersController::profile()   A
last analyzed

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
22
/**
23
 * Class UsersController
24
 *
25
 * @package     Community\Controller
26
 * @property    UsersTable $Users
27
 */
28
class UsersController extends AppController
29
{
30
31
    /**
32
     * Default profile page action.
33
     *
34
     * @return  void
35
     *
36
     * @throws  \Aura\Intl\Exception
37
     */
38
    public function profile()
39
    {
40
        $userId = $this->Auth->user('id');
41
        if (!$userId) {
42
            $this->Flash->error(__d('community', 'You are not logged in'));
43
            $this->redirect($this->Auth->getConfig('loginAction'));
44
        }
45
46
        $this
47
            ->set('user', $this->Users->get($userId, ['contain' => 'Groups']))
48
            ->set('page_title', __d('community', 'Edit profile'));
49
    }
50
51
    /**
52
     * Activation user profile action.
53
     *
54
     * @param   null|int $id            User id.
55
     * @param   null|string $token      User activation token.
56
     * @return  \Cake\Http\Response|null
57
     *
58
     * @throws  \Aura\Intl\Exception
59
     */
60
    public function activate($id = null, $token = null)
61
    {
62
        $user = $this->_getUser($id, $token);
63
64 View Code Duplication
        if ($user === null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
65
            $this->Flash->error(__d('community', 'User was not found'));
66
            return $this->redirect(['action' => 'login']);
67
        }
68
69
        if ($user->status) {
70
            $this->Flash->error(__d(
71
                'community',
72
                '«{0}», you have already activated your profile before.',
73
                sprintf('<strong>%s</strong>', $user->name)
74
            ));
75
        } else {
76
            $user
77
                ->set('token', null)
78
                ->set('status', true);
79
80
            $result = $this->Users->save($user);
0 ignored issues
show
Bug introduced by
It seems like $user defined by $this->_getUser($id, $token) on line 62 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...
81
            if ($result) {
82
                EventManager::trigger('Controller.Users.successActivate', $this, ['user' => $result]);
83
                $this->Flash->success(__d(
84
                    'community',
85
                    '«{0}», you profile has been activate successfully.',
86
                    sprintf('<strong>%s</strong>', $user->name)
87
                ));
88
            } else {
89
                $this->Flash->error(__d(
90
                    'community',
91
                    'An error has occurred. Please, try again.',
92
                    sprintf('<strong>%s</strong>', $user->name)
93
                ));
94
            }
95
        }
96
97
        return $this->redirect(['action' => 'login']);
98
    }
99
100
    /**
101
     * Login action.
102
     *
103
     * @return  \Cake\Http\Response|null
104
     *
105
     * @throws  \Aura\Intl\Exception
106
     */
107 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...
108
    {
109
        if ($this->request->is('post')) {
110
            $user = $this->Auth->identify();
111
            if ($user !== false) {
112
                $this->Auth->setUser($user);
0 ignored issues
show
Bug introduced by
It seems like $user defined by $this->Auth->identify() on line 110 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...
113
                return $this->redirect($this->Auth->redirectUrl());
114
            }
115
116
            $this->Flash->error(__d('community', 'Login or password is incorrect'));
117
        }
118
119
        $this->set('page_title', __d('community', 'Authorize profile'));
120
    }
121
122
    /**
123
     * Logout action.
124
     *
125
     * @return  \Cake\Http\Response|null
126
     */
127
    public function logout()
128
    {
129
        return $this->redirect($this->Auth->logout());
130
    }
131
132
    /**
133
     * Setup password action.
134
     *
135
     * @param   null|int $id        User id.
136
     * @param   null|string $token  User activation token.
137
     * @return  \Cake\Http\Response|null
138
     *
139
     * @throws  \Aura\Intl\Exception
140
     */
141
    public function setupPassword($id = null, $token = null)
142
    {
143
        $user = $this->_getUser($id, $token);
144
145 View Code Duplication
        if ($user === null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
146
            $this->Flash->error(__d('community', 'User was not found'));
147
            return $this->redirect(['action' => 'login']);
148
        }
149
150
        if ($this->request->is(['patch', 'post', 'put'])) {
151
            $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 143 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...
Bug introduced by
It seems like $this->request->getData() targeting Cake\Http\ServerRequest::getData() can also be of type null or string; however, Cake\ORM\Table::patchEntity() does only seem to accept array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

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