Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
M
meibuyu-rpc
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
without authentication
meibuyu-rpc
Commits
729b0ce7
Commit
729b0ce7
authored
Apr 09, 2020
by
王源
🎧
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
添加自动权限注解,处理权限名生成规则
parent
dc4123a6
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
86 additions
and
18 deletions
+86
-18
AutoPerm.php
src/Annotation/AutoPerm.php
+30
-0
PermAnnotationAspect.php
src/Aspect/PermAnnotationAspect.php
+56
-18
No files found.
src/Annotation/AutoPerm.php
0 → 100644
View file @
729b0ce7
<?php
/**
* Created by PhpStorm.
* User: Zero
* Date: 2020/4/9
* Time: 14:59
*/
namespace
Meibuyu\Micro\Annotation
;
use
Hyperf\Di\Annotation\AbstractAnnotation
;
/**
* @Annotation
* @Target({"CLASS"})
*/
class
AutoPerm
extends
AbstractAnnotation
{
/**
* @var string
*/
public
$prefix
=
''
;
/**
* @var array
*/
public
$exclude
=
[];
}
\ No newline at end of file
src/Aspect/PermAnnotationAspect.php
View file @
729b0ce7
...
@@ -8,11 +8,13 @@
...
@@ -8,11 +8,13 @@
namespace
Meibuyu\Micro\Aspect
;
namespace
Meibuyu\Micro\Aspect
;
use
Hyperf\Contract\ConfigInterface
;
use
Hyperf\Di\Annotation\Aspect
;
use
Hyperf\Di\Annotation\Aspect
;
use
Hyperf\Di\Annotation\Inject
;
use
Hyperf\Di\Annotation\Inject
;
use
Hyperf\Di\Aop\AbstractAspect
;
use
Hyperf\Di\Aop\AbstractAspect
;
use
Hyperf\Di\Aop\ProceedingJoinPoint
;
use
Hyperf\Di\Aop\ProceedingJoinPoint
;
use
Hyperf\Utils\Str
;
use
Hyperf\Utils\Str
;
use
Meibuyu\Micro\Annotation\AutoPerm
;
use
Meibuyu\Micro\Annotation\Perm
;
use
Meibuyu\Micro\Annotation\Perm
;
use
Meibuyu\Micro\Exceptions\HttpResponseException
;
use
Meibuyu\Micro\Exceptions\HttpResponseException
;
use
Meibuyu\Micro\Handler\PermHandler
;
use
Meibuyu\Micro\Handler\PermHandler
;
...
@@ -29,8 +31,15 @@ class PermAnnotationAspect extends AbstractAspect
...
@@ -29,8 +31,15 @@ class PermAnnotationAspect extends AbstractAspect
*/
*/
private
$permHandler
;
private
$permHandler
;
/**
* @Inject
* @var ConfigInterface
*/
protected
$config
;
public
$annotations
=
[
public
$annotations
=
[
Perm
::
class
,
Perm
::
class
,
AutoPerm
::
class
,
];
];
/**
/**
...
@@ -41,43 +50,72 @@ class PermAnnotationAspect extends AbstractAspect
...
@@ -41,43 +50,72 @@ class PermAnnotationAspect extends AbstractAspect
*/
*/
public
function
process
(
ProceedingJoinPoint
$proceedingJoinPoint
)
public
function
process
(
ProceedingJoinPoint
$proceedingJoinPoint
)
{
{
$perm
=
$this
->
getPermName
(
$proceedingJoinPoint
);
$perm
=
$this
->
genPermName
(
$proceedingJoinPoint
);
if
(
$this
->
permHandler
->
check
(
$perm
))
{
if
(
$perm
)
{
return
$proceedingJoinPoint
->
process
();
if
(
$this
->
permHandler
->
check
(
$perm
))
{
}
else
{
return
$proceedingJoinPoint
->
process
();
throw
new
HttpResponseException
(
'当前用户没有此操作权限'
);
}
else
{
throw
new
HttpResponseException
(
'当前用户没有此操作权限'
);
}
}
}
return
$proceedingJoinPoint
->
process
();
}
}
public
function
getPermName
(
ProceedingJoinPoint
$proceedingJoinPoint
)
// 生成权限名
public
function
genPermName
(
ProceedingJoinPoint
$proceedingJoinPoint
)
{
{
/** @var Perm $annotation */
/** @var AutoPerm $autoPerm */
$annotation
=
$this
->
getAnnotation
(
$proceedingJoinPoint
);
/** @var Perm $perm */
if
(
$annotation
->
name
)
{
[
$autoPerm
,
$perm
]
=
$this
->
getAnnotations
(
$proceedingJoinPoint
);
return
$annotation
->
name
;
if
(
$perm
&&
$perm
->
name
)
{
// 如果有指定权限名,直接返回
return
$perm
->
name
;
}
else
{
}
else
{
$className
=
$proceedingJoinPoint
->
className
;
$methodName
=
$proceedingJoinPoint
->
methodName
;
$methodName
=
$proceedingJoinPoint
->
methodName
;
return
$this
->
getPrefix
(
$className
)
.
$methodName
;
$className
=
$proceedingJoinPoint
->
className
;
if
(
$autoPerm
)
{
if
(
in_array
(
$methodName
,
$autoPerm
->
exclude
))
{
// 排除不鉴权的方法
return
false
;
}
if
(
$autoPerm
->
prefix
)
{
// 如果有指定前缀,直接拼接返回
return
$this
->
parseName
(
$autoPerm
->
prefix
,
$methodName
);
}
}
return
$this
->
parseName
(
$this
->
genPrefix
(
$className
),
$methodName
);
}
}
}
}
protected
function
getPrefix
(
string
$className
)
:
string
// 拼接权限名
protected
function
parseName
(
$prefix
,
$methodName
)
{
// 注意每个应用的app_name的唯一性
$appName
=
$this
->
config
->
get
(
'app_name'
);
if
(
$prefix
[
-
1
]
!==
'_'
)
{
$prefix
.=
'_'
;
}
return
$appName
.
'_'
.
$prefix
.
$methodName
;
}
// 生成前缀
protected
function
genPrefix
(
string
$className
)
:
string
{
{
$handledNamespace
=
Str
::
replaceFirst
(
'Controller'
,
''
,
Str
::
after
(
$className
,
'\\Controller\\'
));
$handledNamespace
=
Str
::
replaceFirst
(
'Controller'
,
''
,
Str
::
after
(
$className
,
'\\Controller\\'
));
$handledNamespace
=
str_replace
(
'\\'
,
'_'
,
$handledNamespace
);
$handledNamespace
=
str_replace
(
'\\'
,
'_'
,
$handledNamespace
);
$prefix
=
Str
::
snake
(
$handledNamespace
);
$prefix
=
Str
::
snake
(
$handledNamespace
);
$prefix
=
str_replace
(
'__'
,
'_'
,
$prefix
);
$prefix
=
str_replace
(
'__'
,
'_'
,
$prefix
);
if
(
$prefix
[
-
1
]
!==
'_'
)
{
$prefix
.=
'_'
;
}
return
$prefix
;
return
$prefix
;
}
}
public
function
getAnnotation
(
ProceedingJoinPoint
$proceedingJoinPoint
)
// 获取注解
public
function
getAnnotations
(
ProceedingJoinPoint
$proceedingJoinPoint
)
{
{
$metadata
=
$proceedingJoinPoint
->
getAnnotationMetadata
();
$metadata
=
$proceedingJoinPoint
->
getAnnotationMetadata
();
return
$metadata
->
method
[
Perm
::
class
]
??
null
;
return
[
$metadata
->
class
[
AutoPerm
::
class
]
??
null
,
$metadata
->
method
[
Perm
::
class
]
??
null
];
}
}
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment