2017-02-27 17 views
2

Symfonyのドキュメントごとにすべてを実装しようとしましたが、認証がまったく機能しないようです。私は正確に何をしたかを見てみましょう:symfony3カスタムユーザプロバイダが動作しない

のsecurity.yml

security: 
    encoders: 
     AppBundle\Entity\StUser: 
      algorithm: bcrypt 
      cost: 12 

    providers: 
     our_db_provider: 
      entity: 
       class: AppBundle:Entity:StUser 

    firewalls: 
     user_secured_area: 
      pattern: ^/([a-z]{2})/account 
      provider: our_db_provider 
      form_login: 
       login_path: login 
       check_path: login_check 
       csrf_token_generator: security.csrf.token_manager 
     default: 
      anonymous: ~ 
      http_basic: ~ 

StUser.phpエンティティ

namespace AppBundle\Entity; 

class StUser implements UserInterface 
{ 
    private $id; 
    private $firstName; 
    private $lastName; 
    private $password; 
    private $username; 
    private $isAdmin = '0'; 
    private $confirmed; 
    private $created = 'CURRENT_TIMESTAMP'; 
    private $status = '1'; 

    public function getId() 
    { 
     return $this->id; 
    } 

    public function setFirstName($firstName) 
    { 
     $this->firstName = $firstName; 

     return $this; 
    } 

    public function getFirstName() 
    { 
     return $this->firstName; 
    } 

    public function setLastName($lastName) 
    { 
     $this->lastName = $lastName; 

     return $this; 
    } 

    public function getLastName() 
    { 
     return $this->lastName; 
    } 

    public function setPassword($password) 
    { 
     $this->password = $password; 

     return $this; 
    } 

    public function getPassword() 
    { 
     return $this->password; 
    } 

    public function setUsername($username) 
    { 
     $this->username = $username; 

     return $this; 
    } 

    public function getUsername() 
    { 
     return $this->username; 
    } 

    public function setIsAdmin($isAdmin) 
    { 
     $this->isAdmin = $isAdmin; 

     return $this; 
    } 

    public function getIsAdmin() 
    { 
     return $this->isAdmin; 
    } 

    public function setConfirmed($confirmed) 
    { 
     $this->confirmed = $confirmed; 

     return $this; 
    } 

    public function getConfirmed() 
    { 
     return $this->confirmed; 
    } 

    public function setCreated($created) 
    { 
     $this->created = $created; 

     return $this; 
    } 

    public function getCreated() 
    { 
     return $this->created; 
    } 

    public function setStatus($status) 
    { 
     $this->status = $status; 

     return $this; 
    } 

    public function getStatus() 
    { 
     return $this->status; 
    } 

    /* ==== Additional =================================================== */ 
    public function __construct($username, $password, $salt, array $roles) 
    { 
     $this->username = $username; 
     $this->password = $password; 
     /* $this->salt = $salt; */ 
     /* $this->roles = $roles; */ 
    } 

    public function getRoles() 
    { 
     return null; 
    } 

    public function getSalt() 
    { 
     return null; 
    } 

    public function eraseCredentials() 
    { 

    } 
} 

UserRepository.phpリポジトリ:

namespace AppBundle\Repository; 

use Symfony\Bridge\Doctrine\Security\User\UserLoaderInterface; 
use Doctrine\ORM\EntityRepository; 

class UserRepository extends EntityRepository implements UserLoaderInterface 
{ 
    public function loadUserByUsername($username) 
    { 
     return $this->createQueryBuilder('u') 
      ->where('u.username = :username OR u.email = :email') 
      ->setParameter('username', $username) 
      ->setParameter('email', $username) 
      ->getQuery() 
      ->getOneOrNullResult(); 
    } 
} 

AccountController.php

{% block body %} 
    <form action="{{ path('login', {'_lang': lang}) }}" method="post"> 
     <label for="username">Username:</label> 
     <input type="text" id="username" name="_username" value="" /> 

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

     <input type="hidden" name="_csrf_token" value="{{ csrf_token('authenticate') }}"> 

     <button type="submit">login</button> 
    </form> 
{% endblock %} 

、何が起こる

namespace AccountBundle\Controller; 

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; 
use Symfony\Bundle\FrameworkBundle\Controller\Controller; 
use Symfony\Component\HttpFoundation\Request; 

class AccountController extends Controller 
{ 
    /** 
    * @Route("{_lang}/login", name="login", requirements={"_lang": "pl"}) 
    */ 
    public function loginAction(Request $request, $_lang = '') 
    { 
     $helper = $this->get('security.authentication_utils'); 
     $error = $helper->getLastAuthenticationError(); 

     var_dump($error); 

     return $this->render('account/login.html.twig', array(
      'projects' => "", 
      'lang' => "pl", 
      'allLangs' => "", 
      'mainLang' => "", 
      'meta_title' => "test", 
      'meta_description' => "", 
      'meta_keywords' => "", 
      'meta_robots' => "", 
      'image_src' => "", 
      'social_title' => "", 
      'social_description' => "", 
      'social_url' => "", 
      'aaaa' => $helper, 

      /* 'last_username' => $lastUsername, 
      'error'   => $error, */ 
     )); 
    } 
} 

login.html.twigビューは、$ヘルパーと$エラーは空/ nullに、私はログで見つけることができる何もないです。私が逃したもの前もって感謝します。

デフォルト値を持つ 匿名キーを追加し、キー プロバイダを修正する必要があり、あなたのsecurity.ymlで

答えて

0

私はそれを試みましたが、成功しませんでした。任意のカスタムコードが呼び出されないようです。

私がsecurity.ymlファイル更新しました(私は匿名に設定する場合:〜、それは、ユーザーが認証なしexamppleアカウントページに開くことができます):

security: 
    encoders: 
     AppBundle\Entity\StUser: 
      algorithm: bcrypt 
      cost: 12 

    providers: 
     our_db_provider: 
      entity: 
       class: AppBundle:StUser 

    firewalls: 
     user_secured_area: 
      pattern: ^/([a-z]{2})/account 
      # anonymous: ~ 
      provider: our_db_provider 
      form_login: 
       login_path: login 
       check_path: login_check 
       csrf_token_generator: security.csrf.token_manager 
     default: 
      anonymous: ~ 
      http_basic: ~ 

をしても、私はUserエンティティ更新しました:

StUser.orm。YMLエンティティ:あなたはログインしてパスをlogin_checkファイアウォールをbehingしているので、あなたがそれを置く必要があり、あなたのsecurity.ymlで**匿名**キーについて

AppBundle\Entity\StUser: 
    type: entity 
    table: st_user 
    repositoryClass: AppBundle\Repository\UserRepository 
    indexes: 
     status_username_password: 
      columns: 
       - status 
       - username 
       - password 
     status_is_admin_username_password: 
      columns: 
       - status 
       - is_admin 
       - username 
       - password 
    id: 
     id: 
      type: integer 
      nullable: false 
      options: 
       unsigned: false 
      id: true 
      generator: 
       strategy: IDENTITY 
    fields: 
     firstName: 
      type: string 
      nullable: true 
      length: 255 
      options: 
       fixed: false 
      column: first_name 
     lastName: 
      type: string 
      nullable: true 
      length: 255 
      options: 
       fixed: false 
      column: last_name 
     password: 
      type: string 
      nullable: true 
      length: 255 
      options: 
       fixed: false 
     username: 
      type: string 
      nullable: true 
      length: 255 
      options: 
       fixed: false 
     isAdmin: 
      type: boolean 
      nullable: false 
      options: 
       default: '0' 
      column: is_admin 
     confirmed: 
      type: datetime 
      nullable: true 
     created: 
      type: datetime 
      nullable: false 
      options: 
       default: CURRENT_TIMESTAMP 
     status: 
      type: boolean 
      nullable: false 
      options: 
       default: '0' 
    lifecycleCallbacks: { } 
+0

。匿名ユーザーへのアクセスを拒否した場合、ユーザーはどのようにログインページにアクセスしてログインできますか?次に**注釈やACLのような異なる戦略を使って** account **パスを確保するのはあなたの仕事です。 –

+0

また、** login_check **の代わりにcheck_pathに** login **を入れてみてください。 –

+0

そして、私はあなたのログインパス名を**に設定しているので、あなたのファイアウォール設定で**パターン**キーは必要ないと思いますログイン**は既にパターンと要件を持っています。ログインパスは '^ /([a-z] {2})/ account'のファイアウォールのパターンと一致しません。このパターンを使用せず、アカウントルートをaclまたは注釈で保護してください –

0

のsecurity.yml

security: 
    encoders: 
     AppBundle\Entity\StUser: 
      algorithm: bcrypt 
      cost: 12 
    providers: 
     our_db_provider: 
      entity: 
       class: AppBundle:StUser # not necessary to put 'Entity' here if your entities are in the Entity Folder 
    firewalls: 
     user_secured_area: 
      anonymous: ~ 
      pattern: ^/([a-z]{2})/account 
      provider: our_db_provider 
      form_login: 
       login_path: login 
       check_path: login_check 
       csrf_token_generator: security.csrf.token_manager 

あなたAccountControllerの次の、ファイルなぜ特別なルーティングパラメータ_localeを使用しないのですか?これを読んでください:Special Routing Parameters

その後、あなたは、このようなlocaleパラメータを使用してルートを接頭辞:

AccountController.php

namespace AccountBundle\Controller; 

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; 
use Symfony\Bundle\FrameworkBundle\Controller\Controller; 
use Symfony\Component\HttpFoundation\Request; 

class AccountController extends Controller 
{ 
    /** 
    * @Route("/{_locale}/login", name="login", requirements={"_locale": "pl"}) 
    */ 
    public function loginAction(Request $request, $_lang = '') 
    { 
     $helper = $this->get('security.authentication_utils'); 
     $error = $helper->getLastAuthenticationError(); 

     var_dump($error); 

     return $this->render('account/login.html.twig', array(
      'projects' => "", 
      'lang' => "pl", 
      'allLangs' => "", 
      'mainLang' => "", 
      'meta_title' => "test", 
      'meta_description' => "", 
      'meta_keywords' => "", 
      'meta_robots' => "", 
      'image_src' => "", 
      'social_title' => "", 
      'social_description' => "", 
      'social_url' => "", 
      'aaaa' => $helper, 

      /* 'last_username' => $lastUsername, 
      'error'   => $error, */ 
     )); 
    } 
} 

私はあなたが使用しているエンティティのマッピング戦略を知りません。アノテーションまたはxml? (ドキュメントから)この上を見てみましょう:

mapping definition of your entityにリポジトリクラスを追加することを忘れないでください。

関連する問題