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 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Sets up invitation information for a would be user |
59
|
|
|
* @return Response |
60
|
|
|
*/ |
61
|
3 |
|
public function insertInvite(): Response |
62
|
|
|
{ |
63
|
3 |
|
$request = $this->request->getPost(); |
64
|
3 |
|
$random = new Random(); |
65
|
|
|
|
66
|
3 |
|
$validation = new Validation(); |
67
|
3 |
|
$validation->add('email', new PresenceOf(['message' => _('The email is required.')])); |
68
|
3 |
|
$validation->add('role', new PresenceOf(['message' => _('The role is required.')])); |
69
|
|
|
|
70
|
|
|
//validate this form for password |
71
|
3 |
|
$messages = $validation->validate($this->request->getPost()); |
72
|
3 |
|
if (count($messages)) { |
73
|
|
|
foreach ($messages as $message) { |
74
|
|
|
throw new ServerErrorHttpException((string)$message); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
//Save data to users_invite table and generate a hash for the invite |
79
|
3 |
|
$userInvite = $this->model; |
80
|
3 |
|
$userInvite->company_id = $this->userData->default_company; |
81
|
3 |
|
$userInvite->app_id = $this->app->getId(); |
82
|
3 |
|
$userInvite->role_id = Roles::getByAppName($request['role'], $this->userData->defaultCompany)->getId(); |
83
|
3 |
|
$userInvite->email = $request['email']; |
84
|
3 |
|
$userInvite->invite_hash = $random->base58(); |
85
|
3 |
|
$userInvite->created_at = date('Y-m-d H:m:s'); |
86
|
|
|
|
87
|
3 |
|
if (!$userInvite->save()) { |
88
|
|
|
throw new UnprocessableEntityHttpException((string) current($userInvite->getMessages())); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
//Fetch email template of user |
92
|
3 |
|
$emailTemplate = EmailTemplates::getByName('users-invite'); |
93
|
|
|
|
94
|
|
|
// Lets send the mail |
95
|
|
|
|
96
|
3 |
|
$invitationUrl = $this->config->app->frontEndUrl . 'user-invite/' . $userInvite->invite_hash; |
97
|
|
|
|
98
|
3 |
|
if (!defined('API_TESTS')) { |
99
|
|
|
$subject = _('You have been invited!'); |
100
|
|
|
$this->mail |
101
|
|
|
->to($userInvite->email) |
102
|
|
|
->subject($subject) |
103
|
|
|
->params($invitationUrl) |
104
|
|
|
->content($emailTemplate->template) |
105
|
|
|
->sendNow(); |
106
|
|
|
} |
107
|
|
|
|
108
|
3 |
|
return $this->response($userInvite); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Add invited user to our system |
113
|
|
|
* @return Response |
114
|
|
|
*/ |
115
|
3 |
|
public function processUserInvite(string $hash): Response |
116
|
|
|
{ |
117
|
3 |
|
$request = $this->request->getPost(); |
118
|
|
|
|
119
|
3 |
|
if (empty($request)) { |
120
|
|
|
$request = $this->request->getJsonRawBody(true); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
//Ok let validate user password |
124
|
3 |
|
$validation = new Validation(); |
125
|
3 |
|
$validation->add('password', new PresenceOf(['message' => _('The password is required.')])); |
126
|
|
|
|
127
|
3 |
|
$validation->add( |
128
|
3 |
|
'password', |
129
|
3 |
|
new StringLength([ |
130
|
3 |
|
'min' => 8, |
131
|
3 |
|
'messageMinimum' => _('Password is too short. Minimum 8 characters.'), |
132
|
|
|
]) |
133
|
|
|
); |
134
|
|
|
|
135
|
|
|
//validate this form for password |
136
|
3 |
|
$messages = $validation->validate($request); |
137
|
3 |
|
if (count($messages)) { |
138
|
|
|
foreach ($messages as $message) { |
139
|
|
|
throw new ServerErrorHttpException((string)$message); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
//Lets find users_invite by hash on our database |
144
|
3 |
|
$usersInvite = $this->model::findFirst([ |
145
|
3 |
|
'conditions' => 'invite_hash = ?0 and is_deleted = 0', |
146
|
3 |
|
'bind' => [$hash] |
147
|
|
|
]); |
148
|
|
|
|
149
|
3 |
|
if (!is_object($usersInvite)) { |
150
|
|
|
throw new NotFoundHttpException('Users Invite not found'); |
151
|
|
|
} |
152
|
|
|
|
153
|
3 |
|
$newUser = new Users(); |
154
|
3 |
|
$newUser->firstname = $request['firstname']; |
155
|
3 |
|
$newUser->lastname = $request['lastname']; |
156
|
3 |
|
$newUser->displayname = $request['displayname']; |
157
|
3 |
|
$newUser->password = ltrim(trim($request['password'])); |
158
|
3 |
|
$newUser->email = $usersInvite->email; |
159
|
3 |
|
$newUser->user_active = 1; |
160
|
3 |
|
$newUser->roles_id = $usersInvite->role_id; |
161
|
3 |
|
$newUser->created_at = date('Y-m-d H:m:s'); |
162
|
3 |
|
$newUser->default_company = $usersInvite->company_id; |
163
|
3 |
|
$newUser->default_company_branch = $usersInvite->company->branch->getId(); |
164
|
|
|
|
165
|
|
|
try { |
166
|
3 |
|
$this->db->begin(); |
167
|
|
|
|
168
|
|
|
//signup |
169
|
3 |
|
$newUser->signup(); |
170
|
|
|
|
171
|
2 |
|
$this->db->commit(); |
172
|
1 |
|
} catch (Exception $e) { |
173
|
1 |
|
$this->db->rollback(); |
174
|
|
|
|
175
|
1 |
|
throw new UnprocessableEntityHttpException($e->getMessage()); |
176
|
|
|
} |
177
|
|
|
|
178
|
2 |
|
return $this->response($newUser); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|