好的,我尝试自己实现它。也许它不漂亮,但这是我自己想出的最佳解决方案。这是正确的方向吗?如有任何反馈,我将不胜感激!
解决方案:
我使用我的模型(suggested here)代替字符串作为资源和角色。我使用PointResourceInterface 来标记需要特定点数的资源,并在我的用户类中实现Zend\Permissions\Acl\Role\RoleInterface。现在我创建一个新的NeededPointsAssertion:
class NeededPointsAssertion implements AssertionInterface
{
public function assert(Acl $acl, RoleInterface $role = null,
ResourceInterface $resource = null, $privilege = null) {
// Resource must have points, otherwise not applicable
if (!($resource instanceof PointResourceInterface)) {
throw new Exception('Resource is not an PointResourceInterface. NeededPointsAssertion is not applicable.');
}
//check if points are high enough, in my app only users have points
$hasEnoughPoints = false;
if ($role instanceof User) {
// role is User and resource is PointResourceInterface
$hasEnoughPoints = ($role->getPoints() >= $resource->getPoints());
}
return $hasEnoughPoints;
}
}
PointResourceInterface 看起来像这样:
use Zend\Permissions\Acl\Resource\ResourceInterface;
interface PointResourceInterface extends ResourceInterface {
public function getPoints();
}
设置:
$acl->allow('user', $pointResource, null, new NeededPointsAssertion());
用户可以访问需要积分的资源。但另外检查了NeededPointsAssertion。
访问:
我正在检查是否允许这样访问:
$acl->isAllowed($role, $someResource);
如果有用户 $role = $user,否则是 guest 或其他。
灵感来自http://www.aviblock.com/blog/2009/03/19/acl-in-zend-framework/
更新:现在回顾一下,也可以通过构造函数添加所需的点并将其存储为属性。自己决定什么对你的应用有意义...