2014-11-04 23 views
1

搜索了2天后尝试了许多不同的解决方案,但找不到答案。我按照教程(http://roberto.costumero.es/2011/08/15/creando-un-login-simple-con-symfony2/)的步骤来建立一个简单的登录过程,其中我有:登录中的角色未存储在安全上下文的用户

  • 表用户
  • 表的角色

很多他们之间有很多关系(user_rol )。所有的mysql表都被正确地创建了,并且我也有ORM Doctrine Classes。

如果我加载通过手的用户,并加载它们的角色是工作完全正常,但是当我在被识别的用户使用该自动登录

{% block body %} 
{% if error %} 
    <div>{{ error.message }}</div> 
{% endif %} 

<form action="{{ path('login_check') }}" method="post"> 
    <label for="username">Usuario:</label> 
    <input id="username" type="text" name="_username" value="{{ last_username }}" /> 
    <label for="password">Contraseña:</label> 
    <input id="password" type="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" /> 
    #} 

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

登录后但在_profiler说一条警告消息“认证NO”(可能是因为用户没有角色)

所以我做这个代码:

$user = $this->get('security.context')->getToken()->getUser(); 
print_r(count($user->getRoles())) 

而且角色是空的。有任何想法吗?

P.D:我试着覆盖对象的序列化函数并包括角色,但它没有工作。

回答

0

El problema ha sido mezclarlo con or ORM que elige la base de datosdinámicamente。 El panel principal que cambiaba los datos deconfiguraciónpara seleccionar la base de datos dependiendo del subdominio no se vuelve a llamar antes de hacer un login_check,con lo cual consulta a una BBDDerrónea。

Por si a alguien le pasa losolucionéincluyendo un servicio que cambia de BBDDdinámicamenteyllamándoledes la lafunciónboot del Bundle que controla la parte de usuarios logeados。

迪登特鲁德DataBundle \的Util \ Connection.php

public function initRuntimeConnection($company) { 
    // Choose the database 
    $this->database = "$company"; 

    // Get the default connection 
    $connection = $this->container->get(sprintf('doctrine.dbal.%s_connection', 'default')); 
    $connection->close(); 

    $refConn = new \ReflectionObject($connection); 
    $refParams = $refConn->getProperty('_params'); 
    $refParams->setAccessible('public'); //we have to change it for a moment 

    $params = $refParams->getValue($connection); 
    $params['dbname'] = $this->database; 

    $refParams->setAccessible('private'); 
    $refParams->setValue($connection, $params); 
    $this->container->get('doctrine')->resetManager('default'); // for sure (unless you like broken transactions) 
} 

迪登特鲁德PanelBundle \ AcmePanelBundle.php

/** 
* Overwriting the boot call to select the database of the company 
*/ 
public function boot(){ 
    // Get the company attrs from the session. 
    $company = $this->container->get('session')->get('company'); 

    // TODO: Otherwise take it from the subdomain 
    if(!is_array($company)) $company = ''; 

    // Change the database 
    $this->container->get('acme.connection')->initRuntimeConnection($company); 
} 
0

只是几个问题:

  1. 贵用户的实体扩展的Symfony \分量\安全\核心\ User类?
  2. 您的getRoles()是否为每个角色返回一个包含字符串的数组?

也许您需要强制在构造函数中设置用户角色,因为如果您使用另一个实体来管理角色,那么角色对象不是标识角色的字符串数组。

+0

顺便说一句,你按照文章是从2011年,和也许是使用Symfony 2.0,我认为你必须阅读你的版本的官方文档(最新我猜)http://symfony.com/doc/current/book/security.html#using-a-traditional-login-form Symfony有一个非常好的文档。 – mangelsnc 2014-11-04 15:46:10

+0

1。它实现了以下Symfony \ Component \ Security \ Core \ User \ UserInterface 2.是的,我修改了该函数,为每个角色返回一个包含字符串的数组。 – Javier 2014-11-04 16:03:41