Test Failed
Pull Request — master (#160)
by Maximo
05:37
created

UsersController   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 231
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 5
Bugs 1 Features 0
Metric Value
eloc 93
c 5
b 1
f 0
dl 0
loc 231
ccs 0
cts 94
cp 0
rs 10
wmc 24

6 Methods

Rating   Name   Duplication   Size   Complexity  
A onConstruct() 0 13 2
B getById() 0 40 7
A delete() 0 7 2
A updateNotifications() 0 7 1
A processOutput() 0 12 2
B edit() 0 48 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Canvas\Api\Controllers;
6
7
use Canvas\Models\Users;
8
use Phalcon\Http\Response;
9
use Phalcon\Validation;
10
use Phalcon\Validation\Validator\PresenceOf;
0 ignored issues
show
Bug introduced by
The type Phalcon\Validation\Validator\PresenceOf was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Canvas\Exception\BadRequestHttpException;
12
use Canvas\Models\AccessList;
13
use Canvas\Exception\ServerErrorHttpException;
14
use \Baka\Auth\UsersController as BakaUsersController;
15
16
/**
17
 * Class UsersController.
18
 *
19
 * @package Canvas\Api\Controllers
20
 *
21
 * @property Users $userData
22
 * @property Request $request
23
 * @property Config $config
24
 * @property Apps $app
25
 */
26
class UsersController extends BakaUsersController
27
{
28
    /*
29
     * fields we accept to create
30
     *
31
     * @var array
32
     */
33
    protected $createFields = [
34
        'name',
35
        'firstname',
36
        'lastname',
37
        'displayname',
38
        'language',
39
        'country_id',
40
        'timezone',
41
        'email',
42
        'password',
43
        'created_at',
44
        'updated_at',
45
        'default_company',
46
        'default_company_branch',
47
        'family',
48
        'cell_phone_number',
49
        'country_id'
50
    ];
51
52
    /*
53
     * fields we accept to create
54
     *
55
     * @var array
56
     */
57
    protected $updateFields = [
58
        'name',
59
        'firstname',
60
        'lastname',
61
        'displayname',
62
        'language',
63
        'country_id',
64
        'timezone',
65
        'email',
66
        'password',
67
        'created_at',
68
        'updated_at',
69
        'default_company',
70
        'default_company_branch',
71
        'cell_phone_number',
72
        'country_id'
73
    ];
74
75
    /**
76
     * set objects.
77
     *
78
     * @return void
79
     */
80
    public function onConstruct()
81
    {
82
        $this->model = new Users();
83
84
        //if you are not a admin you cant see all the users
85
        if (!$this->userData->hasRole('Defaults.Admins')) {
0 ignored issues
show
Unused Code introduced by
The call to Canvas\Models\Users::hasRole() has too many arguments starting with 'Defaults.Admins'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

85
        if (!$this->userData->/** @scrutinizer ignore-call */ hasRole('Defaults.Admins')) {

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
86
            $this->additionalSearchFields = [
87
                ['id', ':', $this->userData->getId()],
88
            ];
89
        } else {
90
            //admin get all the users for this company
91
            $this->additionalSearchFields = [
92
                ['id', ':', implode('|', $this->userData->getDefaultCompany()->getAssociatedUsersByApp())],
93
            ];
94
        }
95
    }
96
97
    /**
98
     * Get Uer.
99
     *
100
     * @param mixed $id
101
     *
102
     * @method GET
103
     * @url /v1/users/{id}
104
     *
105
     * @return Response
106
     */
107
    public function getById($id) : Response
108
    {
109
        //none admin users can only edit themselves
110
        if (!$this->userData->hasRole('Default.Admins') || (int) $id === 0) {
0 ignored issues
show
Unused Code introduced by
The call to Canvas\Models\Users::hasRole() has too many arguments starting with 'Default.Admins'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

110
        if (!$this->userData->/** @scrutinizer ignore-call */ hasRole('Default.Admins') || (int) $id === 0) {

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
111
            $id = $this->userData->getId();
112
        }
113
114
        /**
115
         * @todo filter only by user from this app / company
116
         */
117
        $user = $this->model->findFirstOrFail([
118
            'id = ?0 AND is_deleted = 0',
119
            'bind' => [$id],
120
        ]);
121
        $userObject = $user;
122
123
        //get the results and append its relationships
124
        $user = $this->appendRelationshipsToResult($this->request, $user);
125
126
        //if you search for roles we give you the access for this app
127
        //@todo move this to DTO
128
        if (array_key_exists('roles', $user)) {
129
            if (!isset($user['roles'][0])) {
130
                throw new ServerErrorHttpException('User with no Role , please contact system admin');
131
            }
132
            $accesList = AccessList::find([
133
                'conditions' => 'roles_name = ?0 and apps_id = ?1 and allowed = 0',
134
                'bind' => [$user['roles'][0]->name, $this->app->getId()]
135
            ]);
136
137
            if (count($accesList) > 0) {
138
                foreach ($accesList as $access) {
139
                    $user['access_list'][strtolower($access->resources_name)][$access->access_name] = 0;
140
                }
141
            }
142
143
            $user['default_company'] = $userObject->getDefaultCompany()->getId();
144
        }
145
146
        return $this->response($this->processOutput($user));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->processOutput($user) targeting Canvas\Api\Controllers\U...roller::processOutput() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
147
    }
148
149
    /**
150
     * Update a User Info.
151
     *
152
     * @method PUT
153
     * @url /v1/users/{id}
154
     *
155
     * @return Response
156
     */
157
    public function edit($id) : Response
158
    {
159
        //none admin users can only edit themselves
160
        if (!$this->userData->hasRole('Default.Admins')) {
0 ignored issues
show
Unused Code introduced by
The call to Canvas\Models\Users::hasRole() has too many arguments starting with 'Default.Admins'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

160
        if (!$this->userData->/** @scrutinizer ignore-call */ hasRole('Default.Admins')) {

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
161
            $id = $this->userData->getId();
162
        }
163
164
        $user = $this->model->findFirstOrFail($id);
165
        $request = $this->request->getPutData();
166
167
        if (empty($request)) {
168
            throw new BadRequestHttpException(_('No data to update this account with '));
169
        }
170
171
        //update password
172
        if (array_key_exists('new_password', $request) && (!empty($request['new_password']) && !empty($request['current_password']))) {
173
            //Ok let validate user password
174
            $validation = new Validation();
175
            $validation->add('new_password', new PresenceOf(['message' => 'The new_password is required.']));
176
            $validation->add('current_password', new PresenceOf(['message' => 'The current_password is required.']));
177
            $validation->add('confirm_new_password', new PresenceOf(['message' => 'The confirm_new_password is required.']));
178
            $messages = $validation->validate($request);
179
180
            if (count($messages)) {
181
                foreach ($messages as $message) {
182
                    throw new BadRequestHttpException((string)$message);
183
                }
184
            }
185
186
            $user->updatePassword($request['current_password'], $request['new_password'], $request['confirm_new_password']);
187
        } else {
188
            //remove on any actino that doesnt involve password
189
            unset($request['password']);
190
        }
191
192
        //change my default company , the #teamfrontend is sending us the branchid instead of the company id
193
        //on this value so we use is as the branch
194
        if (array_key_exists('default_company', $request) && !array_key_exists('default_company_branch', $request)) {
195
            $user->switchDefaultCompanyByBranch((int) $request['default_company']);
196
            unset($request['default_company'], $request['default_company_branch']);
197
        } else {
198
            $user->switchDefaultCompanyByBranch((int) $request['default_company_branch']);
199
            unset($request['default_company'], $request['default_company_branch']);
200
        }
201
202
        //update
203
        $user->updateOrFail($request, $this->updateFields);
204
        return $this->response($this->processOutput($user));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->processOutput($user) targeting Canvas\Api\Controllers\U...roller::processOutput() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
205
    }
206
207
    /**
208
     * Given the results we will proess the output
209
     * we will check if a DTO transformer exist and if so we will send it over to change it.
210
     *
211
     * @param object|array $results
212
     * @return void
213
     */
214
    protected function processOutput($results)
215
    {
216
        /**
217
         * remove password.
218
         * @todo move to DTO
219
         */
220
        if (is_object($results)) {
221
            $results->password = null;
222
            $results->bypassRoutes = null;
223
        }
224
225
        return $results;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $results returns the type array|object which is incompatible with the documented return type void.
Loading history...
226
    }
227
228
    /**
229
     * Add users notifications.
230
     *
231
     * @param int $id
232
     * @method PUT
233
     * @return Response
234
     */
235
    public function updateNotifications(int $id) : Response
236
    {
237
        //get the notification array
238
        //delete the current ones
239
        //iterate and save into users
240
241
        return $this->response(['OK' => $id]);
242
    }
243
244
    /**
245
     * Delete a Record.
246
     *
247
     * @throws Exception
248
     * @return Response
249
     */
250
    public function delete($id): Response
251
    {
252
        if ((int) $this->userData->getId() === (int) $id) {
253
            throw new ServerErrorHttpException('Cant delete your own user . If you want to close your account contact support or go to app settings');
254
        }
255
256
        return parent::delete($id);
257
    }
258
}
259