如何检测角色的特定拒绝?

发布于 2024-11-30 12:52:05 字数 639 浏览 1 评论 0原文

Zend_Acl 默认情况下不允许每个角色对每个权限的每个资源,直到或除非 他们是特别允许的。 在我的系统中,用户可以拥有许多角色,并且权限被分配给这些角色。 我只是让所有用户角色迭代所有用户角色,并检查 isAllowed() 以获得每个角色的给定资源和权限。

例如,如果当前资源是“foo”并且权限是“bar”,

public function checkAllow($roles, $resouse, $privilege)
{

    foreach ($roles as $role) {
        if ($acl->isAllowed($role, 'foo', 'bar') === true)
            return true;
    }
    return false;
}

现在我想对这些角色实现排序顺序,即分配的第一个角色将具有更多的优先权,然后是第二个角色,依此类推。

问题来了,我如何检测对某些角色的特定拒绝,例如

$this->deny('member','foo','bar');

在迭代所有角色时,我如何知道给定的角色被特定“拒绝”? 因此,此时我可以跳出 foreach 循环并返回 false

Zend_Acl by default disallow every role to every resource on every privilege untill or unless
they are specifically allowed.
In my system a user can have many roles and permission are assinged to these roles.
I simply get all user roles iterate over all of them and check isAllowed() for given resourse and privilege for every role.

for e.g if current resourse is 'foo' and privilege is 'bar'

public function checkAllow($roles, $resouse, $privilege)
{

    foreach ($roles as $role) {
        if ($acl->isAllowed($role, 'foo', 'bar') === true)
            return true;
    }
    return false;
}

Now I want to implement sort order on these roles i.e first role assinged will have more preference then second and so forth.

Problem comes how can I detect specific deny to some roles like

$this->deny('member','foo','bar');

While iterating over all roles how can I know the given role was specificly "denyied"?
So at that point I can break out of foreach loop and return false.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

稍尽春風 2024-12-07 12:52:05

好的,假设我理解这个问题,您正在寻找这样的东西吗?
如果没有,请更好地解释一下这个问题,以便我可以更好地提供帮助。

伪代码

// User starts without permission
$has_permission = false

// if any of the users roles have permission set true
foreach $user->roles as $role
    // if any of the user's roles allow this permission
    if $role->has_permission() == true
        $has_permission = true

    // if a user is specifically denied a permission that takes precedence
    // and immediately kicks the user out
    if $role->is_specifically_denied_permission() == true
        return  false

// return whatever value (true or false) is in $has_permission
return $has_permission

Ok, so assuming I undertand the question, is something like this what you are looking for?
if not, please explain the question a little better so i can assist better.

psuedo-code

// User starts without permission
$has_permission = false

// if any of the users roles have permission set true
foreach $user->roles as $role
    // if any of the user's roles allow this permission
    if $role->has_permission() == true
        $has_permission = true

    // if a user is specifically denied a permission that takes precedence
    // and immediately kicks the user out
    if $role->is_specifically_denied_permission() == true
        return  false

// return whatever value (true or false) is in $has_permission
return $has_permission

就像说晚安 2024-12-07 12:52:05

好吧,我自己通过进入 Zend_Acl 代码找到了解决方案,结果是 Zend_Acl 缺少 isDenied() 方法,它有 isAllowed() 但没有 isDenied()

这是我在类中扩展 Zend_Acl 的 isDenied() 方法的实现

public function isDenied($roleId,$resource,$privilege)
    {

        if($this->has($resource) && $this->hasRole($roleId))
        {

         $roleId = $this->getRole($roleId)->getRoleId();
         $resourceId = $this->get($resource)->getResourceId();   

       return @$this->_rules['byResourceId'][$resourceId]['byRoleId'][$roleId]['byPrivilegeId'][$privilege]['type'] === 'TYPE_DENY';
        }

        return false;
    }

Ok I found the solution myself by going into Zend_Acl code well its come out to be Zend_Acl is missing isDenied() Method , it has isAllowed() but not isDenied()

Heres my implementation of isDenied() method in class which extends Zend_Acl

public function isDenied($roleId,$resource,$privilege)
    {

        if($this->has($resource) && $this->hasRole($roleId))
        {

         $roleId = $this->getRole($roleId)->getRoleId();
         $resourceId = $this->get($resource)->getResourceId();   

       return @$this->_rules['byResourceId'][$resourceId]['byRoleId'][$roleId]['byPrivilegeId'][$privilege]['type'] === 'TYPE_DENY';
        }

        return false;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文