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