Completed
Push — master ( 8207ea...7c1383 )
by Cheren
01:44
created

UsersController::changePassword()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 13

Duplication

Lines 9
Ratio 47.37 %

Importance

Changes 0
Metric Value
dl 9
loc 19
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 13
nc 3
nop 1
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 Core\Event\EventManager;
20
use Community\Model\Entity\User;
21
use Community\Model\Table\UsersTable;
22
use Cake\ORM\Exception\RolledbackTransactionException;
23
use Cake\Datasource\Exception\RecordNotFoundException;
24
use Cake\Datasource\Exception\InvalidPrimaryKeyException;
25
26
/**
27
 * Class UsersController
28
 *
29
 * @package Community\Controller\Admin
30
 * @property UsersTable $Users
31
 */
32
class UsersController extends AppController
33
{
34
35
    /**
36
     * Add action.
37
     *
38
     * @return \Cake\Http\Response|null
39
     *
40
     * @throws \Cake\ORM\Exception\RolledbackTransactionException
41
     */
42
    public function add()
43
    {
44
        $user = $this->Users->newEntity();
45
        if ($this->request->is('post')) {
46
            $user = $this->Users->patchEntity($user, $this->request->getData());
47
48
            if ($user->get('notify') === '1') {
49
                $user->set('token', Token::generate());
50
            }
51
52
            EventManager::trigger('Admin.Controller.User.Add.BeforeSave', $this, [
53
                'user' => $user
54
            ]);
55
56
            if ($result = $this->Users->save($user)) {
57
                EventManager::trigger('Admin.Controller.User.Add.AfterSave', $this, [
58
                    'user' => $result
59
                ]);
60
61
                $this->Flash->success(__d('community', 'The user {0} has been saved.', sprintf(
62
                    '<strong>«%s»</strong>',
63
                    $result->get('login')
64
                )));
65
66
                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...
67
            } else {
68
                $this->Flash->error(__d('community', 'User could not be updated. Please, try again.'));
69
            }
70
        }
71
72
        $groups = $this->Users->Groups->getTreeList();
73
74
        $this
75
            ->set(compact('user', 'groups'))
76
            ->set('page_title', __d('community', 'Profiles: Add new user'));
77
    }
78
79
    /**
80
     * Change user password action.
81
     *
82
     * @param null|int $id
83
     * @return \Cake\Http\Response|null
84
     */
85
    public function changePassword($id = null)
86
    {
87
        $user = $this->Users->get($id, ['contain' => []]);
88
        $user->set('password', null);
89
90 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...
91
            $user = $this->Users->patchEntity($user, $this->request->getData());
92
            if ($result = $this->Users->save($user)) {
93
                $this->Flash->success(__d('community', 'Password has been updated.'));
94
                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...
95
            } else {
96
                $this->Flash->error(__d('community', 'Password could not be updated. Please, try again.'));
97
            }
98
        }
99
100
        $this
101
            ->set(compact('user'))
102
            ->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...
103
    }
104
105
    /**
106
     * Edit action.
107
     *
108
     * @param null|int $id
109
     * @return \Cake\Http\Response|null
110
     *
111
     * @throws RecordNotFoundException
112
     * @throws InvalidPrimaryKeyException
113
     * @throws RolledbackTransactionException
114
     */
115
    public function edit($id = null)
116
    {
117
        /** @var User $user */
118
        $user = $this->Users->get($id);
119
        $groups = $this->Users->Groups->getTreeList();
120
121 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...
122
            $group = $this->Users->patchEntity($user, $this->request->getData());
123
            if ($result = $this->Users->save($group)) {
124
                $this->Flash->success(__d('community', 'The user {0} has been saved.', sprintf(
125
                    '<strong>«%s»</strong>',
126
                    $result->get('login')
127
                )));
128
                return $this->App->redirect([
129
                    '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...
130
                ]);
131
            } else {
132
                $this->Flash->error(__d('community', 'User could not be updated. Please, try again.'));
133
            }
134
        }
135
136
        $this
137
            ->set(compact('user', 'groups'))
138
            ->set('page_title', __d('community', 'Profiles: Edit user - {0}', $user->name));
139
    }
140
141
    /**
142
     * Index action.
143
     *
144
     * @return void
145
     *
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.
161
     *
162
     * Implement this method to avoid having to overwrite
163
     * the constructor and call parent.
164
     *
165
     * @return void
166
     *
167
     * @throws \Cake\Core\Exception\Exception
168
     */
169
    public function initialize()
170
    {
171
        parent::initialize();
172
        $this->Security->setConfig('unlockedFields', ['password', 'password_confirm']);
173
    }
174
175
    /**
176
     * Process action.
177
     *
178
     * @return \Cake\Http\Response|null
179
     */
180
    public function process()
181
    {
182
        list($action, $ids) = $this->Process->getRequestVars($this->name);
183
        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 182 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...
184
    }
185
}
186