【问题标题】:condense logics into loop将逻辑压缩成循环
【发布时间】:2014-01-04 03:29:11
【问题描述】:

我有一些逻辑是我前一阵子拼凑起来的,现在我需要使它成为通用的,以便在无限数量的父级上寻找父级。将此代码构造为循环而不是我的意大利面条巢的最有效方法是什么?

public function hasAccess($user,$container)
{

    //If user is admin
    $roles = $user->getRoles();
    foreach ($roles as $role) {
        if ($role == 'ROLE_ADMIN') {
            return true;
        }    
    };

    //Or if user has access to object
    foreach ($container->getUsers() as $userWithAccess) {
        if ($userWithAccess == $user) {
            return true;
        }
    }

    //Or if object has parent and user has access to the parent
    $parent = $container->getParent();
    if ($parent) {
        foreach ($parent->getUsers() as $userWithAccess) {
            if ($userWithAccess == $user) {
                return true;
            }
        }
    }

    //Or if object has grandparent and user has access to the grandparent
    $parent = $container->getParent();
    if ($parent) {
        $grandparent = $parent->getParent();   
        if ($grandparent) {        
            foreach ($grandparent->getUsers() as $userWithAccess) {
                if ($userWithAccess == $user) {
                    return true;
                }
            }
        }
    }     

    //Or if object has greatgrandparent (=entire company access) and user has access to the greatgrandparent ('entire company')
    $parent = $container->getParent();
    if ($parent) {
        $grandparent = $parent->getParent();   
        if ($grandparent) {
            $greatgrandparent = $grandparent->getParent();   
                if ($greatgrandparent) {                        
                foreach ($greatgrandparent->getUsers() as $userWithAccess) {
                    if ($userWithAccess == $user) {
                        return true;
                    }
                }
            }
        }
    }

    //Or if object has greatgreatgrandparent (=entire company if this content lives within a module, otherwise this level doesn't exist) and user has access to the greatgreatgrandparent (ie entire company)
    $parent = $container->getParent();
    if ($parent) {
        $grandparent = $parent->getParent();   
        if ($grandparent) {
            $greatgrandparent = $grandparent->getParent();   
            if ($greatgrandparent) {  
                $greatgreatgrandparent = $greatgrandparent->getParent();                        
                if ($greatgreatgrandparent) {                       
                    foreach ($greatgreatgrandparent->getUsers() as $userWithAccess) {
                        if ($userWithAccess == $user) {
                            return true;
                        }
                    }
                }
            }
        }
    }        

    //At the moment, access to 'entire company' does NOT grant access to monitors outside the hierarchy.
    //It is still possible to add privileges to those individual monitors.


    //If none of the above has matched...
    return false;

}

【问题讨论】:

标签: php loops logic


【解决方案1】:

也许是一个 while 循环来检查 $parent 的存在,检查所有用户的访问权限,然后将 $parent 设为当前用户的父级。像这样的:

$parent = $container->getParent();
while ($parent) {
  foreach ($parent->getUsers() as $userWithAccess) {
    if ($userWithAccess == $user) {
      return true;
    }
  }
  $parent = $parent->getParent();
}

【讨论】:

    【解决方案2】:

    如果您想获得祖先,使用while 可能会有所帮助:

    $ancestor = $parent;
    while($parent = $ancestor->getParent())
      $ancestor = $parent;
    
    // foreach($ancestor->getUsers() ...
    

    要获得第 4 个父节点,只需添加一个计数器变量并在 $counter > 4 时中断循环

    您也可以使用in_array() 来避免所有这些 foreach 语句(如果 get* 方法返回数组)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-31
      • 2017-03-01
      • 2018-09-09
      • 1970-01-01
      • 2011-06-05
      相关资源
      最近更新 更多