1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Canvas\Api\Controllers; |
6
|
|
|
|
7
|
|
|
use Baka\Auth\UsersController as BakaUsersController; |
|
|
|
|
8
|
|
|
use Canvas\Contracts\Controllers\ProcessOutputMapperTrait; |
9
|
|
|
use Canvas\Dto\User as UserDto; |
10
|
|
|
use Canvas\Http\Exception\InternalServerErrorException; |
11
|
|
|
use Canvas\Mapper\UserMapper; |
12
|
|
|
use Canvas\Models\Users; |
13
|
|
|
use Canvas\Models\UsersAssociatedApps; |
14
|
|
|
use Canvas\Validation as CanvasValidation; |
15
|
|
|
use Phalcon\Http\Response; |
|
|
|
|
16
|
|
|
use Phalcon\Validation\Validator\PresenceOf; |
|
|
|
|
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 BakaUsersController |
29
|
|
|
{ |
30
|
|
|
use ProcessOutputMapperTrait; |
31
|
|
|
/* |
32
|
|
|
* fields we accept to create |
33
|
|
|
* |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
protected $createFields = [ |
37
|
|
|
'name', |
38
|
|
|
'firstname', |
39
|
|
|
'lastname', |
40
|
|
|
'description', |
41
|
|
|
'displayname', |
42
|
|
|
'language', |
43
|
|
|
'country_id', |
44
|
|
|
'timezone', |
45
|
|
|
'email', |
46
|
|
|
'password', |
47
|
|
|
'roles_id', |
48
|
|
|
'created_at', |
49
|
|
|
'updated_at', |
50
|
|
|
'default_company', |
51
|
|
|
'default_company_branch', |
52
|
|
|
'family', |
53
|
|
|
'cell_phone_number', |
54
|
|
|
'country_id', |
55
|
|
|
'location' |
56
|
|
|
]; |
57
|
|
|
|
58
|
|
|
/* |
59
|
|
|
* fields we accept to create |
60
|
|
|
* |
61
|
|
|
* @var array |
62
|
|
|
*/ |
63
|
|
|
protected $updateFields = [ |
64
|
|
|
'name', |
65
|
|
|
'firstname', |
66
|
|
|
'lastname', |
67
|
|
|
'description', |
68
|
|
|
'displayname', |
69
|
|
|
'language', |
70
|
|
|
'country_id', |
71
|
|
|
'timezone', |
72
|
|
|
'email', |
73
|
|
|
'password', |
74
|
|
|
'roles_id', |
75
|
|
|
'created_at', |
76
|
|
|
'updated_at', |
77
|
|
|
'default_company', |
78
|
|
|
'default_company_branch', |
79
|
|
|
'cell_phone_number', |
80
|
|
|
'country_id', |
81
|
|
|
'location', |
82
|
|
|
'user_active' |
83
|
|
|
]; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* set objects. |
87
|
|
|
* |
88
|
|
|
* @return void |
89
|
|
|
*/ |
90
|
|
|
public function onConstruct() |
91
|
|
|
{ |
92
|
|
|
$this->model = new Users(); |
|
|
|
|
93
|
|
|
$this->dto = UserDto::class; |
94
|
|
|
$this->dtoMapper = new UserMapper(); |
95
|
|
|
|
96
|
|
|
//if you are not a admin you cant see all the users |
97
|
|
|
if (!$this->userData->hasRole('Defaults.Admins')) { |
98
|
|
|
$this->additionalSearchFields = [ |
|
|
|
|
99
|
|
|
['id', ':', $this->userData->getId()], |
100
|
|
|
]; |
101
|
|
|
} else { |
102
|
|
|
//admin get all the users for this company |
103
|
|
|
$this->additionalSearchFields = [ |
104
|
|
|
['id', ':', implode('|', $this->userData->getDefaultCompany()->getAssociatedUsersByApp())], |
105
|
|
|
]; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Get Uer. |
111
|
|
|
* |
112
|
|
|
* @param mixed $id |
113
|
|
|
* |
114
|
|
|
* @method GET |
115
|
|
|
* @url /v1/users/{id} |
116
|
|
|
* |
117
|
|
|
* @return Response |
118
|
|
|
*/ |
119
|
|
|
public function getById($id) : Response |
120
|
|
|
{ |
121
|
|
|
//none admin users can only edit themselves |
122
|
|
|
if (!$this->userData->hasRole('Default.Admins') || (int) $id === 0) { |
123
|
|
|
$id = $this->userData->getId(); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
$this->userData->can('SettingsMenu.company-settings'); |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @todo filter only by user from this app / company |
130
|
|
|
*/ |
131
|
|
|
$user = $this->model->findFirstOrFail([ |
132
|
|
|
'id = ?0 AND is_deleted = 0', |
133
|
|
|
'bind' => [$id], |
134
|
|
|
]); |
135
|
|
|
|
136
|
|
|
//get the results and append its relationships |
137
|
|
|
$user = $this->appendRelationshipsToResult($this->request, $user); |
138
|
|
|
|
139
|
|
|
return $this->response($this->processOutput($user)); |
|
|
|
|
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Update a User Info. |
144
|
|
|
* |
145
|
|
|
* @method PUT |
146
|
|
|
* @url /v1/users/{id} |
147
|
|
|
* |
148
|
|
|
* @return Response |
149
|
|
|
*/ |
150
|
|
|
public function edit($id) : Response |
151
|
|
|
{ |
152
|
|
|
//none admin users can only edit themselves |
153
|
|
|
if (!$this->userData->hasRole('Default.Admins')) { |
154
|
|
|
$id = $this->userData->getId(); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @todo admin users should only be able to update user from their app level |
159
|
|
|
*/ |
160
|
|
|
$user = $this->model->findFirstOrFail($id); |
161
|
|
|
$request = $this->request->getPutData(); |
162
|
|
|
|
163
|
|
|
if (empty($request)) { |
164
|
|
|
throw new InternalServerErrorException(_('No data to update this account with ')); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
//update password |
168
|
|
|
if (isset($request['new_password']) && (!empty($request['new_password']) && !empty($request['current_password']))) { |
169
|
|
|
//Ok let validate user password |
170
|
|
|
$validation = new CanvasValidation(); |
171
|
|
|
$validation->add('new_password', new PresenceOf(['message' => 'The new_password is required.'])); |
172
|
|
|
$validation->add('current_password', new PresenceOf(['message' => 'The current_password is required.'])); |
173
|
|
|
$validation->add('confirm_new_password', new PresenceOf(['message' => 'The confirm_new_password is required.'])); |
174
|
|
|
$validation->validate($request); |
175
|
|
|
|
176
|
|
|
$user->updatePassword($request['current_password'], $request['new_password'], $request['confirm_new_password']); |
177
|
|
|
} else { |
178
|
|
|
//remove on any actinon that doesn't involve password |
179
|
|
|
unset($request['password']); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
//change my default company , the #teamfrontend is sending us the branch's instead of the company id |
183
|
|
|
//on this value so we use is as the branch |
184
|
|
|
if (isset($request['default_company']) && !isset($request['default_company_branch'])) { |
185
|
|
|
$user->switchDefaultCompanyByBranch((int) $request['default_company']); |
186
|
|
|
unset($request['default_company'], $request['default_company_branch']); |
187
|
|
|
} elseif (isset($request['default_company_branch'])) { |
188
|
|
|
$user->switchDefaultCompanyByBranch((int) $request['default_company_branch']); |
189
|
|
|
unset($request['default_company'], $request['default_company_branch']); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
//update |
193
|
|
|
$user->updateOrFail($request, $this->updateFields); |
194
|
|
|
return $this->response($this->processOutput($user)); |
|
|
|
|
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Add users notifications. |
199
|
|
|
* |
200
|
|
|
* @param int $id |
201
|
|
|
* |
202
|
|
|
* @method PUT |
203
|
|
|
* |
204
|
|
|
* @return Response |
205
|
|
|
*/ |
206
|
|
|
public function updateNotifications(int $id) : Response |
207
|
|
|
{ |
208
|
|
|
//get the notification array |
209
|
|
|
//delete the current ones |
210
|
|
|
//iterate and save into users |
211
|
|
|
|
212
|
|
|
return $this->response(['OK' => $id]); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Delete a Record. |
217
|
|
|
* |
218
|
|
|
* @throws Exception |
219
|
|
|
* |
220
|
|
|
* @return Response |
221
|
|
|
*/ |
222
|
|
|
public function delete($id) : Response |
223
|
|
|
{ |
224
|
|
|
if ((int) $this->userData->getId() === (int) $id) { |
225
|
|
|
throw new InternalServerErrorException( |
226
|
|
|
'Cant delete your own user . If you want to close your account contact support or go to app settings' |
227
|
|
|
); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
return parent::delete($id); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Change User's active status for in current app. |
235
|
|
|
* |
236
|
|
|
* @param int $id |
237
|
|
|
* @param int $appsId |
238
|
|
|
* |
239
|
|
|
* @throws Exception |
240
|
|
|
* |
241
|
|
|
* @return Response |
242
|
|
|
*/ |
243
|
|
|
public function changeAppUserActiveStatus(int $id, int $appsId) : Response |
244
|
|
|
{ |
245
|
|
|
$userAssociatedToApp = UsersAssociatedApps::findFirstOrFail([ |
246
|
|
|
'conditions' => 'users_id = ?0 and apps_id = ?1 and companies_id = ?2 and is_deleted = 0', |
247
|
|
|
'bind' => [$id, $this->app->getId(), $this->userData->getDefaultCompany()->getId()] |
248
|
|
|
]); |
249
|
|
|
|
250
|
|
|
$userAssociatedToApp->user_active = $userAssociatedToApp->user_active ? 0 : 1; |
251
|
|
|
$userAssociatedToApp->updateOrFail(); |
252
|
|
|
return $this->response($userAssociatedToApp); |
253
|
|
|
} |
254
|
|
|
} |
255
|
|
|
|
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