|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Gewaer\Api\Controllers; |
|
6
|
|
|
|
|
7
|
|
|
use Gewaer\Models\Users; |
|
8
|
|
|
use Gewaer\Models\UserLinkedSources; |
|
9
|
|
|
use Baka\Auth\Models\Sources; |
|
10
|
|
|
use Gewaer\Models\Companies; |
|
11
|
|
|
use Phalcon\Http\Response; |
|
12
|
|
|
use Phalcon\Validation; |
|
13
|
|
|
use Phalcon\Validation\Validator\PresenceOf; |
|
14
|
|
|
use Gewaer\Exception\BadRequestHttpException; |
|
15
|
|
|
use Gewaer\Exception\UnprocessableEntityHttpException; |
|
16
|
|
|
use Baka\Http\QueryParser; |
|
17
|
|
|
use Gewaer\Exception\ModelException; |
|
18
|
|
|
use Gewaer\Exception\NotFoundHttpException; |
|
19
|
|
|
use Gewaer\Models\AccessList; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Class UsersController |
|
23
|
|
|
* |
|
24
|
|
|
* @package Gewaer\Api\Controllers |
|
25
|
|
|
* |
|
26
|
|
|
* @property Users $userData |
|
27
|
|
|
* @property Request $request |
|
28
|
|
|
* @property Config $config |
|
29
|
|
|
*/ |
|
30
|
|
|
class UsersController extends \Baka\Auth\UsersController |
|
31
|
|
|
{ |
|
32
|
|
|
/* |
|
33
|
|
|
* fields we accept to create |
|
34
|
|
|
* |
|
35
|
|
|
* @var array |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $createFields = ['name', 'firstname', 'lastname', 'displayname', 'language', 'email', 'password', 'created_at', 'updated_at', 'default_company', 'family', 'cell_phone_number']; |
|
38
|
|
|
|
|
39
|
|
|
/* |
|
40
|
|
|
* fields we accept to create |
|
41
|
|
|
* |
|
42
|
|
|
* @var array |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $updateFields = ['name', 'firstname', 'lastname', 'displayname', 'language', 'email', 'password', 'created_at', 'updated_at', 'default_company', 'cell_phone_number']; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* set objects |
|
48
|
|
|
* |
|
49
|
|
|
* @return void |
|
50
|
|
|
*/ |
|
51
|
|
|
public function onConstruct() |
|
52
|
|
|
{ |
|
53
|
|
|
$this->model = new Users(); |
|
54
|
|
|
|
|
55
|
|
|
//if you are not a admin you cant see all the users |
|
56
|
|
|
if (!$this->userData->hasRole('Defaults.Admins')) { |
|
|
|
|
|
|
57
|
|
|
$this->additionalSearchFields = [ |
|
58
|
|
|
['id', ':', $this->userData->getId()], |
|
59
|
|
|
]; |
|
60
|
|
|
} else { |
|
61
|
|
|
//admin get all the users for this company |
|
62
|
|
|
$this->additionalSearchFields = [ |
|
63
|
|
|
['default_company', ':', $this->userData->default_company], |
|
64
|
|
|
]; |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Get Uer |
|
70
|
|
|
* |
|
71
|
|
|
* @param mixed $id |
|
72
|
|
|
* |
|
73
|
|
|
* @method GET |
|
74
|
|
|
* @url /v1/users/{id} |
|
75
|
|
|
* |
|
76
|
|
|
* @return Response |
|
77
|
|
|
*/ |
|
78
|
|
|
public function getById($id) : Response |
|
79
|
|
|
{ |
|
80
|
|
|
//find the info |
|
81
|
|
|
$user = $this->model->findFirst([ |
|
82
|
|
|
'id = ?0 AND is_deleted = 0', |
|
83
|
|
|
'bind' => [$this->userData->getId()], |
|
84
|
|
|
]); |
|
85
|
|
|
|
|
86
|
|
|
$user->password = null; |
|
87
|
|
|
|
|
88
|
|
|
//get relationship |
|
89
|
|
|
if ($this->request->hasQuery('relationships')) { |
|
90
|
|
|
$relationships = $this->request->getQuery('relationships', 'string'); |
|
91
|
|
|
|
|
92
|
|
|
$user = QueryParser::parseRelationShips($relationships, $user); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
//if you search for roles we give you the access for this app |
|
96
|
|
|
if (array_key_exists('roles', $user)) { |
|
97
|
|
|
$accesList = AccessList::find([ |
|
98
|
|
|
'conditions' => 'roles_name = ?0 and apps_id = ?1 and allowed = 0', |
|
99
|
|
|
'bind' => [$user['roles'][0]->name, $this->config->app->id] |
|
100
|
|
|
]); |
|
101
|
|
|
|
|
102
|
|
|
if (count($accesList) > 0) { |
|
103
|
|
|
foreach ($accesList as $access) { |
|
104
|
|
|
$user['access_list'][strtolower($access->resources_name)][$access->access_name] = 0; |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
if ($user) { |
|
110
|
|
|
return $this->response($user); |
|
111
|
|
|
} else { |
|
112
|
|
|
throw new ModelException('Record not found'); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Update a User Info |
|
118
|
|
|
* |
|
119
|
|
|
* @method PUT |
|
120
|
|
|
* @url /v1/users/{id} |
|
121
|
|
|
* |
|
122
|
|
|
* @return Response |
|
123
|
|
|
*/ |
|
124
|
|
|
public function edit($id) : Response |
|
125
|
|
|
{ |
|
126
|
|
|
//none admin users can only edit themselves |
|
127
|
|
|
if (!$this->userData->hasRole('Default.Admins')) { |
|
|
|
|
|
|
128
|
|
|
$id = $this->userData->getId(); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
if ($user = $this->model->findFirst($id)) { |
|
132
|
|
|
$request = $this->request->getPut(); |
|
133
|
|
|
|
|
134
|
|
|
if (empty($request)) { |
|
135
|
|
|
$request = $this->request->getJsonRawBody(true); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
//update password |
|
139
|
|
|
if (array_key_exists('new_password', $request) && (!empty($request['new_password']) && !empty($request['password']))) { |
|
140
|
|
|
//Ok let validate user password |
|
141
|
|
|
$validation = new Validation(); |
|
142
|
|
|
$validation->add('new_password', new PresenceOf(['message' => 'The new_password is required.'])); |
|
143
|
|
|
$validation->add('current_password', new PresenceOf(['message' => 'The current_password is required.'])); |
|
144
|
|
|
$validation->add('confirm_new_password', new PresenceOf(['message' => 'The confirm_new_password is required.'])); |
|
145
|
|
|
$messages = $validation->validate($request); |
|
146
|
|
|
|
|
147
|
|
|
if (count($messages)) { |
|
148
|
|
|
foreach ($messages as $message) { |
|
149
|
|
|
throw new BadRequestHttpException((string)$message); |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
$user->updatePassword($request['current_password'], $request['new_password'], $request['confirm_new_password']); |
|
154
|
|
|
} else { |
|
155
|
|
|
//remove on any actino that doesnt involve password |
|
156
|
|
|
unset($request['password']); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
//change my default company |
|
160
|
|
|
if (array_key_exists('default_company', $request)) { |
|
161
|
|
|
if ($company = Companies::findFirst($request['default_company'])) { |
|
162
|
|
|
if ($company->userAssociatedToCompany($this->userData)) { |
|
163
|
|
|
$user->default_company = $company->getId(); |
|
164
|
|
|
unset($request['default_company']); |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
//update |
|
170
|
|
|
if ($user->update($request, $this->updateFields)) { |
|
171
|
|
|
$user->password = null; |
|
172
|
|
|
return $this->response($user); |
|
173
|
|
|
} else { |
|
174
|
|
|
//didnt work |
|
175
|
|
|
throw new ModelException((string)current($user->getMessages())); |
|
176
|
|
|
} |
|
177
|
|
|
} else { |
|
178
|
|
|
throw new NotFoundHttpException('Record not found'); |
|
179
|
|
|
} |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Add users notifications |
|
184
|
|
|
* |
|
185
|
|
|
* @param int $id |
|
186
|
|
|
* @method PUT |
|
187
|
|
|
* @return Response |
|
188
|
|
|
*/ |
|
189
|
|
|
public function updateNotifications($id) : Response |
|
|
|
|
|
|
190
|
|
|
{ |
|
191
|
|
|
//get the notification array |
|
192
|
|
|
//delete the current ones |
|
193
|
|
|
//iterate and save into users |
|
194
|
|
|
|
|
195
|
|
|
return $this->response(['OK']); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* Associate a Device with the corrent loggedin user |
|
200
|
|
|
* |
|
201
|
|
|
* @url /users/{id}/device |
|
202
|
|
|
* @method POST |
|
203
|
|
|
* @return Response |
|
204
|
|
|
*/ |
|
205
|
|
|
public function devices() : Response |
|
206
|
|
|
{ |
|
207
|
|
|
//Ok let validate user password |
|
208
|
|
|
$validation = new Validation(); |
|
209
|
|
|
$validation->add('app', new PresenceOf(['message' => _('App name is required.')])); |
|
210
|
|
|
$validation->add('deviceId', new PresenceOf(['message' => _('device ID is required.')])); |
|
211
|
|
|
|
|
212
|
|
|
//validate this form for password |
|
213
|
|
|
$messages = $validation->validate($this->request->getPost()); |
|
214
|
|
|
if (count($messages)) { |
|
215
|
|
|
foreach ($messages as $message) { |
|
216
|
|
|
throw new BadRequestHttpException((string)$message); |
|
217
|
|
|
} |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
$app = $this->request->getPost('app', 'string'); |
|
221
|
|
|
$deviceId = $this->request->getPost('deviceId', 'string'); |
|
222
|
|
|
|
|
223
|
|
|
//get the app source |
|
224
|
|
|
if ($source = Sources::getByTitle($app)) { |
|
225
|
|
|
if (!$userSource = UserLinkedSources::findFirst(['conditions' => 'users_id = ?0 and source_users_id_text =?1', 'bind' => [$this->userData->getId(), $deviceId]])) { |
|
|
|
|
|
|
226
|
|
|
$userSource = new UserLinkedSources(); |
|
227
|
|
|
$userSource->users_id = $this->userData->getId(); |
|
228
|
|
|
$userSource->source_id = $source->getId(); |
|
229
|
|
|
$userSource->source_users_id = $this->userData->getId(); |
|
230
|
|
|
$userSource->source_users_id_text = $deviceId; |
|
231
|
|
|
$userSource->source_username = $this->userData->displayname . ' ' . $app; |
|
232
|
|
|
|
|
233
|
|
|
if (!$userSource->save()) { |
|
234
|
|
|
throw new UnprocessableEntityHttpException((string)current($userSource->getMessages())); |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
$msg = 'User Device Associated'; |
|
238
|
|
|
} else { |
|
239
|
|
|
$msg = 'User Device Already Associated'; |
|
240
|
|
|
} |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
//clean password @todo move this to a better place |
|
244
|
|
|
$this->userData->password = null; |
|
245
|
|
|
|
|
246
|
|
|
return $this->response([ |
|
247
|
|
|
'msg' => $msg, |
|
|
|
|
|
|
248
|
|
|
'user' => $this->userData |
|
249
|
|
|
]); |
|
250
|
|
|
} |
|
251
|
|
|
} |
|
252
|
|
|
|
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.