1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Gewaer\Api\Controllers; |
6
|
|
|
|
7
|
|
|
use Gewaer\Models\UsersInvite; |
8
|
|
|
use Gewaer\Models\Users; |
9
|
|
|
use Phalcon\Security\Random; |
10
|
|
|
use Phalcon\Validation; |
11
|
|
|
use Phalcon\Validation\Validator\PresenceOf; |
12
|
|
|
use Phalcon\Validation\Validator\StringLength; |
13
|
|
|
use Gewaer\Exception\UnprocessableEntityHttpException; |
14
|
|
|
use Gewaer\Exception\NotFoundHttpException; |
15
|
|
|
use Gewaer\Exception\ServerErrorHttpException; |
16
|
|
|
use Phalcon\Http\Response; |
17
|
|
|
use Gewaer\Models\EmailTemplates; |
18
|
|
|
use Gewaer\Models\Roles; |
19
|
|
|
use Exception; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class LanguagesController |
23
|
|
|
* @property Users $userData |
24
|
|
|
* @property Request $request |
25
|
|
|
* @property Config $config |
26
|
|
|
* @property Apps $app |
27
|
|
|
* @property Mail $mail |
28
|
|
|
* @package Gewaer\Api\Controllers |
29
|
|
|
* |
30
|
|
|
*/ |
31
|
|
|
class UsersInviteController extends BaseController |
32
|
|
|
{ |
33
|
|
|
/* |
34
|
|
|
* fields we accept to create |
35
|
|
|
* |
36
|
|
|
* @var array |
37
|
|
|
*/ |
38
|
|
|
protected $createFields = ['invite_hash', 'company_id', 'role_id', 'app_id', 'email']; |
39
|
|
|
|
40
|
|
|
/* |
41
|
|
|
* fields we accept to create |
42
|
|
|
* |
43
|
|
|
* @var array |
44
|
|
|
*/ |
45
|
|
|
protected $updateFields = ['invite_hash', 'company_id', 'role_id', 'app_id', 'email']; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* set objects |
49
|
|
|
* |
50
|
|
|
* @return void |
51
|
|
|
*/ |
52
|
3 |
|
public function onConstruct() |
53
|
|
|
{ |
54
|
3 |
|
$this->model = new UsersInvite(); |
55
|
3 |
|
$this->additionalSearchFields = [ |
56
|
3 |
|
['is_deleted', ':', '0'], |
57
|
3 |
|
['company_id', ':', $this->userData->default_company], |
58
|
|
|
]; |
59
|
3 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Sets up invitation information for a would be user |
63
|
|
|
* @return Response |
64
|
|
|
*/ |
65
|
3 |
|
public function insertInvite(): Response |
66
|
|
|
{ |
67
|
3 |
|
$request = $this->request->getPost(); |
68
|
3 |
|
$random = new Random(); |
69
|
|
|
|
70
|
3 |
|
$validation = new Validation(); |
71
|
3 |
|
$validation->add('email', new PresenceOf(['message' => _('The email is required.')])); |
72
|
3 |
|
$validation->add('role', new PresenceOf(['message' => _('The role is required.')])); |
73
|
|
|
|
74
|
|
|
//validate this form for password |
75
|
3 |
|
$messages = $validation->validate($this->request->getPost()); |
76
|
3 |
|
if (count($messages)) { |
77
|
|
|
foreach ($messages as $message) { |
78
|
|
|
throw new ServerErrorHttpException((string)$message); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
//Save data to users_invite table and generate a hash for the invite |
83
|
3 |
|
$userInvite = $this->model; |
84
|
3 |
|
$userInvite->company_id = $this->userData->default_company; |
85
|
3 |
|
$userInvite->app_id = $this->app->getId(); |
86
|
3 |
|
$userInvite->role_id = Roles::getByAppName($request['role'], $this->userData->defaultCompany)->getId(); |
87
|
3 |
|
$userInvite->email = $request['email']; |
88
|
3 |
|
$userInvite->invite_hash = $random->base58(); |
89
|
3 |
|
$userInvite->created_at = date('Y-m-d H:m:s'); |
90
|
|
|
|
91
|
3 |
|
if (!$userInvite->save()) { |
92
|
|
|
throw new UnprocessableEntityHttpException((string) current($userInvite->getMessages())); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
//Fetch email template of user |
96
|
3 |
|
$emailTemplate = EmailTemplates::getByName('users-invite'); |
97
|
|
|
|
98
|
|
|
// Lets send the mail |
99
|
|
|
|
100
|
3 |
|
$invitationUrl = $this->config->app->frontEndUrl . 'user-invite/' . $userInvite->invite_hash; |
101
|
|
|
|
102
|
3 |
|
if (!defined('API_TESTS')) { |
103
|
|
|
$subject = _('You have been invited!'); |
104
|
|
|
$this->mail |
105
|
|
|
->to($userInvite->email) |
106
|
|
|
->subject($subject) |
107
|
|
|
->params($invitationUrl) |
108
|
|
|
->content($emailTemplate->template) |
109
|
|
|
->sendNow(); |
110
|
|
|
} |
111
|
|
|
|
112
|
3 |
|
return $this->response($userInvite); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Add invited user to our system |
117
|
|
|
* @return Response |
118
|
|
|
*/ |
119
|
3 |
|
public function processUserInvite(string $hash): Response |
120
|
|
|
{ |
121
|
3 |
|
$request = $this->request->getPost(); |
122
|
|
|
|
123
|
3 |
|
if (empty($request)) { |
124
|
|
|
$request = $this->request->getJsonRawBody(true); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
//Ok let validate user password |
128
|
3 |
|
$validation = new Validation(); |
129
|
3 |
|
$validation->add('password', new PresenceOf(['message' => _('The password is required.')])); |
130
|
|
|
|
131
|
3 |
|
$validation->add( |
132
|
3 |
|
'password', |
133
|
3 |
|
new StringLength([ |
134
|
3 |
|
'min' => 8, |
135
|
3 |
|
'messageMinimum' => _('Password is too short. Minimum 8 characters.'), |
136
|
|
|
]) |
137
|
|
|
); |
138
|
|
|
|
139
|
|
|
//validate this form for password |
140
|
3 |
|
$messages = $validation->validate($request); |
141
|
3 |
|
if (count($messages)) { |
142
|
|
|
foreach ($messages as $message) { |
143
|
|
|
throw new ServerErrorHttpException((string)$message); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
//Lets find users_invite by hash on our database |
148
|
3 |
|
$usersInvite = $this->model::findFirst([ |
149
|
3 |
|
'conditions' => 'invite_hash = ?0 and is_deleted = 0', |
150
|
3 |
|
'bind' => [$hash] |
151
|
|
|
]); |
152
|
|
|
|
153
|
3 |
|
if (!is_object($usersInvite)) { |
154
|
|
|
throw new NotFoundHttpException('Users Invite not found'); |
155
|
|
|
} |
156
|
|
|
|
157
|
3 |
|
$newUser = new Users(); |
158
|
3 |
|
$newUser->firstname = $request['firstname']; |
159
|
3 |
|
$newUser->lastname = $request['lastname']; |
160
|
3 |
|
$newUser->displayname = $request['displayname']; |
161
|
3 |
|
$newUser->password = ltrim(trim($request['password'])); |
162
|
3 |
|
$newUser->email = $usersInvite->email; |
163
|
3 |
|
$newUser->user_active = 1; |
164
|
3 |
|
$newUser->roles_id = $usersInvite->role_id; |
165
|
3 |
|
$newUser->created_at = date('Y-m-d H:m:s'); |
166
|
3 |
|
$newUser->default_company = $usersInvite->company_id; |
167
|
3 |
|
$newUser->default_company_branch = $usersInvite->company->branch->getId(); |
168
|
|
|
|
169
|
|
|
try { |
170
|
3 |
|
$this->db->begin(); |
171
|
|
|
|
172
|
|
|
//signup |
173
|
3 |
|
$newUser->signup(); |
174
|
|
|
|
175
|
2 |
|
$this->db->commit(); |
176
|
1 |
|
} catch (Exception $e) { |
177
|
1 |
|
$this->db->rollback(); |
178
|
|
|
|
179
|
1 |
|
throw new UnprocessableEntityHttpException($e->getMessage()); |
180
|
|
|
} |
181
|
|
|
|
182
|
2 |
|
return $this->response($newUser); |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|