|
|
<?php |
|
|
|
|
|
namespace Kanboard\Core\Security; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class AccessMap |
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private $defaultRole = ''; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private $hierarchy = array(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private $map = array(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function setDefaultRole($role) |
|
|
{ |
|
|
$this->defaultRole = $role; |
|
|
return $this; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function setRoleHierarchy($role, array $subroles) |
|
|
{ |
|
|
foreach ($subroles as $subrole) { |
|
|
if (isset($this->hierarchy[$subrole])) { |
|
|
$this->hierarchy[$subrole][] = $role; |
|
|
} else { |
|
|
$this->hierarchy[$subrole] = array($role); |
|
|
} |
|
|
} |
|
|
|
|
|
return $this; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function getRoleHierarchy($role) |
|
|
{ |
|
|
$roles = array($role); |
|
|
|
|
|
if (isset($this->hierarchy[$role])) { |
|
|
$roles = array_merge($roles, $this->hierarchy[$role]); |
|
|
} |
|
|
|
|
|
return $roles; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function getHighestRole(array $roles) |
|
|
{ |
|
|
$rank = array(); |
|
|
|
|
|
foreach ($roles as $role) { |
|
|
$rank[$role] = count($this->getRoleHierarchy($role)); |
|
|
} |
|
|
|
|
|
asort($rank); |
|
|
|
|
|
return key($rank); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function add($controller, $methods, $role) |
|
|
{ |
|
|
if (is_array($methods)) { |
|
|
foreach ($methods as $method) { |
|
|
$this->addRule($controller, $method, $role); |
|
|
} |
|
|
} else { |
|
|
$this->addRule($controller, $methods, $role); |
|
|
} |
|
|
|
|
|
return $this; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private function addRule($controller, $method, $role) |
|
|
{ |
|
|
$controller = strtolower($controller); |
|
|
$method = strtolower($method); |
|
|
|
|
|
if (! isset($this->map[$controller])) { |
|
|
$this->map[$controller] = array(); |
|
|
} |
|
|
|
|
|
$this->map[$controller][$method] = $role; |
|
|
|
|
|
return $this; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function getRoles($controller, $method) |
|
|
{ |
|
|
$controller = strtolower($controller); |
|
|
$method = strtolower($method); |
|
|
|
|
|
foreach (array($method, '*') as $key) { |
|
|
if (isset($this->map[$controller][$key])) { |
|
|
return $this->getRoleHierarchy($this->map[$controller][$key]); |
|
|
} |
|
|
} |
|
|
|
|
|
return $this->getRoleHierarchy($this->defaultRole); |
|
|
} |
|
|
} |
|
|
|