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