1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Canvas\Api\Controllers; |
6
|
|
|
|
7
|
|
|
use Canvas\Models\Users; |
8
|
|
|
use Canvas\Models\Companies; |
9
|
|
|
use Phalcon\Http\Response; |
10
|
|
|
use Phalcon\Validation; |
11
|
|
|
use Phalcon\Validation\Validator\PresenceOf; |
|
|
|
|
12
|
|
|
use Canvas\Exception\BadRequestHttpException; |
13
|
|
|
use Canvas\Exception\ModelException; |
14
|
|
|
use Canvas\Exception\NotFoundHttpException; |
15
|
|
|
use Canvas\Models\AccessList; |
16
|
|
|
use Canvas\Exception\ServerErrorHttpException; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class UsersController. |
20
|
|
|
* |
21
|
|
|
* @package Canvas\Api\Controllers |
22
|
|
|
* |
23
|
|
|
* @property Users $userData |
24
|
|
|
* @property Request $request |
25
|
|
|
* @property Config $config |
26
|
|
|
* @property Apps $app |
27
|
|
|
*/ |
28
|
|
|
class UsersController extends \Baka\Auth\UsersController |
29
|
|
|
{ |
30
|
|
|
/* |
31
|
|
|
* fields we accept to create |
32
|
|
|
* |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
protected $createFields = ['name', 'firstname', 'lastname', 'displayname', 'language', 'country_id', 'timezone', 'email', 'password', 'created_at', 'updated_at', 'default_company', 'default_company_branch', 'family', 'cell_phone_number', 'country_id']; |
36
|
|
|
|
37
|
|
|
/* |
38
|
|
|
* fields we accept to create |
39
|
|
|
* |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
protected $updateFields = ['name', 'firstname', 'lastname', 'displayname', 'language', 'country_id', 'timezone', 'email', 'password', 'created_at', 'updated_at', 'default_company', 'default_company_branch', 'cell_phone_number', 'country_id']; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* set objects. |
46
|
|
|
* |
47
|
|
|
* @return void |
48
|
|
|
*/ |
49
|
|
|
public function onConstruct() |
50
|
|
|
{ |
51
|
|
|
$this->model = new Users(); |
52
|
|
|
|
53
|
|
|
//if you are not a admin you cant see all the users |
54
|
|
|
if (!$this->userData->hasRole('Defaults.Admins')) { |
|
|
|
|
55
|
|
|
$this->additionalSearchFields = [ |
56
|
|
|
['id', ':', $this->userData->getId()], |
57
|
|
|
]; |
58
|
|
|
} else { |
59
|
|
|
//admin get all the users for this company |
60
|
|
|
$this->additionalSearchFields = [ |
61
|
|
|
['id', ':', implode('|', $this->userData->currentCompany->getAssociatedUsersByApp())], |
62
|
|
|
]; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Get Uer. |
68
|
|
|
* |
69
|
|
|
* @param mixed $id |
70
|
|
|
* |
71
|
|
|
* @method GET |
72
|
|
|
* @url /v1/users/{id} |
73
|
|
|
* |
74
|
|
|
* @return Response |
75
|
|
|
*/ |
76
|
|
|
public function getById($id) : Response |
77
|
|
|
{ |
78
|
|
|
//none admin users can only edit themselves |
79
|
|
|
if (!$this->userData->hasRole('Default.Admins') || (int) $id === 0) { |
|
|
|
|
80
|
|
|
$id = $this->userData->getId(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @todo filter only by usres from this app / company |
85
|
|
|
*/ |
86
|
|
|
$user = $this->model->findFirstOrFail([ |
87
|
|
|
'id = ?0 AND is_deleted = 0', |
88
|
|
|
'bind' => [$id], |
89
|
|
|
]); |
90
|
|
|
|
91
|
|
|
//get the results and append its relationships |
92
|
|
|
$user = $this->appendRelationshipsToResult($this->request, $user); |
93
|
|
|
|
94
|
|
|
//if you search for roles we give you the access for this app |
95
|
|
|
if (array_key_exists('roles', $user)) { |
96
|
|
|
$accesList = AccessList::find([ |
97
|
|
|
'conditions' => 'roles_name = ?0 and apps_id = ?1 and allowed = 0', |
98
|
|
|
'bind' => [$user['roles'][0]->name, $this->config->app->id] |
99
|
|
|
]); |
100
|
|
|
|
101
|
|
|
if (count($accesList) > 0) { |
102
|
|
|
foreach ($accesList as $access) { |
103
|
|
|
$user['access_list'][strtolower($access->resources_name)][$access->access_name] = 0; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $this->response($this->processOutput($user)); |
|
|
|
|
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Update a User Info. |
113
|
|
|
* |
114
|
|
|
* @method PUT |
115
|
|
|
* @url /v1/users/{id} |
116
|
|
|
* |
117
|
|
|
* @return Response |
118
|
|
|
*/ |
119
|
|
|
public function edit($id) : Response |
120
|
|
|
{ |
121
|
|
|
//none admin users can only edit themselves |
122
|
|
|
if (!$this->userData->hasRole('Default.Admins')) { |
|
|
|
|
123
|
|
|
$id = $this->userData->getId(); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
$user = $this->model->findFirstOrFail($id); |
127
|
|
|
$request = $this->request->getPutData(); |
128
|
|
|
|
129
|
|
|
if (empty($request)) { |
130
|
|
|
throw new BadRequestHttpException(_('No data to update this account with ')); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
//update password |
134
|
|
|
if (array_key_exists('new_password', $request) && (!empty($request['new_password']) && !empty($request['current_password']))) { |
135
|
|
|
//Ok let validate user password |
136
|
|
|
$validation = new Validation(); |
137
|
|
|
$validation->add('new_password', new PresenceOf(['message' => 'The new_password is required.'])); |
138
|
|
|
$validation->add('current_password', new PresenceOf(['message' => 'The current_password is required.'])); |
139
|
|
|
$validation->add('confirm_new_password', new PresenceOf(['message' => 'The confirm_new_password is required.'])); |
140
|
|
|
$messages = $validation->validate($request); |
141
|
|
|
|
142
|
|
|
if (count($messages)) { |
143
|
|
|
foreach ($messages as $message) { |
144
|
|
|
throw new BadRequestHttpException((string)$message); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
$user->updatePassword($request['current_password'], $request['new_password'], $request['confirm_new_password']); |
149
|
|
|
} else { |
150
|
|
|
//remove on any actino that doesnt involve password |
151
|
|
|
unset($request['password']); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
//change my default company |
155
|
|
|
if (array_key_exists('default_company', $request)) { |
156
|
|
|
if ($company = Companies::findFirst($request['default_company'])) { |
157
|
|
|
if ($company->userAssociatedToCompany($this->userData)) { |
158
|
|
|
$user->default_company = $company->getId(); |
159
|
|
|
unset($request['default_company']); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
//update |
165
|
|
|
$user->updateOrFail($request, $this->updateFields); |
166
|
|
|
return $this->response($this->processOutput($user)); |
|
|
|
|
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Given the results we will proess the output |
171
|
|
|
* we will check if a DTO transformer exist and if so we will send it over to change it. |
172
|
|
|
* |
173
|
|
|
* @param object|array $results |
174
|
|
|
* @return void |
175
|
|
|
*/ |
176
|
|
|
protected function processOutput($results) |
177
|
|
|
{ |
178
|
|
|
/** |
179
|
|
|
* remove password. |
180
|
|
|
* @todo move to DTO |
181
|
|
|
*/ |
182
|
|
|
if (is_object($results)) { |
183
|
|
|
$results->password = null; |
184
|
|
|
$results->bypassRoutes = null; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
return $results; |
|
|
|
|
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Add users notifications. |
192
|
|
|
* |
193
|
|
|
* @param int $id |
194
|
|
|
* @method PUT |
195
|
|
|
* @return Response |
196
|
|
|
*/ |
197
|
|
|
public function updateNotifications(int $id) : Response |
198
|
|
|
{ |
199
|
|
|
//get the notification array |
200
|
|
|
//delete the current ones |
201
|
|
|
//iterate and save into users |
202
|
|
|
|
203
|
|
|
return $this->response(['OK' => $id]); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Delete a Record. |
208
|
|
|
* |
209
|
|
|
* @throws Exception |
210
|
|
|
* @return Response |
211
|
|
|
*/ |
212
|
|
|
public function delete($id): Response |
213
|
|
|
{ |
214
|
|
|
if ((int) $this->userData->getId() === (int) $id) { |
215
|
|
|
throw new ServerErrorHttpException('Cant delete your own user . If you want to close your account contact support or go to app settings'); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
return parent::delete($id); |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths