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