<?php
namespace App\Security;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Security\Core\Exception\AccountExpiredException;
use Symfony\Component\Security\Core\Exception\CustomUserMessageAccountStatusException;
use Symfony\Component\Security\Core\Exception\DisabledException;
use Symfony\Component\Security\Core\User\UserCheckerInterface;
use Symfony\Component\Security\Core\User\UserInterface;
class OauthUserChecker implements UserCheckerInterface
{
private $requestStack;
public function __construct(RequestStack $requestStack)
{
$this->requestStack = $requestStack;
}
public function checkPreAuth(UserInterface $user)
{
if (!$user->isEnabled()) {
$ex = new CustomUserMessageAccountStatusException("Account is disabled.");
$ex->setUser($user);
throw $ex;
}
}
public function checkPostAuth(UserInterface $user)
{
if(
is_array($user->getWhitelistedIps()) &&
sizeof($user->getWhitelistedIps()) &&
!in_array($this->requestStack->getCurrentRequest()->getClientIp(), $user->getWhitelistedIps())
) {
// throw new CustomUserMessageAccountStatusException('User Inactive');
$ex = new CustomUserMessageAccountStatusException("IP address not authorised for access.");
$ex->setUser($user);
throw $ex;
}
}
}