1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Gewaer\Api\Controllers; |
6
|
|
|
|
7
|
|
|
use Gewaer\Models\Users; |
8
|
|
|
use Gewaer\Models\Companies; |
9
|
|
|
use Phalcon\Http\Response; |
10
|
|
|
use Phalcon\Validation; |
11
|
|
|
use Phalcon\Validation\Validator\PresenceOf; |
12
|
|
|
use Gewaer\Exception\BadRequestHttpException; |
13
|
|
|
use Baka\Http\QueryParser; |
14
|
|
|
use Gewaer\Exception\ModelException; |
15
|
|
|
use Gewaer\Exception\NotFoundHttpException; |
16
|
|
|
use Gewaer\Models\AccessList; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class UsersController |
20
|
|
|
* |
21
|
|
|
* @package Gewaer\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', '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', '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
|
|
|
['default_company', ':', $this->userData->currentCompanyId()], |
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
|
|
|
//find the info |
79
|
|
|
$user = $this->model->findFirst([ |
80
|
|
|
'id = ?0 AND is_deleted = 0', |
81
|
|
|
'bind' => [$this->userData->getId()], |
82
|
|
|
]); |
83
|
|
|
|
84
|
|
|
$user->password = null; |
85
|
|
|
|
86
|
|
|
//get relationship |
87
|
|
|
if ($this->request->hasQuery('relationships')) { |
88
|
|
|
$relationships = $this->request->getQuery('relationships', 'string'); |
89
|
|
|
|
90
|
|
|
$user = QueryParser::parseRelationShips($relationships, $user); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
//if you search for roles we give you the access for this app |
94
|
|
|
if (array_key_exists('roles', $user)) { |
95
|
|
|
$accesList = AccessList::find([ |
96
|
|
|
'conditions' => 'roles_name = ?0 and apps_id = ?1 and allowed = 0', |
97
|
|
|
'bind' => [$user['roles'][0]->name, $this->config->app->id] |
98
|
|
|
]); |
99
|
|
|
|
100
|
|
|
if (count($accesList) > 0) { |
101
|
|
|
foreach ($accesList as $access) { |
102
|
|
|
$user['access_list'][strtolower($access->resources_name)][$access->access_name] = 0; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
if ($user) { |
108
|
|
|
return $this->response($user); |
109
|
|
|
} else { |
110
|
|
|
throw new ModelException('Record not found'); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Update a User Info |
116
|
|
|
* |
117
|
|
|
* @method PUT |
118
|
|
|
* @url /v1/users/{id} |
119
|
|
|
* |
120
|
|
|
* @return Response |
121
|
|
|
*/ |
122
|
|
|
public function edit($id) : Response |
123
|
|
|
{ |
124
|
|
|
//none admin users can only edit themselves |
125
|
|
|
if (!$this->userData->hasRole('Default.Admins')) { |
126
|
|
|
$id = $this->userData->getId(); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
if ($user = $this->model->findFirst($id)) { |
130
|
|
|
$request = $this->request->getPut(); |
131
|
|
|
|
132
|
|
|
if (empty($request)) { |
133
|
|
|
$request = $this->request->getJsonRawBody(true); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
if (empty($request)) { |
137
|
|
|
throw new BadRequestHttpException(_('No data to update this account with ')); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
//update password |
141
|
|
|
if (array_key_exists('new_password', $request) && (!empty($request['new_password']) && !empty($request['current_password']))) { |
142
|
|
|
//Ok let validate user password |
143
|
|
|
$validation = new Validation(); |
144
|
|
|
$validation->add('new_password', new PresenceOf(['message' => 'The new_password is required.'])); |
145
|
|
|
$validation->add('current_password', new PresenceOf(['message' => 'The current_password is required.'])); |
146
|
|
|
$validation->add('confirm_new_password', new PresenceOf(['message' => 'The confirm_new_password is required.'])); |
147
|
|
|
$messages = $validation->validate($request); |
148
|
|
|
|
149
|
|
|
if (count($messages)) { |
150
|
|
|
foreach ($messages as $message) { |
151
|
|
|
throw new BadRequestHttpException((string)$message); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
$user->updatePassword($request['current_password'], $request['new_password'], $request['confirm_new_password']); |
156
|
|
|
} else { |
157
|
|
|
//remove on any actino that doesnt involve password |
158
|
|
|
unset($request['password']); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
//change my default company |
162
|
|
|
if (array_key_exists('default_company', $request)) { |
163
|
|
|
if ($company = Companies::findFirst($request['default_company'])) { |
164
|
|
|
if ($company->userAssociatedToCompany($this->userData)) { |
165
|
|
|
$user->default_company = $company->getId(); |
166
|
|
|
unset($request['default_company']); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
//update |
172
|
|
|
if ($user->update($request, $this->updateFields)) { |
173
|
|
|
$user->password = null; |
174
|
|
|
return $this->response($user); |
175
|
|
|
} else { |
176
|
|
|
//didnt work |
177
|
|
|
throw new ModelException((string)current($user->getMessages())); |
178
|
|
|
} |
179
|
|
|
} else { |
180
|
|
|
throw new NotFoundHttpException('Record not found'); |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Add users notifications |
186
|
|
|
* |
187
|
|
|
* @param int $id |
188
|
|
|
* @method PUT |
189
|
|
|
* @return Response |
190
|
|
|
*/ |
191
|
|
|
public function updateNotifications(int $id) : Response |
192
|
|
|
{ |
193
|
|
|
//get the notification array |
194
|
|
|
//delete the current ones |
195
|
|
|
//iterate and save into users |
196
|
|
|
|
197
|
|
|
return $this->response(['OK' => $id]); |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|