[学习笔记] Symfony2权限相关语句备忘 [转]

与Symfony2权限相关的语句在官方文档上不是很全,每次要用到时查找起来十分费劲,所以在这里列出与Symfony2权限相关的语句,以备查:

  1. /** 
  2.  * 生成SecurityIdentity 
  3.  */ 
  4. //基于用户生成SecurityIdentity 
  5. $securityIdentity = UserSecurityIdentity::fromAccount($user); 
  6. //基于角色生成SecurityIdentity 
  7. $securityIdentity = new RoleSecurityIdentity($role->getRole()); 
  8.  
  9. /** 
  10.  * 生成ObjectIdentity 
  11.  */ 
  12. //基于对象生成ObjectIdentity 
  13. $objectIdentity = ObjectIdentity::fromDomainObject($comment);  
  14. //基于类生成ObjectIdentity 
  15. $objectIdentity = new ObjectIdentity('some_identifier''Class\FQCN'); 
  16.  
  17. /** 
  18.  * 生成ACL 
  19.  */ 
  20. $acl = $aclProvider->createAcl($objectIdentity); 
  21. //插入ACE(基于对象) 
  22. $acl->insertObjectAce($securityIdentity, MaskBuilder::MASK_OWNER);  
  23. //插入ACE(基于类) 
  24. $acl->insertClassAce($securityIdentity, MaskBuilder::MASK_CREATE); 
  25. $aclProvider->updateAcl($acl); 
  26.  
  27. /** 
  28.  * 判断权限 
  29.  */ 
  30. $securityContext = $this->get('security.context'); 
  31. $securityContext->isGranted('CREATE'$classIdentity);  // 返回真 
  32. $securityContext->isGranted('VIEW'$classIdentity);    // 返回真 
  33. $securityContext->isGranted('DELETE'$classIdentity);  // 返回假 
  34.  
  35.  
  36. /** 
  37.  *ACE的增删改查 
  38.  */ 
  39. //插入基于对象的ACE 
  40. $acl->insertObjectAce(SecurityIdentityInterface $sid$mask$index = 0, $granting = true, $strategy = null) ;
  41. //插入基于对象字段的ACE 
  42. $acl->insertObjectFieldAce($field, SecurityIdentityInterface $sid$mask$index = 0, $granting = true, $strategy = null) ;
  43. //插入基于类的ACE 
  44. $acl->insertClassAce(SecurityIdentityInterface $sid$mask$index = 0, $granting = true, $strategy = null) ;
  45. //插入基于类字段的ACE 
  46. $acl->insertClassFieldAce($field, SecurityIdentityInterface $sid$mask$index = 0, $granting = true, $strategy = null) ;
  47.  
  48. //删除ACE 
  49. $acl->deleteObjectAce($index) ;
  50. $acl->deleteObjectFieldAce($index$field);
  51. $acl->deleteClassAce($index) ;
  52. $acl->deleteClassFieldAce($index$field);
  53.  
  54. //更新ACE 
  55. $acl->updateObjectAce($index$mask$strategy = null) ;
  56. $acl->updateObjectFieldAce($index$field$mask$strategy = null) ;
  57. $acl->updateClassAce($index$mask$strategy = null) ;
  58. $acl->updateClassFieldAce($index$field$mask$strategy = null) ;
  59.  
  60. //得到ACE 
  61. $acl->getObjectAces() ;
  62. $acl->getObjectFieldAces($field) ;
  63. $acl->getClassAces() ;
  64. $acl->getClassFieldAces($field) ;

 

本文出自 “野火兔的窝” 博客,请务必保留此出处http://firehare.blog.51cto.com/809276/767785

posted on 2014-01-17 15:02  Felixdh  阅读(304)  评论(0编辑  收藏  举报

导航