Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
jinli gu
Litemall
Commits
a310c5ed
Commit
a310c5ed
authored
Jan 31, 2019
by
Junling Bu
Browse files
feat[litemall-admin, litemall-admin-api]: 权限管理页面显示按钮对应的API
parent
50e4ec72
Changes
4
Hide whitespace changes
Inline
Side-by-side
litemall-admin-api/src/main/java/org/linlinjava/litemall/admin/util/PermVo.java
View file @
a310c5ed
...
...
@@ -5,6 +5,7 @@ import java.util.List;
public
class
PermVo
{
private
String
id
;
private
String
label
;
private
String
api
;
private
List
<
PermVo
>
children
;
public
String
getId
()
{
...
...
@@ -23,6 +24,14 @@ public class PermVo {
this
.
label
=
label
;
}
public
void
setApi
(
String
api
)
{
this
.
api
=
api
;
}
public
String
getApi
()
{
return
api
;
}
public
List
<
PermVo
>
getChildren
()
{
return
children
;
}
...
...
litemall-admin-api/src/main/java/org/linlinjava/litemall/admin/util/Permission.java
0 → 100644
View file @
a310c5ed
package
org.linlinjava.litemall.admin.util
;
import
org.apache.shiro.authz.annotation.RequiresPermissions
;
import
org.linlinjava.litemall.admin.annotation.RequiresPermissionsDesc
;
import
org.springframework.web.bind.annotation.RequestMapping
;
public
class
Permission
{
private
RequiresPermissions
requiresPermissions
;
private
RequiresPermissionsDesc
requiresPermissionsDesc
;
private
String
api
;
public
RequiresPermissions
getRequiresPermissions
()
{
return
requiresPermissions
;
}
public
RequiresPermissionsDesc
getRequiresPermissionsDesc
()
{
return
requiresPermissionsDesc
;
}
public
void
setRequiresPermissions
(
RequiresPermissions
requiresPermissions
)
{
this
.
requiresPermissions
=
requiresPermissions
;
}
public
void
setRequiresPermissionsDesc
(
RequiresPermissionsDesc
requiresPermissionsDesc
)
{
this
.
requiresPermissionsDesc
=
requiresPermissionsDesc
;
}
public
String
getApi
()
{
return
api
;
}
public
void
setApi
(
String
api
)
{
this
.
api
=
api
;
}
}
litemall-admin-api/src/main/java/org/linlinjava/litemall/admin/util/PermissionUtil.java
View file @
a310c5ed
...
...
@@ -8,6 +8,9 @@ import org.springframework.context.ApplicationContext;
import
org.springframework.core.annotation.AnnotationUtils
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.util.ClassUtils
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.PostMapping
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
java.lang.reflect.Method
;
import
java.util.*
;
...
...
@@ -15,36 +18,13 @@ import java.util.stream.Collectors;
public
class
PermissionUtil
{
public
static
Map
<
RequiresPermissions
,
RequiresPermissionsDesc
>
findPermissions
(
ApplicationContext
context
,
String
basicPackage
)
{
Map
<
String
,
Object
>
map
=
context
.
getBeansWithAnnotation
(
Controller
.
class
);
Map
<
RequiresPermissions
,
RequiresPermissionsDesc
>
permissions
=
new
HashMap
<>();
for
(
Map
.
Entry
<
String
,
Object
>
entry
:
map
.
entrySet
()){
Object
bean
=
entry
.
getValue
();
if
(!
StringUtils
.
contains
(
ClassUtils
.
getPackageName
(
bean
.
getClass
()),
basicPackage
)){
continue
;
}
Class
<?>
clz
=
bean
.
getClass
();
Class
controllerClz
=
clz
.
getSuperclass
();
List
<
Method
>
methods
=
MethodUtils
.
getMethodsListWithAnnotation
(
controllerClz
,
RequiresPermissions
.
class
);
for
(
Method
method
:
methods
){
RequiresPermissions
requiresPermissions
=
AnnotationUtils
.
getAnnotation
(
method
,
RequiresPermissions
.
class
);
RequiresPermissionsDesc
requiresPermissionsDesc
=
AnnotationUtils
.
getAnnotation
(
method
,
RequiresPermissionsDesc
.
class
);
if
(
requiresPermissions
==
null
||
requiresPermissionsDesc
==
null
){
continue
;
}
permissions
.
put
(
requiresPermissions
,
requiresPermissionsDesc
);
}
}
return
permissions
;
}
public
static
List
<
PermVo
>
listPermissions
(
ApplicationContext
context
,
String
basicPackage
)
{
List
<
PermVo
>
root
=
new
ArrayList
<>();
Map
<
RequiresPermissions
,
RequiresPermissionsDesc
>
map
=
findPermissions
(
context
,
basicPackage
);
for
(
Map
.
Entry
<
RequiresPermissions
,
RequiresPermissionsDesc
>
entry
:
map
.
entrySet
())
{
RequiresPermissions
requiresPermissions
=
entry
.
getKey
();
RequiresPermissionsDesc
requiresPermissionsDesc
=
entry
.
getValue
();
List
<
Permission
>
permissions
=
findPermissions
(
context
,
basicPackage
);
for
(
Permission
permission
:
permissions
)
{
RequiresPermissions
requiresPermissions
=
permission
.
getRequiresPermissions
();
RequiresPermissionsDesc
requiresPermissionsDesc
=
permission
.
getRequiresPermissionsDesc
();
String
api
=
permission
.
getApi
();
String
[]
menus
=
requiresPermissionsDesc
.
menu
();
if
(
menus
.
length
!=
2
){
...
...
@@ -84,9 +64,66 @@ public class PermissionUtil {
PermVo
leftPerm
=
new
PermVo
();
leftPerm
.
setId
(
requiresPermissions
.
value
()[
0
]);
leftPerm
.
setLabel
(
requiresPermissionsDesc
.
button
());
leftPerm
.
setApi
(
api
);
perm2
.
getChildren
().
add
(
leftPerm
);
}
return
root
;
}
public
static
List
<
Permission
>
findPermissions
(
ApplicationContext
context
,
String
basicPackage
)
{
Map
<
String
,
Object
>
map
=
context
.
getBeansWithAnnotation
(
Controller
.
class
);
List
<
Permission
>
permissions
=
new
ArrayList
<>();
for
(
Map
.
Entry
<
String
,
Object
>
entry
:
map
.
entrySet
()){
Object
bean
=
entry
.
getValue
();
if
(!
StringUtils
.
contains
(
ClassUtils
.
getPackageName
(
bean
.
getClass
()),
basicPackage
)){
continue
;
}
Class
<?>
clz
=
bean
.
getClass
();
Class
controllerClz
=
clz
.
getSuperclass
();
RequestMapping
clazzRequestMapping
=
AnnotationUtils
.
findAnnotation
(
controllerClz
,
RequestMapping
.
class
);
List
<
Method
>
methods
=
MethodUtils
.
getMethodsListWithAnnotation
(
controllerClz
,
RequiresPermissions
.
class
);
for
(
Method
method
:
methods
){
RequiresPermissions
requiresPermissions
=
AnnotationUtils
.
getAnnotation
(
method
,
RequiresPermissions
.
class
);
RequiresPermissionsDesc
requiresPermissionsDesc
=
AnnotationUtils
.
getAnnotation
(
method
,
RequiresPermissionsDesc
.
class
);
if
(
requiresPermissions
==
null
||
requiresPermissionsDesc
==
null
){
continue
;
}
String
api
=
""
;
if
(
clazzRequestMapping
!=
null
){
api
=
clazzRequestMapping
.
value
()[
0
];
}
PostMapping
postMapping
=
AnnotationUtils
.
getAnnotation
(
method
,
PostMapping
.
class
);
if
(
postMapping
!=
null
){
api
=
"POST "
+
api
+
postMapping
.
value
()[
0
];
Permission
permission
=
new
Permission
();
permission
.
setRequiresPermissions
(
requiresPermissions
);
permission
.
setRequiresPermissionsDesc
(
requiresPermissionsDesc
);
permission
.
setApi
(
api
);
permissions
.
add
(
permission
);
continue
;
}
GetMapping
getMapping
=
AnnotationUtils
.
getAnnotation
(
method
,
GetMapping
.
class
);
if
(
getMapping
!=
null
){
api
=
"GET "
+
api
+
getMapping
.
value
()[
0
];
Permission
permission
=
new
Permission
();
permission
.
setRequiresPermissions
(
requiresPermissions
);
permission
.
setRequiresPermissionsDesc
(
requiresPermissionsDesc
);
permission
.
setApi
(
api
);
permissions
.
add
(
permission
);
continue
;
}
// TODO
// 这里只支持GetMapping注解或者PostMapping注解,应该进一步提供灵活性
throw
new
RuntimeException
(
"目前权限管理应该在method的前面使用GetMapping注解或者PostMapping注解"
);
}
}
return
permissions
;
}
}
litemall-admin/src/views/sys/role.vue
View file @
a310c5ed
...
...
@@ -50,7 +50,12 @@
:default-checked-keys=
"assignedPermissions"
show-checkbox
node-key=
"id"
highlight-current
/>
highlight-current
>
<span
slot-scope=
"{ node, data }"
class=
"custom-tree-node"
>
<span>
{{ data.label }}
</span>
<el-tag
v-if=
"data.api"
type=
"success"
size=
"mini"
>
{{ data.api }}
</el-tag>
</span>
</el-tree>
<div
slot=
"footer"
class=
"dialog-footer"
>
<el-button
@
click=
"permissionDialogFormVisible = false"
>
取消
</el-button>
<el-button
type=
"primary"
@
click=
"updatePermission"
>
确定
</el-button>
...
...
Write
Preview
Supports
Markdown
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