]> BookStack Code Mirror - bookstack/blob - app/Services/EmailConfirmationService.php
Added auto-suggestions to tag names and values
[bookstack] / app / Services / EmailConfirmationService.php
1 <?php namespace BookStack\Services;
2
3
4 use Carbon\Carbon;
5 use Illuminate\Contracts\Mail\Mailer;
6 use Illuminate\Mail\Message;
7 use BookStack\EmailConfirmation;
8 use BookStack\Exceptions\ConfirmationEmailException;
9 use BookStack\Exceptions\UserRegistrationException;
10 use BookStack\Repos\UserRepo;
11 use BookStack\Setting;
12 use BookStack\User;
13
14 class EmailConfirmationService
15 {
16     protected $mailer;
17     protected $emailConfirmation;
18
19     /**
20      * EmailConfirmationService constructor.
21      * @param Mailer            $mailer
22      * @param EmailConfirmation $emailConfirmation
23      */
24     public function __construct(Mailer $mailer, EmailConfirmation $emailConfirmation)
25     {
26         $this->mailer = $mailer;
27         $this->emailConfirmation = $emailConfirmation;
28     }
29
30     /**
31      * Create new confirmation for a user,
32      * Also removes any existing old ones.
33      * @param User $user
34      * @throws ConfirmationEmailException
35      */
36     public function sendConfirmation(User $user)
37     {
38         if ($user->email_confirmed) {
39             throw new ConfirmationEmailException('Email has already been confirmed, Try logging in.', '/login');
40         }
41         $this->deleteConfirmationsByUser($user);
42         $token = $this->getToken();
43         $this->emailConfirmation->create([
44             'user_id' => $user->id,
45             'token'   => $token,
46         ]);
47         $this->mailer->send('emails/email-confirmation', ['token' => $token], function (Message $message) use ($user) {
48             $appName = setting('app-name', 'BookStack');
49             $message->to($user->email, $user->name)->subject('Confirm your email on ' . $appName . '.');
50         });
51     }
52
53     /**
54      * Gets an email confirmation by looking up the token,
55      * Ensures the token has not expired.
56      * @param string $token
57      * @return EmailConfirmation
58      * @throws UserRegistrationException
59      */
60     public function getEmailConfirmationFromToken($token)
61     {
62         $emailConfirmation = $this->emailConfirmation->where('token', '=', $token)->first();
63         // If not found
64         if ($emailConfirmation === null) {
65             throw new UserRegistrationException('This confirmation token is not valid or has already been used, Please try registering again.', '/register');
66         }
67
68         // If more than a day old
69         if (Carbon::now()->subDay()->gt($emailConfirmation->created_at)) {
70             $this->sendConfirmation($emailConfirmation->user);
71             throw new UserRegistrationException('The confirmation token has expired, A new confirmation email has been sent.', '/register/confirm');
72         }
73
74         return $emailConfirmation;
75     }
76
77
78     /**
79      * Delete all email confirmations that belong to a user.
80      * @param User $user
81      * @return mixed
82      */
83     public function deleteConfirmationsByUser(User $user)
84     {
85         return $this->emailConfirmation->where('user_id', '=', $user->id)->delete();
86     }
87
88     /**
89      * Creates a unique token within the email confirmation database.
90      * @return string
91      */
92     protected function getToken()
93     {
94         $token = str_random(24);
95         while ($this->emailConfirmation->where('token', '=', $token)->exists()) {
96             $token = str_random(25);
97         }
98         return $token;
99     }
100
101
102 }