-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement data permission management with DataScope and related…
… aspects
- Loading branch information
Showing
5 changed files
with
150 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
namespace App\Library\DataPermission\Aspects; | ||
|
||
use App\Library\DataPermission\Attribute\DataScope; | ||
use Hyperf\Context\Context; | ||
use Hyperf\Database\Query\Builder; | ||
use Hyperf\Di\Annotation\Aspect; | ||
use Hyperf\Di\Aop\AbstractAspect; | ||
use Hyperf\Di\Aop\ProceedingJoinPoint; | ||
|
||
#[Aspect] | ||
final class DataScopeAspect extends AbstractAspect | ||
{ | ||
|
||
public const CONTEXT_KEY = 'data_permission'; | ||
|
||
public array $annotations = [ | ||
DataScope::class | ||
]; | ||
|
||
public array $classes = [ | ||
Builder::class.'::update', | ||
Builder::class.'::delete', | ||
Builder::class.'::runSelect', | ||
]; | ||
|
||
public function process(ProceedingJoinPoint $proceedingJoinPoint) | ||
{ | ||
if ( | ||
isset($proceedingJoinPoint->getAnnotationMetadata()->class[DataScope::class]) || | ||
isset($proceedingJoinPoint->getAnnotationMetadata()->method[DataScope::class]) | ||
){ | ||
return $this->handleDataScope($proceedingJoinPoint); | ||
} | ||
|
||
if ($proceedingJoinPoint->className === Builder::class){ | ||
if ($proceedingJoinPoint->methodName==='runSelect'){ | ||
return $this->handleSelect($proceedingJoinPoint); | ||
} | ||
if ($proceedingJoinPoint->methodName==='delete'){ | ||
return $this->handleDelete($proceedingJoinPoint); | ||
} | ||
if ($proceedingJoinPoint->methodName==='update'){ | ||
return $this->handleUpdate($proceedingJoinPoint); | ||
} | ||
} | ||
return $proceedingJoinPoint->process(); | ||
} | ||
|
||
protected function handleDelete(ProceedingJoinPoint $proceedingJoinPoint) | ||
{ | ||
return $proceedingJoinPoint->process(); | ||
} | ||
|
||
protected function handleUpdate(ProceedingJoinPoint $proceedingJoinPoint) | ||
{ | ||
return $proceedingJoinPoint->process(); | ||
} | ||
|
||
protected function handleSelect(ProceedingJoinPoint $proceedingJoinPoint) | ||
{ | ||
/** | ||
* @var Builder $builder | ||
*/ | ||
$builder = $proceedingJoinPoint->getInstance(); | ||
if (Context::has(self::CONTEXT_KEY)){ | ||
// todo 做数据权限处理 | ||
} | ||
return $proceedingJoinPoint->process(); | ||
} | ||
|
||
|
||
protected function handleDataScope(ProceedingJoinPoint $proceedingJoinPoint) | ||
{ | ||
Context::set(self::CONTEXT_KEY, 1); | ||
$result = $proceedingJoinPoint->process(); | ||
Context::destroy(self::CONTEXT_KEY); | ||
return $result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
namespace App\Library\DataPermission\Attribute; | ||
|
||
use Attribute; | ||
use Hyperf\Di\Annotation\AbstractAnnotation; | ||
|
||
#[Attribute(Attribute::TARGET_CLASS|Attribute::TARGET_METHOD)] | ||
final class DataScope extends AbstractAnnotation | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace App\Library\DataPermission; | ||
|
||
final class Manager | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace App\Library\DataPermission\Scope; | ||
|
||
use App\Http\CurrentUser; | ||
use Hyperf\Database\Model\Builder; | ||
use Hyperf\Database\Model\Model; | ||
use Hyperf\Database\Model\Scope; | ||
|
||
final class DataScope implements Scope | ||
{ | ||
public function __construct( | ||
private readonly CurrentUser $currentUser | ||
){} | ||
|
||
public function apply(Builder $builder, Model $model): void | ||
{ | ||
if ($this->currentUser->user() === null){ | ||
return; | ||
} | ||
$user = $this->currentUser->user(); | ||
if ($user->isSuperAdmin()){ | ||
return; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters