1 <?php namespace BookStack\Auth\Access;
3 use BookStack\Auth\User;
4 use BookStack\Auth\UserRepo;
5 use BookStack\Exceptions\ConfirmationEmailException;
6 use BookStack\Exceptions\UserRegistrationException;
7 use BookStack\Notifications\ConfirmEmail;
9 use Illuminate\Database\Connection as Database;
11 class EmailConfirmationService
17 * EmailConfirmationService constructor.
19 * @param \BookStack\Auth\UserRepo $users
21 public function __construct(Database $db, UserRepo $users)
24 $this->users = $users;
28 * Create new confirmation for a user,
29 * Also removes any existing old ones.
30 * @param \BookStack\Auth\User $user
31 * @throws ConfirmationEmailException
33 public function sendConfirmation(User $user)
35 if ($user->email_confirmed) {
36 throw new ConfirmationEmailException(trans('errors.email_already_confirmed'), '/login');
39 $this->deleteConfirmationsByUser($user);
40 $token = $this->createEmailConfirmation($user);
42 $user->notify(new ConfirmEmail($token));
46 * Creates a new email confirmation in the database and returns the token.
50 public function createEmailConfirmation(User $user)
52 $token = $this->getToken();
53 $this->db->table('email_confirmations')->insert([
54 'user_id' => $user->id,
56 'created_at' => Carbon::now(),
57 'updated_at' => Carbon::now()
63 * Gets an email confirmation by looking up the token,
64 * Ensures the token has not expired.
65 * @param string $token
66 * @return array|null|\stdClass
67 * @throws UserRegistrationException
69 public function getEmailConfirmationFromToken($token)
71 $emailConfirmation = $this->db->table('email_confirmations')->where('token', '=', $token)->first();
73 // If not found show error
74 if ($emailConfirmation === null) {
75 throw new UserRegistrationException(trans('errors.email_confirmation_invalid'), '/register');
78 // If more than a day old
79 if (Carbon::now()->subDay()->gt(new Carbon($emailConfirmation->created_at))) {
80 $user = $this->users->getById($emailConfirmation->user_id);
81 $this->sendConfirmation($user);
82 throw new UserRegistrationException(trans('errors.email_confirmation_expired'), '/register/confirm');
85 $emailConfirmation->user = $this->users->getById($emailConfirmation->user_id);
86 return $emailConfirmation;
90 * Delete all email confirmations that belong to a user.
91 * @param \BookStack\Auth\User $user
94 public function deleteConfirmationsByUser(User $user)
96 return $this->db->table('email_confirmations')->where('user_id', '=', $user->id)->delete();
100 * Creates a unique token within the email confirmation database.
103 protected function getToken()
105 $token = str_random(24);
106 while ($this->db->table('email_confirmations')->where('token', '=', $token)->exists()) {
107 $token = str_random(25);