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