2021-06-26 11:23:15 -04:00
|
|
|
<?php
|
|
|
|
|
2023-05-17 12:56:55 -04:00
|
|
|
namespace BookStack\Access;
|
2015-09-05 15:25:57 -04:00
|
|
|
|
2015-09-10 14:31:09 -04:00
|
|
|
use BookStack\Exceptions\ConfirmationEmailException;
|
2018-09-25 11:58:03 -04:00
|
|
|
use BookStack\Notifications\ConfirmEmail;
|
2023-05-17 12:56:55 -04:00
|
|
|
use BookStack\Users\Models\User;
|
2015-09-05 15:25:57 -04:00
|
|
|
|
2019-08-17 10:52:33 -04:00
|
|
|
class EmailConfirmationService extends UserTokenService
|
2015-09-05 15:25:57 -04:00
|
|
|
{
|
2023-04-04 05:44:38 -04:00
|
|
|
protected string $tokenTable = 'email_confirmations';
|
|
|
|
protected int $expiryTime = 24;
|
2015-09-05 15:25:57 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create new confirmation for a user,
|
|
|
|
* Also removes any existing old ones.
|
2021-08-21 10:49:40 -04:00
|
|
|
*
|
2015-09-05 15:25:57 -04:00
|
|
|
* @throws ConfirmationEmailException
|
|
|
|
*/
|
|
|
|
public function sendConfirmation(User $user)
|
|
|
|
{
|
2015-09-06 07:14:32 -04:00
|
|
|
if ($user->email_confirmed) {
|
2016-12-04 11:51:39 -05:00
|
|
|
throw new ConfirmationEmailException(trans('errors.email_already_confirmed'), '/login');
|
2015-09-05 15:25:57 -04:00
|
|
|
}
|
2016-09-17 13:22:04 -04:00
|
|
|
|
2019-08-17 10:52:33 -04:00
|
|
|
$this->deleteByUser($user);
|
|
|
|
$token = $this->createTokenForUser($user);
|
2016-09-17 13:22:04 -04:00
|
|
|
|
|
|
|
$user->notify(new ConfirmEmail($token));
|
|
|
|
}
|
|
|
|
|
2019-08-18 05:47:59 -04:00
|
|
|
/**
|
|
|
|
* Check if confirmation is required in this instance.
|
|
|
|
*/
|
2021-06-26 11:23:15 -04:00
|
|
|
public function confirmationRequired(): bool
|
2019-08-18 05:47:59 -04:00
|
|
|
{
|
|
|
|
return setting('registration-confirmation')
|
|
|
|
|| setting('registration-restrict');
|
|
|
|
}
|
2018-01-28 11:58:52 -05:00
|
|
|
}
|