2016-09-04 134 views
1

我想連接我的管理員,當我訪問/管理員或與登錄表單。Symfony登錄 - 用戶/管理

但有些事情是錯的,我無法訪問ROLE_ADMIN。

(一切都很好與ROLE_USER,也許我錯過了聯繫出頭?)

還有的security.yml文件:

安全:

providers: 

    our_db_provider: 
       entity: 
        class: WebAwardsBundle:User 
        property: username 
        # if you're using multiple entity managers 
        # manager_name: customer 
    in_memory: 
     memory: 
      users: 
       admin: 
        password: $2y$13$aabu98fd.l60phldkU.WAeDwgzqiv1IcaF.EndURJuAhGllFgzTv. 
        roles: 'ROLE_ADMIN' 

encoders: 
     Symfony\Component\Security\Core\User\User: bcrypt 
     WebAwardsBundle\Entity\User: 
        algorithm: bcrypt 
firewalls: 

    # disables authentication for assets and the profiler, adapt it according to your needs 
    dev: 
     pattern: ^/(_(profiler|wdt)|css|images|js)/ 
     security: false 

    main: 
     anonymous: ~ 
     #http_basic: ~ 
     #pattern: ^/ 
     #provider: our_db_provider 
     form_login: 
      login_path: login 
      check_path: login 


     # Log out user 
     logout: 
      path: /logout 
      target:/

     # activate different ways to authenticate 

     # http_basic: ~ 
     # http://symfony.com/doc/current/book/security.html#a-configuring-how-your-users-will-authenticate 

     # form_login: ~ 
     # http://symfony.com/doc/current/cookbook/security/form_login_setup.html 
access_control: 
    - { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY } 
    - { path: ^/admin, roles: ROLE_ADMIN } 

還有的SecurityController.php文件:

class SecurityController extends Controller 
/** 
* @Route("/login", name="login") 
*/ 
public function loginAction(Request $request) 
{ 
    $authenticationUtils = $this->get('security.authentication_utils'); 

    // get the login error if there is one 
    $error = $authenticationUtils->getLastAuthenticationError(); 

    // last username entered by the user 
    $lastUsername = $authenticationUtils->getLastUsername(); 

    return $this->render(
     'login.html.twig', 
     array(
      // last username entered by the user 
      'last_username' => $lastUsername, 
      'error'   => $error, 
     ) 
    ); 
} 

/** 
* @Route("/admin", name="admin_action") 
*/ 
public function adminAction() 
{ 
    return new Response('<html><body>Admin page!</body></html>'); 
}} 

而這是login.htm.twig文件:

{% if error %} 
    <div>{{ error.messageKey|trans(error.messageData, 'security') }}</div> 
{% endif %} 
<div>CONNECTEZ-VOUS</div> 
<form action="{{ path('login') }}" 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" /> 
    {# 
     If you want to control the URL the user 
     is redirected to on success (more details below) 
     <input type="hidden" name="_target_path" value="/account" /> 
    #} 
    <button type="submit">login</button> 
</form> 

回答

1

如果你想使用一個以上的供應商,你需要在鏈配置它們

security: 
    providers: 
     chain_provider: 
      chain: 
       providers: [our_db_provider, in_memory] 

你可以閱讀有關的多個供應商here

+0

是的!感謝那 ! @Vladimir –