1

userBundle と xxxBundle の 2 つのバンドルがあります。ユーザーバンドルでユーザーを認証した後、それをxxxBundleにリダイレクトしたい。ただし、ロール (ROLE_ADMIN と ROLE_USER) に応じて、2 つの異なるルート (route1、route2) にリダイレクトします。

このコントローラーを userBundle に追加しました

class SecurityController extends Controller
{
  public function loginAction()
  {
    if ($this->get('security.context')->isGranted('ROLE_ADMIN')) {
      return $this->redirect($this->generateUrl('route1'));

    }
if ($this->get('security.context')->isGranted('ROLE_USER')) {
      return $this->redirect($this->generateUrl('route2'));

    }

    $request = $this->getRequest();
    $session = $request->getSession();

    // On vérifie s'il y a des erreurs d'une précédent soumission du formulaire
    if ($request->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) {
      $error = $request->attributes->get(SecurityContext::AUTHENTICATION_ERROR);
    } else {
      $error = $session->get(SecurityContext::AUTHENTICATION_ERROR);
      $session->remove(SecurityContext::AUTHENTICATION_ERROR);
    }

    return $this->render('UserBundle:Security:login.html.twig', array(
      // Valeur du précédent nom d'utilisateur rentré par l'internaute
      'last_username' => $session->get(SecurityContext::LAST_USERNAME),
      'error'         => $error,
    ));
  }

しかし、これは適切な結果をもたらしません: 正しいユーザー名とパスワードの場合、ユーザーはウェルカム symfony ページにリダイレクトされます。誰かがそれについて説明していますか?

次のように隠しフィールドを使用して、ログインフォームからのリダイレクトを制御できることを symfony のドキュメントで見つけました。

{# src/Acme/SecurityBundle/Resources/views/Security/login.html.twig #}
{% if error %}
    <div>{{ error.message }}</div>
{% endif %}

<form action="{{ path('login_check') }}" method="post">
    <label for="username">Username:</label>
    <input type="text" id="username" name="_username" value="{{ last_username }}" />

    <label for="password">Password:</label>
    <input type="password" id="password" name="_password" />

    <input type="hidden" name="_target_path" value="account" />

    <input type="submit" name="login" />
</form>

質問: ユーザーと管理者を担当するルートをどのようにパラメータ化できますか?

4

1 に答える 1