如果未在Symfony上确认用户,则拒绝访问

时间:2018-10-17 15:56:45

标签: php mysql symfony

我发现很难弄清楚如何拒绝某些在数据库中将is_active设置为false的用户访问网站。如何修改当前的LoginController来检查数据库中的值并抛出一些AccessDenied异常?

下面是LoginController.php

namespace App\Controller\User;

use App\Form\User\LoginType;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Form\FormError;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Twig\Environment;

/**
 * @Route("/login", name="login")
 */
final class LoginController extends Controller
{
    public function __invoke(
        Environment $twig,
        FormFactoryInterface $formFactory,
        AuthenticationUtils $authenticationUtils
    ): Response {
        if ($this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY')) {
            return new RedirectResponse('/profile');
        }

        $form = $formFactory->createNamed('', LoginType::class, [
            'email' => $authenticationUtils->getLastUsername(),
        ]);

        if (null !== $error = $authenticationUtils->getLastAuthenticationError(true)) {
            $form->addError(new FormError($error->getMessage(), $error->getMessageKey(), $error->getMessageData()));
        }

        return new Response($twig->render('user/login.html.twig', [
            'form' => $form->createView(),
        ]));
    }
}

我的数据库有一个表“ user”:

+----+---------+----------------+-----------+------------+--------------------------+--------------------------+------------+-----------+-----------+-----------+------------+----------------------+-------------------------------------+--------------------------------------------------------------+----------------------+-----------------------+------------------------------------------------------------------+--------------+
| id | type_id | upline_user_id | is_active | trainer_id | upline_token             | upline_user_token        | first_name | last_name | file_name | file_size | updated_at | unread_notifications | credential_email                    | credential_password                                          | password_reset_token | password_requested_at | confirmation_token                                               | confirmed_at |
+----+---------+----------------+-----------+------------+--------------------------+--------------------------+------------+-----------+-----------+-----------+------------+----------------------+-------------------------------------+--------------------------------------------------------------+----------------------+-----------------------+------------------------------------------------------------------+--------------+

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

您正在检查用户是否已完全通过身份验证,然后将其重定向到配置文件。在那里,您还可以检查用户是否处于活动状态,然后如果不是则抛出异常。我假设您的用户实体具有isActive()或getActive()吸气剂。

    if ($this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY')) {
        if (!$this->getUser()->isActive()) {
            throw new HttpException(403, 'Access denied.');
        } else {
            return new RedirectResponse('/profile');
        }
    }