Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
M
meibuyu-micro
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
1
Merge Requests
1
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-micro
Commits
dc4123a6
Commit
dc4123a6
authored
Apr 08, 2020
by
王源
🎧
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
添加权限注解和相应的切面
parent
c46692b8
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
141 additions
and
0 deletions
+141
-0
Perm.php
src/Annotation/Perm.php
+30
-0
PermAnnotationAspect.php
src/Aspect/PermAnnotationAspect.php
+83
-0
PermHandler.php
src/Handler/PermHandler.php
+28
-0
No files found.
src/Annotation/Perm.php
0 → 100644
View file @
dc4123a6
<?php
/**
* Created by PhpStorm.
* User: Zero
* Date: 2020/4/8
* Time: 13:59
*/
namespace
Meibuyu\Micro\Annotation
;
use
Hyperf\Di\Annotation\AbstractAnnotation
;
/**
* @Annotation
* @Target({"METHOD"})
*/
class
Perm
extends
AbstractAnnotation
{
/**
* @var string
*/
public
$name
=
''
;
public
function
__construct
(
$value
=
null
)
{
$this
->
bindMainProperty
(
'name'
,
$value
);
}
}
\ No newline at end of file
src/Aspect/PermAnnotationAspect.php
0 → 100644
View file @
dc4123a6
<?php
/**
* Created by PhpStorm.
* User: Zero
* Date: 2020/4/8
* Time: 14:48
*/
namespace
Meibuyu\Micro\Aspect
;
use
Hyperf\Di\Annotation\Aspect
;
use
Hyperf\Di\Annotation\Inject
;
use
Hyperf\Di\Aop\AbstractAspect
;
use
Hyperf\Di\Aop\ProceedingJoinPoint
;
use
Hyperf\Utils\Str
;
use
Meibuyu\Micro\Annotation\Perm
;
use
Meibuyu\Micro\Exceptions\HttpResponseException
;
use
Meibuyu\Micro\Handler\PermHandler
;
/**
* @Aspect()
*/
class
PermAnnotationAspect
extends
AbstractAspect
{
/**
* @Inject()
* @var PermHandler
*/
private
$permHandler
;
public
$annotations
=
[
Perm
::
class
,
];
/**
* @param ProceedingJoinPoint $proceedingJoinPoint
* @return mixed
* @throws HttpResponseException
* @throws \Hyperf\Di\Exception\Exception
*/
public
function
process
(
ProceedingJoinPoint
$proceedingJoinPoint
)
{
$perm
=
$this
->
getPermName
(
$proceedingJoinPoint
);
if
(
$this
->
permHandler
->
check
(
$perm
))
{
return
$proceedingJoinPoint
->
process
();
}
else
{
throw
new
HttpResponseException
(
'当前用户没有此操作权限'
);
}
}
public
function
getPermName
(
ProceedingJoinPoint
$proceedingJoinPoint
)
{
/** @var Perm $annotation */
$annotation
=
$this
->
getAnnotation
(
$proceedingJoinPoint
);
if
(
$annotation
->
name
)
{
return
$annotation
->
name
;
}
else
{
$className
=
$proceedingJoinPoint
->
className
;
$methodName
=
$proceedingJoinPoint
->
methodName
;
return
$this
->
getPrefix
(
$className
)
.
$methodName
;
}
}
protected
function
getPrefix
(
string
$className
)
:
string
{
$handledNamespace
=
Str
::
replaceFirst
(
'Controller'
,
''
,
Str
::
after
(
$className
,
'\\Controller\\'
));
$handledNamespace
=
str_replace
(
'\\'
,
'_'
,
$handledNamespace
);
$prefix
=
Str
::
snake
(
$handledNamespace
);
$prefix
=
str_replace
(
'__'
,
'_'
,
$prefix
);
if
(
$prefix
[
-
1
]
!==
'_'
)
{
$prefix
.=
'_'
;
}
return
$prefix
;
}
public
function
getAnnotation
(
ProceedingJoinPoint
$proceedingJoinPoint
)
{
$metadata
=
$proceedingJoinPoint
->
getAnnotationMetadata
();
return
$metadata
->
method
[
Perm
::
class
]
??
null
;
}
}
\ No newline at end of file
src/Handler/PermHandler.php
0 → 100644
View file @
dc4123a6
<?php
/**
* Created by PhpStorm.
* User: Zero
* Date: 2020/4/8
* Time: 16:12
*/
namespace
Meibuyu\Micro\Handler
;
use
Hyperf\Di\Annotation\Inject
;
use
Meibuyu\Micro\Model\Auth
;
use
Meibuyu\Micro\Service\Interfaces\UserServiceInterface
;
class
PermHandler
{
/**
* @Inject()
* @var UserServiceInterface
*/
protected
$userServer
;
public
function
check
(
$perm
)
{
return
$this
->
userServer
->
checkPerm
(
Auth
::
id
(),
$perm
);
}
}
\ 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