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