回答
isGranted()
來自安全服務獲取用戶調用isGranted(),所以這將是硬/不需要用它來獲取角色,不調整狀態的會議。
不要誤會我的意思,這是絕對有可能...這將工作,例如:
public function strangeAction()
{
// Get your User, however you normally get it
$user = $userRepository->find($id);
// Save the current token so you can put it back later
$previousToken = $this->get("security.context")->getToken();
// Create a new token
$token = new UsernamePasswordToken($user, null, "main", $user->getRoles());
// Update the security context with the new token
$this->get("security.context")->setToken($token);
// Now you have access to isGranted()
if ($this->get("security.context")->isGranted("ROLE_SOMETHING"))
{ /* Do something here */ }
// Don't forget to reset the token!
$this->get("security.context")->setToken($previousToken);
}
...但真的是沒有意義的。
實際上,你不需要令牌。這樣做的更好的方法是添加一個isGranted()
方法到您的用戶實體:
// Namespace\YourBundle\Entity\User.php
class User
{
...
public function isGranted($role)
{
return in_array($role, $this->getRoles());
}
...
}
現在你可以得到這些角色在你的控制器:
public function notSoStrangeAction()
{
// Get your User, however you normally get it
$user = $userRepository->find($id);
// Find out if that User has a Role associated to it
if ($user->isGranted("ROLE_SOMETHING"))
{ /* Do something here */ }
}
謝謝,它工作。然而,在你的例子中,你有一個錯字,我錯過了兩個小時:你的第一個setToken()使用$ previousToken作爲參數,而不是$ token:D – 2012-08-10 10:30:19
順便說一句,這對我來說很合理,因爲user-> getRoles()不是真正的角色。我真的認爲它已經壞了,但這就是應用程序的工作原理,我不能說「嘿,讓我們改變安全防火牆!」。尤其是我實習的最後一天:p – 2012-08-10 10:33:41
這個命題的問題在於它不依賴security.yml中定義的角色層次結構。查找用戶是否具有角色A與查找用戶是否被授予角色(將角色層次結構考慮在內)不一樣。後者更困難。我正在尋找這樣的解決方案... – 2013-04-12 13:09:14
我有同樣的要求,而前。所以我自己實現了。由於您需要容器中的層次結構信息,因此它不是
可能
建議使用此功能擴展用戶實體。
// first check if the role is inside the user roles of the user
// if not then check for each user role if it is a master role of the check role
public function isGranted($user, $checkrole){
$userroles = $user->getRoles();
if (in_array($checkrole, $userroles)){return true;}
foreach ($userroles as $userrole){
if ($this->roleOwnsRole($userrole, $checkrole)){return true;}
}
return false;
}
// recursively loop over the subroles of the master to check if any of them are
// the suggested slave role. If yes then the masterrole is a master and has
// the same grants as the slave.
private function roleOwnsRole($masterRole, $slaveRole, $checkvalidityroles=true, $hierarchy=null)
{
if ($hierarchy===null){$hierarchy = $this->container->getParameter('security.role_hierarchy.roles');}
if ($masterRole === $slaveRole){ return false; }
if($checkvalidityroles && (!array_key_exists($masterRole, $hierarchy) || !array_key_exists($slaveRole, $hierarchy))){ return false; }
$masterroles = $hierarchy[$masterRole];
if(in_array($slaveRole, $masterroles)){
return true;
}else{
foreach($masterroles as $masterrolerec){
if ($this->roleOwnsRole($masterrolerec, $slaveRole, false, $hierarchy)){return true;}
}
return false;
}
}
注意:您可以使用@ security.role_hierarchy:getReachableRoles服務方法來解析繼承角色 – delf 2016-11-07 13:15:31
我認爲最好的方法是手動調用AccessDecisionManager
- 像$securityContext->isGranted()
確實很好,但當前登錄的用戶。如果您使用Symfony選民來確定訪問權限,這也很好。
$token = new UsernamePasswordToken($userObject, 'none', 'main', $userObject->getRoles());
$hasAccess = $this->get('security.access.decision_manager')->decide($token, array('voter'), $optionalObjectToCheckAccessTo);
- 1. 獲取Twitter OAuth令牌和祕密,但未使用戶使用Twitter登錄
- 2. 獲取用戶登錄訪問令牌以使用YouTube API
- 3. LARAVEL - 獲取CSRF令牌用我的api登錄用戶
- 4. 使用Python登錄,以安全的網站與令牌
- 5. Symfony 2獲取登錄用戶列表
- 6. 如何獲取Windows服務的LogOn用戶的安全令牌?
- 7. 當用戶未登錄時使用表單令牌
- 8. 如何使用UDID或設備令牌進行安全登錄?
- 9. 如何獲取嘰嘰喳喳oAuth令牌的登錄用戶
- 10. 登錄用戶的firebase用戶安全
- 11. 使用sha256登錄令牌
- 12. Facebook SDK - 在用戶登錄時獲取訪問令牌
- 13. 獲取訪問令牌,無需用戶登錄
- 14. React Redux - 登錄後獲取用戶和授權令牌
- 15. Symfony登錄用戶
- 16. IsInRole獲取新的安全令牌
- 17. 獲取使用用戶令牌與C++
- 18. 如何使用用戶令牌獲取應用訪問令牌
- 19. Google登錄使用新的GoogleSignInOptions獲取訪問令牌
- 20. 獲取否。當前登錄的用戶在春季安全
- 21. 獲取已登錄用戶的Grails彈簧安全性
- 22. 如何使用httpwebrequest登錄Facebook並獲取用戶訪問令牌?
- 23. 春季安全用令牌登錄到網絡
- 24. 用戶在令牌上登錄
- 25. fosuserbundle和用戶在安全令牌
- 26. Facebook用戶訪問令牌安全
- 27. 安全存儲用戶憑證令牌
- 28. Identity Server:使用API獲取令牌並避免身份登錄
- 29. 如何使用外部登錄從Web API 2獲取令牌
- 30. 使用Android登錄獲取Google OAuth令牌
您是否將用戶的角色存儲在數據庫中? – richsage 2012-08-09 17:15:20