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 Phalcon\Http\Response; |
||
11 | use Phalcon\Validation; |
||
12 | use Phalcon\Validation\Validator\PresenceOf; |
||
13 | use Gewaer\Exception\BadRequestHttpException; |
||
14 | use Gewaer\Exception\UnprocessableEntityHttpException; |
||
15 | use Baka\Http\QueryParser; |
||
16 | use Gewaer\Exception\ModelException; |
||
17 | use Gewaer\Exception\NotFoundHttpException; |
||
18 | use Gewaer\Models\AccessList; |
||
19 | |||
20 | /** |
||
21 | * Class UsersController |
||
22 | * |
||
23 | * @package Gewaer\Api\Controllers |
||
24 | * |
||
25 | * @property Users $userData |
||
26 | * @property Request $request |
||
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', 'email', 'password', 'created_at', 'updated_at', 'default_company', 'family']; |
||
36 | |||
37 | /* |
||
38 | * fields we accept to create |
||
39 | * |
||
40 | * @var array |
||
41 | */ |
||
42 | protected $updateFields = ['name', 'firstname', 'lastname', 'displayname', 'email', 'password', 'created_at', 'updated_at', 'default_company']; |
||
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('Default.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->default_company], |
||
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] |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
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 | if ($user = $this->model->findFirst($this->userData->getId())) { |
||
125 | $request = $this->request->getPut(); |
||
126 | |||
127 | if (empty($request)) { |
||
128 | $request = $this->request->getJsonRawBody(true); |
||
129 | } |
||
130 | |||
131 | //clean pass |
||
132 | if (array_key_exists('password', $request) && !empty($request['password'])) { |
||
133 | $user->password = Users::passwordHash($request['password']); |
||
134 | unset($request['password']); |
||
135 | } |
||
136 | |||
137 | //clean default company |
||
138 | if (array_key_exists('default_company', $request)) { |
||
139 | //@todo check if I belong to this company |
||
140 | if ($company = Companies::findFirst($request['default_company'])) { |
||
141 | $user->default_company = $company->getId(); |
||
142 | unset($request['default_company']); |
||
143 | } |
||
144 | } |
||
145 | |||
146 | //update |
||
147 | if ($user->update($request, $this->updateFields)) { |
||
148 | $user->password = null; |
||
149 | return $this->response($user); |
||
150 | } else { |
||
151 | //didnt work |
||
152 | throw new ModelException((string) current($user->getMessages())); |
||
153 | } |
||
154 | } else { |
||
155 | throw new NotFoundHttpException('Record not found'); |
||
156 | } |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * Add users notifications |
||
161 | * |
||
162 | * @param int $id |
||
163 | * @method PUT |
||
164 | * @return Response |
||
165 | */ |
||
166 | public function updateNotifications($id): Response |
||
167 | { |
||
168 | //get the notification array |
||
169 | //delete the current ones |
||
170 | //iterate and save into users |
||
171 | |||
172 | return $this->response(['OK']); |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * Associate a Device with the corrent loggedin user |
||
177 | * |
||
178 | * @url /users/{id}/device |
||
179 | * @method POST |
||
180 | * @return Response |
||
181 | */ |
||
182 | public function devices(): Response |
||
183 | { |
||
184 | //Ok let validate user password |
||
185 | $validation = new Validation(); |
||
186 | $validation->add('app', new PresenceOf(['message' => _('App name is required.')])); |
||
187 | $validation->add('deviceId', new PresenceOf(['message' => _('device ID is required.')])); |
||
188 | |||
189 | //validate this form for password |
||
190 | $messages = $validation->validate($this->request->getPost()); |
||
191 | if (count($messages)) { |
||
192 | foreach ($messages as $message) { |
||
193 | throw new BadRequestHttpException((string) $message); |
||
194 | } |
||
195 | } |
||
196 | |||
197 | $app = $this->request->getPost('app', 'string'); |
||
198 | $deviceId = $this->request->getPost('deviceId', 'string'); |
||
199 | |||
200 | //get the app source |
||
201 | if ($source = Sources::getByTitle($app)) { |
||
202 | if (!$userSource = UserLinkedSources::findFirst(['conditions' => 'users_id = ?0 and source_users_id_text =?1', 'bind' => [$this->userData->getId(), $deviceId]])) { |
||
203 | $userSource = new UserLinkedSources(); |
||
204 | $userSource->users_id = $this->userData->getId(); |
||
205 | $userSource->source_id = $source->getId(); |
||
206 | $userSource->source_users_id = $this->userData->getId(); |
||
207 | $userSource->source_users_id_text = $deviceId; |
||
208 | $userSource->source_username = $this->userData->displayname . ' ' . $app; |
||
209 | |||
210 | if (!$userSource->save()) { |
||
211 | throw new UnprocessableEntityHttpException((string) current($userSource->getMessages())); |
||
212 | } |
||
213 | |||
214 | $msg = 'User Device Associated'; |
||
215 | } else { |
||
216 | $msg = 'User Device Already Associated'; |
||
217 | } |
||
218 | } |
||
219 | |||
220 | //clean password @todo move this to a better place |
||
221 | $this->userData->password = null; |
||
222 | |||
223 | return $this->response([ |
||
224 | 'msg' => $msg, |
||
225 | 'user' => $this->userData |
||
226 | ]); |
||
227 | } |
||
228 | } |
||
229 |