X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/dec0cbb1b25d139c504923a15b4ce884562c7404..refs/pull/438/head:/app/Services/EmailConfirmationService.php diff --git a/app/Services/EmailConfirmationService.php b/app/Services/EmailConfirmationService.php index dd85ad834..8eb52708c 100644 --- a/app/Services/EmailConfirmationService.php +++ b/app/Services/EmailConfirmationService.php @@ -1,30 +1,27 @@ -mailer = $mailer; - $this->emailConfirmation = $emailConfirmation; + $this->db = $db; + $this->users = $users; } /** @@ -35,46 +32,60 @@ class EmailConfirmationService */ public function sendConfirmation(User $user) { - if($user->email_confirmed) { - throw new ConfirmationEmailException('Email has already been confirmed, Try logging in.', '/login'); + if ($user->email_confirmed) { + throw new ConfirmationEmailException(trans('errors.email_already_confirmed'), '/login'); } + $this->deleteConfirmationsByUser($user); + $token = $this->createEmailConfirmation($user); + + $user->notify(new ConfirmEmail($token)); + } + + /** + * Creates a new email confirmation in the database and returns the token. + * @param User $user + * @return string + */ + public function createEmailConfirmation(User $user) + { $token = $this->getToken(); - $this->emailConfirmation->create([ + $this->db->table('email_confirmations')->insert([ 'user_id' => $user->id, - 'token' => $token, + 'token' => $token, + 'created_at' => Carbon::now(), + 'updated_at' => Carbon::now() ]); - $this->mailer->send('emails/email-confirmation', ['token' => $token], function (Message $message) use ($user) { - $appName = \Setting::get('app-name', 'BookStack'); - $message->to($user->email, $user->name)->subject('Confirm your email on ' . $appName . '.'); - }); + return $token; } /** * Gets an email confirmation by looking up the token, * Ensures the token has not expired. * @param string $token - * @return EmailConfirmation + * @return array|null|\stdClass * @throws UserRegistrationException */ public function getEmailConfirmationFromToken($token) { - $emailConfirmation = $this->emailConfirmation->where('token', '=', $token)->first(); - // If not found + $emailConfirmation = $this->db->table('email_confirmations')->where('token', '=', $token)->first(); + + // If not found show error if ($emailConfirmation === null) { - throw new UserRegistrationException('This confirmation token is not valid or has already been used, Please try registering again.', '/register'); + throw new UserRegistrationException(trans('errors.email_confirmation_invalid'), '/register'); } // If more than a day old - if(Carbon::now()->subDay()->gt($emailConfirmation->created_at)) { - $this->sendConfirmation($emailConfirmation->user); - throw new UserRegistrationException('The confirmation token has expired, A new confirmation email has been sent.', '/register/confirm'); + if (Carbon::now()->subDay()->gt(new Carbon($emailConfirmation->created_at))) { + $user = $this->users->getById($emailConfirmation->user_id); + $this->sendConfirmation($user); + throw new UserRegistrationException(trans('errors.email_confirmation_expired'), '/register/confirm'); } + $emailConfirmation->user = $this->users->getById($emailConfirmation->user_id); return $emailConfirmation; } - /** * Delete all email confirmations that belong to a user. * @param User $user @@ -82,7 +93,7 @@ class EmailConfirmationService */ public function deleteConfirmationsByUser(User $user) { - return $this->emailConfirmation->where('user_id', '=', $user->id)->delete(); + return $this->db->table('email_confirmations')->where('user_id', '=', $user->id)->delete(); } /** @@ -92,7 +103,7 @@ class EmailConfirmationService protected function getToken() { $token = str_random(24); - while ($this->emailConfirmation->where('token', '=', $token)->exists()) { + while ($this->db->table('email_confirmations')->where('token', '=', $token)->exists()) { $token = str_random(25); } return $token;