1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Gewaer\Api\Controllers; |
||
6 | |||
7 | use Gewaer\Models\Users; |
||
8 | use Baka\Auth\Models\Users as BakaUsers; |
||
9 | use Gewaer\Models\UsersInvite; |
||
10 | use Gewaer\Models\UserLinkedSources; |
||
11 | use Gewaer\Exception\ServerErrorHttpException; |
||
12 | use Gewaer\Exception\UnprocessableEntityHttpException; |
||
13 | use Phalcon\Http\Response; |
||
14 | use Phalcon\Validation\Validator\PresenceOf; |
||
15 | use Phalcon\Validation; |
||
16 | use Phalcon\Security\Random; |
||
17 | |||
18 | /** |
||
19 | * Class AuthController |
||
20 | * |
||
21 | * @package Gewaer\Api\Controllers |
||
22 | * |
||
23 | * @property Users $userData |
||
24 | * @property Request $request |
||
25 | * @property Config $config |
||
26 | * @property \Baka\Mail\Message $mail |
||
27 | */ |
||
28 | class AuthController extends \Baka\Auth\AuthController |
||
29 | { |
||
30 | /** |
||
31 | * Hash for invite record |
||
32 | */ |
||
33 | protected $invite_hash = ' '; |
||
34 | |||
35 | /** |
||
36 | * Setup for this controller |
||
37 | * |
||
38 | * @return void |
||
39 | */ |
||
40 | 14 | public function onConstruct() |
|
41 | { |
||
42 | 14 | $this->userLinkedSourcesModel = new UserLinkedSources(); |
|
43 | 14 | $this->userModel = new Users(); |
|
44 | |||
45 | 14 | if (!isset($this->config->jwt)) { |
|
46 | throw new ServerErrorHttpException('You need to configure your app JWT'); |
||
47 | } |
||
48 | 14 | } |
|
49 | |||
50 | /** |
||
51 | * Set the email config array we are going to be sending |
||
52 | * |
||
53 | * @param String $emailAction |
||
54 | * @param Users $user |
||
55 | */ |
||
56 | protected function sendEmail(BakaUsers $user, string $type): void |
||
57 | { |
||
58 | $send = true; |
||
59 | $subject = null; |
||
60 | $body = null; |
||
61 | |||
62 | switch ($type) { |
||
63 | case 'recover': |
||
64 | $recoveryLink = $this->config->app->frontEndUrl . '/user/reset/' . $user->user_activation_forgot; |
||
65 | |||
66 | $subject = _('Password Recovery'); |
||
67 | $body = sprintf(_('Click %shere%s to set a new password for your account.'), '<a href="' . $recoveryLink . '" target="_blank">', '</a>'); |
||
68 | |||
69 | // send email to recover password |
||
70 | break; |
||
71 | case 'reset': |
||
72 | $activationUrl = $this->config->app->frontEndUrl . '/user/activate/' . $user->user_activation_key; |
||
73 | |||
74 | $subject = _('Password Updated!'); |
||
75 | $body = sprintf(_('Your password was update please, use this link to activate your account: %sActivate account%s'), '<a href="' . $activationUrl . '">', '</a>'); |
||
76 | // send email that password was update |
||
77 | break; |
||
78 | case 'invite': |
||
79 | $activationUrl = $this->config->app->frontEndUrl . '/user/invite/' . $this->invite_hash; |
||
80 | //Send invitation link to person |
||
81 | $subject = _('You have been invited!'); |
||
82 | $body = sprintf(_('Your have been invite to join our system, use this link to succesfully create your account: %Create account%s'), '<a href="' . $activationUrl . '">', '</a>'); |
||
83 | |||
84 | break; |
||
85 | default: |
||
86 | $send = false; |
||
87 | break; |
||
88 | } |
||
89 | |||
90 | if ($send) { |
||
91 | $this->mail |
||
92 | ->to($user->email) |
||
93 | ->subject($subject) |
||
94 | ->content($body) |
||
95 | ->sendNow(); |
||
96 | } |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * Sets up invitation information for a would be user |
||
101 | * @return Response |
||
102 | */ |
||
103 | 1 | public function insertInvite(): Response |
|
104 | { |
||
105 | 1 | $request = $this->request->getPost(); |
|
106 | 1 | $random = new Random(); |
|
107 | |||
108 | 1 | $validation = new Validation(); |
|
109 | 1 | $validation->add('email', new PresenceOf(['message' => _('The email is required.')])); |
|
110 | 1 | $validation->add('role', new PresenceOf(['message' => _('The role is required.')])); |
|
111 | |||
112 | //validate this form for password |
||
113 | 1 | $messages = $validation->validate($this->request->getPost()); |
|
114 | 1 | if (count($messages)) { |
|
115 | foreach ($messages as $message) { |
||
116 | throw new ServerErrorHttpException((string)$message); |
||
117 | } |
||
118 | } |
||
119 | |||
120 | //Save data to users_invite table and generate a hash for the invite |
||
121 | 1 | $userInvite = new UsersInvite(); |
|
122 | 1 | $userInvite->company_id = $this->userData->default_company; |
|
123 | 1 | $userInvite->app_id = $this->app->getId(); |
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
124 | 1 | $userInvite->role_id = $request['role'] == 'Admins' ? 1 : 2; |
|
125 | 1 | $userInvite->email = $request['email']; |
|
126 | 1 | $userInvite->invite_hash = $random->base58(); |
|
127 | 1 | $userInvite->created_at = date('Y-m-d H:m:s'); |
|
128 | |||
129 | 1 | if (!$userInvite->save()) { |
|
130 | throw new UnprocessableEntityHttpException((string) current($userInvite->getMessages())); |
||
131 | } |
||
132 | |||
133 | $userInviteArray = $userInvite->toArray(); |
||
134 | |||
135 | $this->setInviteHash($userInviteArray['invite_hash']); |
||
136 | |||
137 | return $this->response($userInviteArray); |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * Set Invite Hash |
||
142 | */ |
||
143 | protected function setInviteHash(string $hash) |
||
144 | { |
||
145 | $this->invite_hash = $hash; |
||
146 | } |
||
147 | } |
||
148 |