ProgTeam10
ProgTeam10

Reputation: 1

Unsupported methods in Symfony

I am implementing in my symfony project 2Fa authentication with the help of the official documentation. All good until I try to customize the conditions so that the user is forced to go through the 2Fa form, using the documentation method.

The problem starts when in the function shouldPerformTwoFactorAuthentication I start to implement code as if it were a common function, I try do this:

public function shouldPerformTwoFactorAuthentication(AuthenticationContextInterface $context, ManagerRegistry $doctrine, UserInterface $userInterface): bool
    {
        $entityManager = $doctrine->getManager();
        $user = $entityManager->getRepository(User::class)->findOneBy(['email' => $userInterface->getUserIdentifier()]);
        if ($user->isIsGoogleEnabled()) {
            return false;
        } else {
            return false;
        }
    }

As you can see I try to customize when to make it work and when not, but I get this error:

Method 'App\Controller\TwoFactorConditionController::shouldPerformTwoFactorAuthentication()' is not compatible with method 'Scheb\TwoFactorBundle\Security\TwoFactor\Condition\TwoFactorConditionInterface::shouldPerformTwoFactorAuthentication()'.intelephense(1038)

I've been searching the internet with no results that work for me, until i found this.

I have made so many attempts at this that I will not post them, if someone can guide me, I appreciate your time, thanks!

Upvotes: 0

Views: 83

Answers (1)

Bhavin Nakrani
Bhavin Nakrani

Reputation: 532

This will work using __construct() method

private EntityManagerInterface $entityManager;
private UserInterface $userInterface;

public function __construct(EntityManagerInterface $entityManager, UserInterface $userInterface)
{
  $this->entityManager = $entityManager;
  $this->userInterface = $userInterface;
}

public function shouldPerformTwoFactorAuthentication(AuthenticationContextInterface $context): bool
{        
  $user = $this->entityManager->getRepository(User::class)->findOneBy(['email' => $this->userInterface->getUserIdentifier()]);
  if ($user->isIsGoogleEnabled()) {
    return false;
  } else {
    return false;
  }
}

Upvotes: 0

Related Questions