Acl\Adapter\Utility\PhpAco::path PHP Method

path() public method

return path to the requested ACO with allow and deny rules attached on each level
public path ( string $aco ) : array
$aco string ACO string
return array
    public function path($aco)
    {
        $aco = $this->resolve($aco);
        $path = [];
        $level = 0;
        $root = $this->_tree;
        $stack = [[$root, 0]];
        while (!empty($stack)) {
            list($root, $level) = array_pop($stack);
            if (empty($path[$level])) {
                $path[$level] = [];
            }
            foreach ($root as $node => $elements) {
                $pattern = '/^' . str_replace(array_keys(static::$modifiers), array_values(static::$modifiers), $node) . '$/';
                if ($node == $aco[$level] || preg_match($pattern, $aco[$level])) {
                    // merge allow/denies with $path of current level
                    foreach (['allow', 'deny'] as $policy) {
                        if (!empty($elements[$policy])) {
                            if (empty($path[$level][$policy])) {
                                $path[$level][$policy] = [];
                            }
                            $path[$level][$policy] = array_merge($path[$level][$policy], $elements[$policy]);
                        }
                    }
                    // traverse
                    if (!empty($elements['children']) && isset($aco[$level + 1])) {
                        array_push($stack, [$elements['children'], $level + 1]);
                    }
                }
            }
        }
        return $path;
    }

Usage Example

Ejemplo n.º 1
0
 /**
  * Main ACL check function. Checks to see if the ARO (access request object) has access to the
  * ACO (access control object).
  *
  * @param string $aro ARO
  * @param string $aco ACO
  * @param string $action Action
  * @return bool true if access is granted, false otherwise
  */
 public function check($aro, $aco, $action = "*")
 {
     $allow = $this->options['policy'];
     $prioritizedAros = $this->Aro->roles($aro);
     if ($action && $action !== "*") {
         $aco .= '/' . $action;
     }
     $path = $this->Aco->path($aco);
     if (empty($path)) {
         return $allow;
     }
     foreach ($path as $node) {
         foreach ($prioritizedAros as $aros) {
             if (!empty($node['allow'])) {
                 $allow = $allow || count(array_intersect($node['allow'], $aros));
             }
             if (!empty($node['deny'])) {
                 $allow = $allow && !count(array_intersect($node['deny'], $aros));
             }
         }
     }
     return $allow;
 }