2016-09-04 117 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 –