Completed
Push — master ( a3d311...211d79 )
by Cheren
02:09
created

UsersController::initialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
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  \Cake\ORM\Exception\RolledbackTransactionException
44
     */
45
    public function add()
46
    {
47
        $user = $this->Users->newEntity();
48
        if ($this->request->is('post')) {
49
            $user = $this->Users->patchEntity($user, $this->request->getData());
50
            if (Filter::bool($user->get('notify'))) {
51
                $user->set('token', Token::generate());
52
            }
53
54
            $result = $this->Users->save($user);
55
            if ($result) {
56
                $this->Flash->success(__d('community', 'The user {0} has been saved.', sprintf(
57
                    '<strong>«%s»</strong>',
58
                    $result->get('login')
59
                )));
60
61
                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...
62
            } else {
63
                $this->Flash->error(__d('community', 'User could not be updated. Please, try again.'));
64
            }
65
        }
66
67
        $groups = $this->Users->Groups->getTreeList();
68
69
        $this
70
            ->set(compact('user', 'groups'))
71
            ->set('page_title', __d('community', 'Profiles: Add new user'));
72
    }
73
74
    /**
75
     * Change user password action.
76
     *
77
     * @param   null|int $id User id.
78
     * @return  \Cake\Http\Response|null
79
     */
80
    public function changePassword($id = null)
81
    {
82
        $user = $this->Users->get($id, ['contain' => []]);
83
        $user->set('password', null);
84
85 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...
86
            $user = $this->Users->patchEntity($user, $this->request->getData());
87
            if ($result = $this->Users->save($user)) {
88
                $this->Flash->success(__d('community', 'Password has been updated.'));
89
                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...
90
            } else {
91
                $this->Flash->error(__d('community', 'Password could not be updated. Please, try again.'));
92
            }
93
        }
94
95
        $this
96
            ->set(compact('user'))
97
            ->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...
98
    }
99
100
    /**
101
     * Edit action.
102
     *
103
     * @param   null|int $id User id.
104
     * @return  \Cake\Http\Response|null
105
     *
106
     * @throws  RecordNotFoundException
107
     * @throws  InvalidPrimaryKeyException
108
     * @throws  RolledbackTransactionException
109
     */
110
    public function edit($id = null)
111
    {
112
        /** @var User $user */
113
        $user = $this->Users->get($id);
114
        $groups = $this->Users->Groups->getTreeList();
115
116 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...
117
            $user = $this->Users->patchEntity($user, $this->request->getData());
118
            if ($result = $this->Users->save($user)) {
119
                $this->Flash->success(__d('community', 'The user {0} has been saved.', sprintf(
120
                    '<strong>«%s»</strong>',
121
                    $result->get('login')
122
                )));
123
                return $this->App->redirect([
124
                    '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...
125
                ]);
126
            } else {
127
                $this->Flash->error(__d('community', 'User could not be updated. Please, try again.'));
128
            }
129
        }
130
131
        $this
132
            ->set(compact('user', 'groups'))
133
            ->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...
134
    }
135
136
    /**
137
     * Index action.
138
     *
139
     * @return  void
140
     *
141
     * @throws  \RuntimeException
142
     */
143
    public function index()
144
    {
145
        $query = $this->Users
146
            ->find('search', $this->Users->filterParams($this->request->getQueryParams()))
147
            ->contain('Groups');
148
149
        $this
150
            ->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 145 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...
151
            ->set('page_title', __d('community', 'Profiles: User list'));
152
    }
153
154
    /**
155
     * Initialization hook method. Implement this method to avoid having to overwrite the constructor and call parent.
156
     *
157
     * @return  void
158
     *
159
     * @throws  \Cake\Core\Exception\Exception
160
     */
161
    public function initialize()
162
    {
163
        parent::initialize();
164
        $this->Security->setConfig('unlockedFields', ['password', 'password_confirm']);
165
    }
166
167
    /**
168
     * Login action.
169
     *
170
     * @return  \Cake\Http\Response|null
171
     */
172
    public function login()
173
    {
174
        $this->viewBuilder()->setLayout('login');
175
        if ($this->request->is('post')) {
176
            $user = $this->Auth->identify();
177
            if ($user !== false) {
178
                $this->Auth->setUser($user);
0 ignored issues
show
Bug introduced by
It seems like $user defined by $this->Auth->identify() on line 176 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...
179
                return $this->redirect($this->Auth->redirectUrl());
180
            }
181
182
            $this->Flash->error(__d('community', 'Login or password is incorrect'));
183
        }
184
185
        $this->set('page_title', __d('community', 'Authorize profile'));
186
    }
187
188
    /**
189
     * Logout action.
190
     *
191
     * @return  \Cake\Http\Response|null
192
     */
193
    public function logout()
194
    {
195
        return $this->redirect($this->Auth->logout());
196
    }
197
198
    /**
199
     * Process action.
200
     *
201
     * @return  \Cake\Http\Response|null
202
     */
203
    public function process()
204
    {
205
        list($action, $ids) = $this->Process->getRequestVars($this->name);
206
        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 205 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...
207
    }
208
}
209