2016-12-30 47 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