0

Im using symfony 6 and easyadmin 4. I'm trying to figure out how to block a user account on my website but I can't find a solution. I tried to:

  1. Store users status in a boolean column (able to connect or not)
  public function isIsEnabled(): ?bool
    {
        return $this->isEnabled;
    }

    public function setIsEnabled(bool $isEnabled): self
    {
        $this->isEnabled = $isEnabled;

        return $this;
    }

  1. Prevent the user from logging in in SecureController
    public function login(AuthenticationUtils $authenticationUtils): Response
    {
        // if ($this->getUser()) {
        //     return $this->redirectToRoute('target_path');
        // }
        $user = new User();
        if (!$user->isIsEnabled()) {
            // fail authentication with a custom error
            throw new CustomUserMessageAuthenticationException('Account is disabled.');
        }
        
        // get the login error if there is one
        $error = $authenticationUtils->getLastAuthenticationError();
        // last username entered by the user
        $lastUsername = $authenticationUtils->getLastUsername();

        return $this->render('security/login.html.twig', ['last_username' => $lastUsername, 'error' => $error]);
    }

    #[Route(path: '/logout', name: 'app_logout')]
    public function logout(): void
    {
        throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
    }
}
  1. Be able to disable a user with EasyAdmin by using a boolean field

But yet the User can still connect and I don't know why? I think it's the $user = new User(); line but I don't know how to use the isIsEnabled() method in another file without using the User entity. Can someone help, please? thanks in advance.

gp_sflover
  • 3,460
  • 5
  • 38
  • 48
yaboiii23
  • 29
  • 5
  • 3
    Maybe you need this https://symfony.com/doc/current/security/user_checkers.html – hous Nov 08 '22 at 14:40
  • Oddly similar to: https://stackoverflow.com/questions/74300816/how-can-i-block-a-user-account-in-symfony-6 . Just for info there is no need to `bump` questions on stackoverflow and doing so is frowned upon. Engaging with people trying to help works better. There are also other more discussion oriented Symfony forums which might be better at walking you through the very basics. – Cerad Nov 08 '22 at 14:47
  • yes sorry I thought asking a more specific question with more specific details might help – yaboiii23 Nov 08 '22 at 14:54
  • That is okay. Live and learn. As @hous commented, a user checker is what you want. – Cerad Nov 08 '22 at 14:57
  • @yaboiii23 if you found a solution put it in the answer section and not inside the question. – gp_sflover Nov 09 '22 at 14:19

0 Answers0