自动载入权限实现

This commit is contained in:
张松 2025-02-19 17:14:45 +08:00
parent 77803e0b36
commit 9470735feb
3 changed files with 16 additions and 4 deletions

View File

@ -33,7 +33,7 @@ public class MerchantRegisterController {
* @return 激活码列表
*/
@SaAdminCheckRole("管理员")
@SaAdminCheckPermission("merchantRegister:list")
@SaAdminCheckPermission(value = "merchantRegister:list", name = "激活码列表")
@GetMapping
public CzgResult<Page<MerchantRegister>> get(PageDTO pageDTO, Integer state, String startTime, String endTime) {
return CzgResult.success(merchantRegisterService.get(pageDTO, state, startTime, endTime));

View File

@ -28,6 +28,8 @@ public @interface SaAdminCheckPermission {
@AliasFor(annotation = SaCheckPermission.class)
String [] value() default {};
String name() default "";
/**
* 验证模式AND | OR默认AND
* @return 验证模式

View File

@ -1,6 +1,7 @@
package com.czg;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.mybatisflex.core.row.Db;
import com.mybatisflex.core.row.Row;
import lombok.extern.slf4j.Slf4j;
@ -41,12 +42,15 @@ public class LoadingRole implements CommandLineRunner {
Method valueMethod = annotationClass.getMethod("value");
String[] permissions = (String[]) valueMethod.invoke(annotation);
Method nameMethod = annotationClass.getMethod("name");
String permissionName = (String) nameMethod.invoke(annotation);
for (String s : permissions) {
String sql = "select * from sys_menu where permission=?";
Row menu1 = Db.selectOneBySql(sql, s);
if (menu1 != null) {
Long menuId = menu1.getLong("menu_id");
String name = menu1.getString("name");
String title = menu1.getString("title");
String permission = menu1.getString("permission");
String listSql = "select * from sys_roles_menus where menu_id=? and role_id=?";
List<Row> count1 = Db.selectListBySql(listSql, menuId, 1L);
@ -54,14 +58,20 @@ public class LoadingRole implements CommandLineRunner {
if (count1.isEmpty()) {
sql = "INSERT INTO `sys_roles_menus` (`menu_id`, `role_id`) VALUES (?, ?);";
Db.insertBySql(sql, menu1, 1L);
log.info("接口菜单添加成功, 菜单名称: {}, 菜单权限: {}", name, permission);
log.info("接口菜单添加成功, 菜单名称: {}, 菜单权限: {}", title, permission);
}
if (StrUtil.isNotBlank(permissionName) && !title.equals(permissionName)) {
sql = "update sys_menu set title=? where menu_id=?";
Db.updateBySql(sql, permissionName, menuId);
log.info("接口菜单修改成功, 旧名称: {}, 新菜单名称: {}", title, permissionName);
}
continue;
}
sql = "INSERT INTO `czg_cashier`.`sys_menu` ( `sub_count`, `type`, `title`, `name`, `component`, `menu_sort`, `icon`, `path`, `i_frame`, `cache`, `hidden`, `permission`, `create_by`, `update_by`, `create_time`, `update_time`, `active_menu`, `is_shop`) VALUES " +
"(0, 2, ?, NULL, '', 2, '', '', b'0', b'0', b'0', ?, NULL, NULL, ?, NULL, NULL, 0);";
Db.insertBySql(sql, s, s, DateUtil.date());
Db.insertBySql(sql, StrUtil.isNotBlank(permissionName) ? permissionName : s, s, DateUtil.date());
sql = "select * from sys_menu where permission=?";
Row info = Db.selectOneBySql(sql, s);
Long menuId = info.getLong("menu_id");