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 | |||
16 | /** |
||
17 | * Users controller |
||
18 | * |
||
19 | */ |
||
20 | class UsersController extends \Baka\Auth\UsersController |
||
21 | { |
||
22 | /* |
||
23 | * fields we accept to create |
||
24 | * |
||
25 | * @var array |
||
26 | */ |
||
27 | protected $createFields = ['name', 'firstname', 'lastname', 'displayname', 'email', 'password', 'created_at', 'updated_at', 'default_company', 'family']; |
||
28 | |||
29 | /* |
||
30 | * fields we accept to create |
||
31 | * |
||
32 | * @var array |
||
33 | */ |
||
34 | protected $updateFields = ['name', 'firstname', 'lastname', 'displayname', 'email', 'password', 'created_at', 'updated_at', 'default_company']; |
||
35 | |||
36 | /** |
||
37 | * set objects |
||
38 | * |
||
39 | * @return void |
||
40 | */ |
||
41 | public function onConstruct() |
||
42 | { |
||
43 | $this->model = new Users(); |
||
44 | } |
||
45 | |||
46 | /** |
||
47 | * Associate a Device with the corrent loggedin user |
||
48 | * |
||
49 | * @url /users/{id}/device |
||
50 | * @method POST |
||
51 | * @return Response |
||
52 | */ |
||
53 | public function devices(): Response |
||
54 | { |
||
55 | //Ok let validate user password |
||
56 | $validation = new Validation(); |
||
57 | $validation->add('app', new PresenceOf(['message' => _('App name is required.')])); |
||
58 | $validation->add('deviceId', new PresenceOf(['message' => _('device ID is required.')])); |
||
59 | |||
60 | //validate this form for password |
||
61 | $messages = $validation->validate($this->request->getPost()); |
||
62 | if (count($messages)) { |
||
63 | foreach ($messages as $message) { |
||
64 | throw new BadRequestHttpException((string) $message); |
||
65 | } |
||
66 | } |
||
67 | |||
68 | $app = $this->request->getPost('app', 'string'); |
||
69 | $deviceId = $this->request->getPost('deviceId', 'string'); |
||
70 | |||
71 | //get the app source |
||
72 | if ($source = Sources::getByTitle($app)) { |
||
73 | if (!$userSource = UserLinkedSources::findFirst(['conditions' => 'user_id = ?0 and source_user_id_text =?1', 'bind' => [$this->userData->getId(), $deviceId]])) { |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
74 | $userSource = new UserLinkedSources(); |
||
75 | $userSource->user_id = $this->userData->getId(); |
||
0 ignored issues
–
show
The property
user_id does not exist on Gewaer\Models\UserLinkedSources . Since you implemented __set , consider adding a @property annotation.
![]() |
|||
76 | $userSource->source_id = $source->source_id; |
||
77 | $userSource->source_user_id = $this->userData->getId(); |
||
0 ignored issues
–
show
The property
source_user_id does not exist on Gewaer\Models\UserLinkedSources . Since you implemented __set , consider adding a @property annotation.
![]() |
|||
78 | $userSource->source_user_id_text = $deviceId; |
||
0 ignored issues
–
show
The property
source_user_id_text does not exist on Gewaer\Models\UserLinkedSources . Since you implemented __set , consider adding a @property annotation.
![]() |
|||
79 | $userSource->source_username = $this->userData->displayname . ' ' . $app; |
||
80 | |||
81 | if (!$userSource->save()) { |
||
82 | throw new UnprocessableEntityHttpException(current($userSource->getMessages())); |
||
83 | } |
||
84 | |||
85 | $msg = 'User Device Associated'; |
||
86 | } else { |
||
87 | $msg = 'User Device Already Associated'; |
||
88 | } |
||
89 | } |
||
90 | |||
91 | //clean password @todo move this to a better place |
||
92 | $this->userData->password = null; |
||
93 | |||
94 | return $this->response([ |
||
95 | 'msg' => $msg, |
||
96 | 'user' => $this->userData |
||
97 | ]); |
||
98 | } |
||
99 | } |
||
100 |