与Symfony2权限相关的语句在官方文档上不是很全,每次要用到时查找起来十分费劲,所以在这里列出与Symfony2权限相关的语句,以备查:
- /**
- * 生成SecurityIdentity
- */
- //基于用户生成SecurityIdentity
- $securityIdentity = UserSecurityIdentity::fromAccount($user);
- //基于角色生成SecurityIdentity
- $securityIdentity = new RoleSecurityIdentity($role->getRole());
-
- /**
- * 生成ObjectIdentity
- */
- //基于对象生成ObjectIdentity
- $objectIdentity = ObjectIdentity::fromDomainObject($comment);
- //基于类生成ObjectIdentity
- $objectIdentity = new ObjectIdentity('some_identifier', 'Class\FQCN');
-
- /**
- * 生成ACL
- */
- $acl = $aclProvider->createAcl($objectIdentity);
- //插入ACE(基于对象)
- $acl->insertObjectAce($securityIdentity, MaskBuilder::MASK_OWNER);
- //插入ACE(基于类)
- $acl->insertClassAce($securityIdentity, MaskBuilder::MASK_CREATE);
- $aclProvider->updateAcl($acl);
-
- /**
- * 判断权限
- */
- $securityContext = $this->get('security.context');
- $securityContext->isGranted('CREATE', $classIdentity); // 返回真
- $securityContext->isGranted('VIEW', $classIdentity); // 返回真
- $securityContext->isGranted('DELETE', $classIdentity); // 返回假
-
-
- /**
- *ACE的增删改查
- */
- //插入基于对象的ACE
- $acl->insertObjectAce(SecurityIdentityInterface $sid, $mask, $index = 0, $granting = true, $strategy = null) ;
- //插入基于对象字段的ACE
- $acl->insertObjectFieldAce($field, SecurityIdentityInterface $sid, $mask, $index = 0, $granting = true, $strategy = null) ;
- //插入基于类的ACE
- $acl->insertClassAce(SecurityIdentityInterface $sid, $mask, $index = 0, $granting = true, $strategy = null) ;
- //插入基于类字段的ACE
- $acl->insertClassFieldAce($field, SecurityIdentityInterface $sid, $mask, $index = 0, $granting = true, $strategy = null) ;
-
- //删除ACE
- $acl->deleteObjectAce($index) ;
- $acl->deleteObjectFieldAce($index, $field);
- $acl->deleteClassAce($index) ;
- $acl->deleteClassFieldAce($index, $field);
-
- //更新ACE
- $acl->updateObjectAce($index, $mask, $strategy = null) ;
- $acl->updateObjectFieldAce($index, $field, $mask, $strategy = null) ;
- $acl->updateClassAce($index, $mask, $strategy = null) ;
- $acl->updateClassFieldAce($index, $field, $mask, $strategy = null) ;
-
- //得到ACE
- $acl->getObjectAces() ;
- $acl->getObjectFieldAces($field) ;
- $acl->getClassAces() ;
- $acl->getClassFieldAces($field) ;
本文出自 “野火兔的窝” 博客,请务必保留此出处http://firehare.blog.51cto.com/809276/767785