Метод checkIfComplete класса Passport
Метод checkIfComplete класса Passport
проверяет, завершён ли паспорт аутентификации.
Метод возвращает true, если паспорт готов
к передаче пользователя в систему безопасности,
и false в противном случае. Параметров метод
не принимает. Обычно вызывается внутри
аутентификатора для проверки возможности
завершения процесса входа.
Синтаксис
public function checkIfComplete(): bool
Пример
Давайте создадим паспорт и проверим его завершённость:
<?php
namespace AppSecurity;
use SymfonyComponentSecurityHttpAuthenticatorPassportPassport;
use SymfonyComponentSecurityHttpAuthenticatorPassportBadgeUserBadge;
$passport = new Passport(new UserBadge('abcde'));
$res = $passport->checkIfComplete();
var_dump($res);
?>
Результат выполнения кода:
false
Пример
Давайте добавим бейдж и снова проверим завершённость паспорта:
<?php
namespace AppSecurity;
use SymfonyComponentSecurityHttpAuthenticatorPassportPassport;
use SymfonyComponentSecurityHttpAuthenticatorPassportBadgeUserBadge;
use SymfonyComponentSecurityHttpAuthenticatorPassportBadgeRememberMeBadge;
$passport = new Passport(new UserBadge('abcde'));
$passport->addBadge(new RememberMeBadge());
$res = $passport->checkIfComplete();
var_dump($res);
?>
Результат выполнения кода:
true
Пример
Давайте используем метод checkIfComplete
внутри аутентификатора для проверки паспорта:
<?php
namespace AppSecurity;
use SymfonyComponentHttpFoundationRequest;
use SymfonyComponentSecurityCoreExceptionAuthenticationException;
use SymfonyComponentSecurityHttpAuthenticatorAuthenticatorInterface;
use SymfonyComponentSecurityHttpAuthenticatorPassportPassport;
use SymfonyComponentSecurityHttpAuthenticatorPassportBadgeUserBadge;
use SymfonyComponentSecurityHttpAuthenticatorTokenTokenInterface;
class ApiAuthenticator implements AuthenticatorInterface
{
public function authenticate(Request $request): Passport
{
$passport = new Passport(new UserBadge('abcde'));
if (!$passport->checkIfComplete()) {
throw new AuthenticationException('Passport is not complete');
}
return $passport;
}
public function createToken(Passport $passport, string $firewallName): TokenInterface
{
throw new AuthenticationException('Not implemented');
}
}
?>
Результат выполнения кода при неполном паспорте:
"Passport is not complete"
Смотрите также
-
класс
Passport,
который представляет паспорт аутентификации -
метод
addBadge,
который добавляет бейдж в паспорт -
метод
hasBadge,
который проверяет наличие бейджа в паспорте -
класс
SelfValidatingPassport,
который всегда считается завершённым