UsersController::logout()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
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\Admin;
17
18
use Community\Token;
19
use JBZoo\Utils\Filter;
20
use Community\Model\Entity\User;
21
use Community\Model\Table\UsersTable;
22
use Community\Controller\Component\AuthComponent;
23
use Cake\ORM\Exception\RolledbackTransactionException;
24
use Cake\Datasource\Exception\RecordNotFoundException;
25
use Cake\Datasource\Exception\InvalidPrimaryKeyException;
26
27
/**
28
 * Class UsersController
29
 *
30
 * @package     Community\Controller\Admin
31
 *
32
 * @property    AuthComponent $Auth
33
 * @property    UsersTable $Users
34
 */
35
class UsersController extends AppController
36
{
37
38
    /**
39
     * Add action.
40
     *
41
     * @return  \Cake\Http\Response|null
42
     *
43
     * @throws  \Aura\Intl\Exception
44
     * @throws  \Cake\ORM\Exception\RolledbackTransactionException
45
     */
46
    public function add()
47
    {
48
        $user = $this->Users->newEntity();
49
        if ($this->request->is('post')) {
50
            $user = $this->Users->patchEntity($user, $this->request->getData());
0 ignored issues
show
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...
51
            if (Filter::bool($user->get('notify'))) {
52
                $user->set('token', Token::generate());
53
            }
54
55
            $result = $this->Users->save($user);
56
            if ($result) {
57
                $this->Flash->success(__d('community', 'The user {0} has been saved.', sprintf(
58
                    '<strong>«%s»</strong>',
59
                    $result->get('login')
60
                )));
61
62
                return $this->App->redirect(['apply' => ['action' => 'edit', $result->id]]);
0 ignored issues
show
Bug introduced by
Accessing id on the interface Cake\Datasource\EntityInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
63
            } else {
64
                $this->Flash->error(__d('community', 'User could not be updated. Please, try again.'));
65
            }
66
        }
67
68
        $groups = $this->Users->Groups->getTreeList();
69
70
        $this
71
            ->set(compact('user', 'groups'))
72
            ->set('page_title', __d('community', 'Profiles: Add new user'));
73
    }
74
75
    /**
76
     * Change user password action.
77
     *
78
     * @param   null|int $id User id.
79
     * @return  \Cake\Http\Response|null
80
     *
81
     * @throws  \Aura\Intl\Exception
82
     */
83
    public function changePassword($id = null)
84
    {
85
        $user = $this->Users->get($id, ['contain' => []]);
86
        $user->set('password', null);
87
88 View Code Duplication
        if ($this->request->is(['patch', 'post', 'put'])) {
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...
89
            $user = $this->Users->patchEntity($user, $this->request->getData());
0 ignored issues
show
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...
90
            if ($result = $this->Users->save($user)) {
91
                $this->Flash->success(__d('community', 'Password has been updated.'));
92
                return $this->App->redirect(['apply' => ['action' => 'edit', $result->id]]);
0 ignored issues
show
Bug introduced by
Accessing id on the interface Cake\Datasource\EntityInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
93
            } else {
94
                $this->Flash->error(__d('community', 'Password could not be updated. Please, try again.'));
95
            }
96
        }
97
98
        $this
99
            ->set(compact('user'))
100
            ->set('page_title', __d('community', 'Profiles: {0} - change password', $user->name));
0 ignored issues
show
Bug introduced by
Accessing name on the interface Cake\Datasource\EntityInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
101
    }
102
103
    /**
104
     * Edit action.
105
     *
106
     * @param   null|int $id User id.
107
     * @return  \Cake\Http\Response|null
108
     *
109
     * @throws  \Aura\Intl\Exception
110
     * @throws  RecordNotFoundException
111
     * @throws  InvalidPrimaryKeyException
112
     * @throws  RolledbackTransactionException
113
     */
114
    public function edit($id = null)
115
    {
116
        /** @var User $user */
117
        $user = $this->Users->get($id);
118
        $groups = $this->Users->Groups->getTreeList();
119
120 View Code Duplication
        if ($this->request->is(['patch', 'post', 'put'])) {
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...
121
            $user = $this->Users->patchEntity($user, $this->request->getData());
0 ignored issues
show
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...
122
            if ($result = $this->Users->save($user)) {
123
                $this->Flash->success(__d('community', 'The user {0} has been saved.', sprintf(
124
                    '<strong>«%s»</strong>',
125
                    $result->get('login')
126
                )));
127
                return $this->App->redirect([
128
                    'apply' => ['action' => 'edit', $result->id]
0 ignored issues
show
Bug introduced by
Accessing id on the interface Cake\Datasource\EntityInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
129
                ]);
130
            } else {
131
                $this->Flash->error(__d('community', 'User could not be updated. Please, try again.'));
132
            }
133
        }
134
135
        $this
136
            ->set(compact('user', 'groups'))
137
            ->set('page_title', __d('community', 'Profiles: Edit user - {0}', $user->name));
0 ignored issues
show
Bug introduced by
Accessing name on the interface Cake\Datasource\EntityInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
138
    }
139
140
    /**
141
     * Index action.
142
     *
143
     * @return  void
144
     *
145
     * @throws  \Aura\Intl\Exception
146
     * @throws  \RuntimeException
147
     */
148
    public function index()
149
    {
150
        $query = $this->Users
151
            ->find('search', $this->Users->filterParams($this->request->getQueryParams()))
152
            ->contain('Groups');
153
154
        $this
155
            ->set('users', $this->paginate($query))
0 ignored issues
show
Bug introduced by
It seems like $query defined by $this->Users->find('sear...()))->contain('Groups') on line 150 can also be of type array; however, Cake\Controller\Controller::paginate() does only seem to accept object<Cake\ORM\Table>|s...ct<Cake\ORM\Query>|null, 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...
156
            ->set('page_title', __d('community', 'Profiles: User list'));
157
    }
158
159
    /**
160
     * Initialization hook method. Implement this method to avoid having to overwrite the constructor and call parent.
161
     *
162
     * @return  void
163
     *
164
     * @throws  \JBZoo\Utils\Exception
165
     * @throws  \Cake\Core\Exception\Exception
166
     */
167
    public function initialize()
168
    {
169
        parent::initialize();
170
        $this->Security->setConfig('unlockedFields', ['password', 'password_confirm']);
171
    }
172
173
    /**
174
     * Login action.
175
     *
176
     * @return  \Cake\Http\Response|null
177
     *
178
     * @throws  \Aura\Intl\Exception
179
     */
180 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...
181
    {
182
        $this->viewBuilder()->setLayout('login');
183
        if ($this->request->is('post')) {
184
            $user = $this->Auth->identify();
185
            if ($user !== false) {
186
                $this->Auth->setUser($user);
0 ignored issues
show
Bug introduced by
It seems like $user defined by $this->Auth->identify() on line 184 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...
187
                return $this->redirect($this->Auth->redirectUrl());
188
            }
189
190
            $this->Flash->error(__d('community', 'Login or password is incorrect'));
191
        }
192
193
        $this->set('page_title', __d('community', 'Authorize profile'));
194
    }
195
196
    /**
197
     * Logout action.
198
     *
199
     * @return  \Cake\Http\Response|null
200
     */
201
    public function logout()
202
    {
203
        return $this->redirect($this->Auth->logout());
204
    }
205
206
    /**
207
     * Process action.
208
     *
209
     * @return  \Cake\Http\Response|null
210
     *
211
     * @throws  \Aura\Intl\Exception
212
     */
213
    public function process()
214
    {
215
        list($action, $ids) = $this->Process->getRequestVars($this->name);
216
        return $this->Process->make($this->Users, $action, $ids);
0 ignored issues
show
Bug introduced by
It seems like $action defined by $this->Process->getRequestVars($this->name) on line 215 can also be of type array or null; however, Core\Controller\Component\ProcessComponent::make() does only seem to accept string, 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...
217
    }
218
}
219