|
|
@@ -13,32 +13,33 @@ import org.springframework.web.bind.annotation.PostMapping;
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
|
|
import java.lang.reflect.Method;
|
|
|
-import java.util.*;
|
|
|
-import java.util.stream.Collectors;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
|
|
|
public class PermissionUtil {
|
|
|
|
|
|
public static List<PermVo> listPermissions(ApplicationContext context, String basicPackage) {
|
|
|
List<PermVo> root = new ArrayList<>();
|
|
|
List<Permission> permissions = findPermissions(context, basicPackage);
|
|
|
- for(Permission permission : permissions) {
|
|
|
+ for (Permission permission : permissions) {
|
|
|
RequiresPermissions requiresPermissions = permission.getRequiresPermissions();
|
|
|
RequiresPermissionsDesc requiresPermissionsDesc = permission.getRequiresPermissionsDesc();
|
|
|
String api = permission.getApi();
|
|
|
|
|
|
String[] menus = requiresPermissionsDesc.menu();
|
|
|
- if(menus.length != 2){
|
|
|
+ if (menus.length != 2) {
|
|
|
throw new RuntimeException("目前只支持两级菜单");
|
|
|
}
|
|
|
String menu1 = menus[0];
|
|
|
PermVo perm1 = null;
|
|
|
- for(PermVo permVo : root){
|
|
|
- if(permVo.getLabel().equals(menu1)){
|
|
|
+ for (PermVo permVo : root) {
|
|
|
+ if (permVo.getLabel().equals(menu1)) {
|
|
|
perm1 = permVo;
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
- if(perm1 == null){
|
|
|
+ if (perm1 == null) {
|
|
|
perm1 = new PermVo();
|
|
|
perm1.setId(menu1);
|
|
|
perm1.setLabel(menu1);
|
|
|
@@ -47,13 +48,13 @@ public class PermissionUtil {
|
|
|
}
|
|
|
String menu2 = menus[1];
|
|
|
PermVo perm2 = null;
|
|
|
- for(PermVo permVo : perm1.getChildren()){
|
|
|
- if(permVo.getLabel().equals(menu2)){
|
|
|
+ for (PermVo permVo : perm1.getChildren()) {
|
|
|
+ if (permVo.getLabel().equals(menu2)) {
|
|
|
perm2 = permVo;
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
- if(perm2 == null){
|
|
|
+ if (perm2 == null) {
|
|
|
perm2 = new PermVo();
|
|
|
perm2.setId(menu2);
|
|
|
perm2.setLabel(menu2);
|
|
|
@@ -61,12 +62,28 @@ public class PermissionUtil {
|
|
|
perm1.getChildren().add(perm2);
|
|
|
}
|
|
|
|
|
|
- PermVo leftPerm = new PermVo();
|
|
|
- leftPerm.setId(requiresPermissions.value()[0]);
|
|
|
- leftPerm.setLabel(requiresPermissionsDesc.button());
|
|
|
- leftPerm.setApi(api);
|
|
|
+ String button = requiresPermissionsDesc.button();
|
|
|
+ PermVo leftPerm = null;
|
|
|
+ for (PermVo permVo : perm2.getChildren()) {
|
|
|
+ if (permVo.getLabel().equals(button)) {
|
|
|
+ leftPerm = permVo;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (leftPerm == null) {
|
|
|
+ leftPerm = new PermVo();
|
|
|
+ leftPerm.setId(requiresPermissions.value()[0]);
|
|
|
+ leftPerm.setLabel(requiresPermissionsDesc.button());
|
|
|
+ leftPerm.setApi(api);
|
|
|
+ perm2.getChildren().add(leftPerm);
|
|
|
+ }
|
|
|
+ else{
|
|
|
+ // TODO
|
|
|
+ // 目前限制Controller里面每个方法的RequiresPermissionsDesc注解是唯一的
|
|
|
+ // 如果允许相同,可能会造成内部权限不一致。
|
|
|
+ throw new RuntimeException("权限已经存在,不能添加新权限");
|
|
|
+ }
|
|
|
|
|
|
- perm2.getChildren().add(leftPerm);
|
|
|
}
|
|
|
return root;
|
|
|
}
|
|
|
@@ -74,9 +91,9 @@ public class PermissionUtil {
|
|
|
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()){
|
|
|
+ for (Map.Entry<String, Object> entry : map.entrySet()) {
|
|
|
Object bean = entry.getValue();
|
|
|
- if(!StringUtils.contains(ClassUtils.getPackageName(bean.getClass()), basicPackage)){
|
|
|
+ if (!StringUtils.contains(ClassUtils.getPackageName(bean.getClass()), basicPackage)) {
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
@@ -84,21 +101,21 @@ public class PermissionUtil {
|
|
|
Class controllerClz = clz.getSuperclass();
|
|
|
RequestMapping clazzRequestMapping = AnnotationUtils.findAnnotation(controllerClz, RequestMapping.class);
|
|
|
List<Method> methods = MethodUtils.getMethodsListWithAnnotation(controllerClz, RequiresPermissions.class);
|
|
|
- for(Method method : methods){
|
|
|
+ for (Method method : methods) {
|
|
|
RequiresPermissions requiresPermissions = AnnotationUtils.getAnnotation(method, RequiresPermissions.class);
|
|
|
RequiresPermissionsDesc requiresPermissionsDesc = AnnotationUtils.getAnnotation(method, RequiresPermissionsDesc.class);
|
|
|
|
|
|
- if(requiresPermissions == null || requiresPermissionsDesc == null){
|
|
|
+ if (requiresPermissions == null || requiresPermissionsDesc == null) {
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
String api = "";
|
|
|
- if(clazzRequestMapping != null){
|
|
|
+ if (clazzRequestMapping != null) {
|
|
|
api = clazzRequestMapping.value()[0];
|
|
|
}
|
|
|
|
|
|
PostMapping postMapping = AnnotationUtils.getAnnotation(method, PostMapping.class);
|
|
|
- if(postMapping != null){
|
|
|
+ if (postMapping != null) {
|
|
|
api = "POST " + api + postMapping.value()[0];
|
|
|
|
|
|
Permission permission = new Permission();
|
|
|
@@ -109,7 +126,7 @@ public class PermissionUtil {
|
|
|
continue;
|
|
|
}
|
|
|
GetMapping getMapping = AnnotationUtils.getAnnotation(method, GetMapping.class);
|
|
|
- if(getMapping != null){
|
|
|
+ if (getMapping != null) {
|
|
|
api = "GET " + api + getMapping.value()[0];
|
|
|
Permission permission = new Permission();
|
|
|
permission.setRequiresPermissions(requiresPermissions);
|