2016-12-30 48 views
1

我有注释* @Security("is_granted('PERM_MODULE_OUTBOUND_INVOICES_READ')")控制器和我写的测试在此控制器一些行动,创建用户和loginIn,而当这个行动呼吁狂胜有错误Symfony的安全性被授予

Expression "is_granted('PERM_MODULE_OUTBOUND_INVOICES_READ')" denied access. 

时添加角色到用户PERM_MODULE_OUTBOUND_INVOICES_READ仍然拒绝访问

评论TGIS和行动检查当前用户,当被授予了true

/** 
* @Route("/manage/new_outbound_invoices", name="new_outbound_invoices") 
*/ 
public function outBoundInvoiceListsAction(Request $request) 
{ 
     $check = $this->get('security.authorization_checker') 
     ->isGranted('PERM_MODULE_OUTBOUND_INVOICES_READ', $this->getUser()); 

但安全注解拒绝访问,为什么不明白 这是我的测试

 $user = $this->user; 
    $this->logIn($user); 
    //$t = $this->getContainer()->get('security.context')->getToken(); try get token and have null, but in action have user from session 
    $this->client->setServerParameter('HTTP_HOST', 'erp.houseoptima.fi.local'); 
    $crawler = $this->client->request('GET', '/economy/manage/new_outbound_invoices'); 

此功能进行登录

public function logIn(User $user) 
{ 
    $session = $this->client->getContainer()->get('session'); 

    $firewall = 'main'; 
    $token = new UsernamePasswordToken($user, null, $firewall, $user->getRoles()); 
    $session->set('_security_'.$firewall, serialize($token)); 
    $session->save(); 

    $cookie = new Cookie($session->getName(), $session->getId()); 
    $this->client->getCookieJar()->set($cookie); 
} 

什么问题,此安全?随着注释错误403 withot 200,当检查行动中被授权的用户拥有真正的

回答

2

您需要通过User对象

/** 
* @Security("is_granted('PERM_MODULE_OUTBOUND_INVOICES_READ', user)") 
*/ 
public function indexAction(User $user) 
{ 
+0

在行动,我不等待用户,只要求 –

+0

尝试添加的作用, expirement'PERM_MODULE_OUTBOUND_INVOICES_READ'位仍然有403 –

+0

哪个对象需要'PERM_MODULE_OUTBOUND_INVOICES_READ'的选举人? – Federkun