2012-05-09 40 views

回答

0

由於本地EntityUserProvider是抽象,它不可能......但是,如果你在你自己的公共服務的定義方法loadUserByUsername和EntityUserProvider調用它,你可以從任何ContainerAware類使用它。

+0

感謝@ PECE! 實際上,我只需要使用安全組件行爲從他的用戶名中檢索用戶。 – Glutamatt

+0

我設置了很多提供商(鏈接)...所以我想保留現有的chainedprovider – Glutamatt

0

AFAIK Symfony2不直接訪問註冊的用戶提供商。一個骯髒的方式是,解析security.ymlload()方法DependencyInjection/BundleExtension.php e.g

//DependencyInjection/BundleExtension.php 
public function load(array $configs, ContainerBuilder $container) 
{ 
    //... 
    //parse security.yml 
    //create an array mapping each provider keys in the firewall to the providers 
    //set the array as global parameter using $container->setParameter('provider.mappings', $array) 
} 

然後在控制器,你可以做

$mapping = $this->container->getParameter('provider.mappings'); 
$providerKey = $this->get('security.context')->getToken()->getProviderKey(); 
$user = $mapping[$providerKey]->loadUserByUsername('user_name'); 
+0

謝謝@ m2mdas,我會試試這個並保持聯繫! – Glutamatt