feat : Composite permission
This commit is contained in:
77
app/Service/Test/Composite/PermissionService.php
Normal file
77
app/Service/Test/Composite/PermissionService.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;
|
||||
|
||||
use App\Service\Test\TestBaseService;
|
||||
|
||||
class PermissionService extends TestBaseService
|
||||
{
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function handle(): array
|
||||
{
|
||||
return $this->return->success();
|
||||
}
|
||||
|
||||
/**
|
||||
* todo permission
|
||||
* function createPermissionTree(): PermissionComponent {
|
||||
* $root = new PermissionGroup("Root");
|
||||
*
|
||||
* $adminGroup = new PermissionGroup("Admin");
|
||||
* $adminGroup->add(new SimplePermission("user.create"));
|
||||
* $adminGroup->add(new SimplePermission("user.delete"));
|
||||
*
|
||||
* $editorGroup = new PermissionGroup("Editor");
|
||||
* $editorGroup->add(new SimplePermission("content.create"));
|
||||
* $editorGroup->add(new SimplePermission("content.edit"));
|
||||
*
|
||||
* $viewerGroup = new PermissionGroup("Viewer");
|
||||
* $viewerGroup->add(new SimplePermission("content.view"));
|
||||
*
|
||||
* $root->add($adminGroup);
|
||||
* $root->add($editorGroup);
|
||||
* $root->add($viewerGroup);
|
||||
*
|
||||
* return $root;
|
||||
* }
|
||||
* // 配置依赖
|
||||
* // config/autoload/dependencies.php
|
||||
* return [
|
||||
* PermissionService::class => function () {
|
||||
* $permissionTree = createPermissionTree();
|
||||
* return new PermissionService($permissionTree);
|
||||
* },
|
||||
* ];
|
||||
* // 在中间件中使用
|
||||
* class PermissionMiddleware implements MiddlewareInterface
|
||||
* {
|
||||
* /**
|
||||
* Inject
|
||||
* var PermissionService
|
||||
* /
|
||||
* private $permissionService;
|
||||
*
|
||||
* public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
|
||||
* {
|
||||
* $permission = $request->getAttribute('permission');
|
||||
*
|
||||
* if (!$this->permissionService->hasPermission($permission)) {
|
||||
* throw new HttpException(403, 'Forbidden');
|
||||
* }
|
||||
*
|
||||
* return $handler->handle($request);
|
||||
* }
|
||||
* }
|
||||
*/
|
||||
}
|
||||
Reference in New Issue
Block a user