feat : Composite permission
This commit is contained in:
77
app/Service/Test/Composite/Permission/GroupPermission.php
Normal file
77
app/Service/Test/Composite/Permission/GroupPermission.php
Normal file
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
/**
|
||||
* This service file is part of item.
|
||||
*
|
||||
* @author ctexthuang
|
||||
* @contact ctexthuang@qq.com
|
||||
* @web_site https://ctexthuang.com
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service\Test\Composite\Permission;
|
||||
|
||||
use App\Interface\Test\Composite\PermissionComponentInterface;
|
||||
|
||||
class GroupPermission implements PermissionComponentInterface
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private string $name;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private array $children = [];
|
||||
|
||||
/**
|
||||
* 构造函数
|
||||
* @param string $name
|
||||
*/
|
||||
public function __construct(string $name)
|
||||
{
|
||||
$this->name = $name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $permission
|
||||
* @return bool
|
||||
*/
|
||||
public function check(string $permission): bool
|
||||
{
|
||||
foreach ($this->children as $child) {
|
||||
if ($child->check($permission)) return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getName(): string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param PermissionComponentInterface $component
|
||||
* @return void
|
||||
*/
|
||||
public function add(PermissionComponentInterface $component): void
|
||||
{
|
||||
$this->children[] = $component;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param PermissionComponentInterface $component
|
||||
* @return void
|
||||
*/
|
||||
public function remove(PermissionComponentInterface $component): void
|
||||
{
|
||||
$this->children = array_filter($this->children, function ($c) use ($component) {
|
||||
return $c === $component;
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user