protected function inviteUser() { if ($this->input('email')) { if ($this->currentUser->getRole() !== UserRole::TYPE_OWNER) { $this->setError(lang('You dont have permissions to invite users')); response()->refresh(); } $this->post->email->addValidation([new ValidateInputNotNullOrEmpty(), new ValidateInputEmail()]); $this->post->role->addValidation(new ValidateInputNotNullOrEmpty()); if (!$this->hasErrors()) { $user = ModelUser::getByUsername($this->input('email')); if ($user->hasRow() && $user->hasAccess($this->activeOrganisation->id)) { if ($user->getRole() === $this->input('role')) { $this->setMessage(lang('The user already has access to this organisation'), 'danger'); } else { $user->setRole($this->input('role')); $this->setMessage(lang('The role has been updated'), 'success'); // TODO: sent mail notifying about role-change } response()->refresh(); } // Save invitation $invitation = new OrganisationInvite(); $invitation->user_id = $this->currentUser->id; $invitation->email = $this->input('email'); $invitation->organisation_id = $this->activeOrganisation->id; $invitation->role = $this->input('role'); $invitation->save(); // This point we send out a confirmation mail to accept the organisation invite. // TODO: move this shit to separate template $transport = \Swift_SendmailTransport::newInstance(env('MAIL_TRANSPORT') . ' -bs'); $swift = \Swift_Mailer::newInstance($transport); $message = new \Swift_Message(lang('Invite to join ' . $this->activeOrganisation->name . ' on NinjaImg')); $message->setFrom(env('MAIL_FROM')); $message->setSender(env('MAIL_FROM')); $message->setReplyTo(env('MAIL_FROM')); $message->setBody("Dear customer!\n\n{$this->currentUser->data->name} has invited you to join the organisation {$this->activeOrganisation->name} on NinjaImg!\n\nClick on the link below to accept the invite:\nhttps://{$_SERVER['HTTP_HOST']}" . url('user.register') . "?email=" . $this->input('email') . "\n\nIf you have any questions, feel free to contact us any time.\n\nKind regards,\nThe NinjaImg Team", 'text/plain'); $message->setTo($this->input('email')); $swift->send($message); $this->setMessage('An invite has been sent to the user.', 'success'); } } }
public function __construct() { parent::__construct(); $this->prependSiteTitle(lang('Register')); $this->currency = ModelSettings::getInstance(); $role = UserRole::TYPE_OWNER; if ($this->get->email->getValue()) { $this->invite = OrganisationInvite::getByEmail($this->get->email->getValue()); if ($this->invite->hasRow()) { $this->organisation = ModelOrganisation::getById($this->invite->organisation_id); $role = $this->invite->role; } } if ($this->isPostBack()) { $this->post->name->addValidation(new ValidateInputNotNullOrEmpty()); $this->post->email->addValidation(new ValidateInputEmail()); if ($this->organisation === null || !$this->organisation->hasRow()) { $this->post->company->addValidation(new ValidateInputNotNullOrEmpty()); } $this->post->password->addValidation(new ValidateInputNotNullOrEmpty()); $this->post->terms->addValidation(new ValidateInputNotNullOrEmpty()); $this->post->password_repeat->addValidation(new ValidateInputNotNullOrEmpty()); if (!$this->hasErrors()) { // Do not create organisation if invite is available if ($this->organisation === null || !$this->organisation->hasRow()) { // Check if organisation name is taken if (ModelOrganisation::getByName($this->input('company'))->hasRow()) { $this->setError(lang('An organisation with the name %s already exists - please request an invite from the organisation owner instead.', $this->input('company'))); response()->refresh(); } $organisation = new ModelOrganisation(); $organisation->setName($this->input('company')); $organisation->save(); $role = UserRole::TYPE_OWNER; } else { $organisation = $this->organisation; } $user = new ModelUser(); $user->username = $this->post->email->getValue(); $user->data->name = $this->input('name'); $user->setPassword($this->input('password')); try { $user->save(); $this->sendWelcomeMail($user); } catch (UserException $e) { if ($e->getCode() === ModelUser::ERROR_TYPE_EXISTS) { $error = lang('The e-mail is already registered.'); } else { $error = $e->getMessage(); } $this->setError($error); response()->refresh(); } $user->addOrganisation($organisation, $role); $user->auth(); // Delete invite if ($this->invite && $this->invite->hasRow()) { $this->invitationAcceptedMail($this->invite, $organisation, $user); $this->invite->delete(); } redirect(url('controlpanel.organisations')); } } }