BookStack/app/Access/EmailConfirmationService.php

41 lines
1.1 KiB
PHP
Raw Normal View History

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