优化checks

This commit is contained in:
none 2023-03-09 12:00:24 +08:00
parent b0d43447c6
commit 82f0783cb8
11 changed files with 413 additions and 95 deletions

View File

@ -5,13 +5,11 @@ import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import xyz.playedu.api.constant.BPermissionConstant;
import xyz.playedu.api.constant.BackendConstant;
import xyz.playedu.api.domain.AdminPermission;
import xyz.playedu.api.service.AdminPermissionService;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.*;
/**
* @Author 杭州白书科技有限公司
@ -23,79 +21,129 @@ public class AdminPermissionCheck implements ApplicationRunner {
@Autowired
private AdminPermissionService permissionService;
private final String[][] ACTION_PERMISSIONS = {
{"管理员", "0", "管理员-查看", BPermissionConstant.ADMIN_USER_INDEX},
{"管理员", "5", "管理员-添加", BPermissionConstant.ADMIN_USER_STORE},
{"管理员", "10", "管理员-编辑", BPermissionConstant.ADMIN_USER_UPDATE},
{"管理员", "15", "管理员-删除", BPermissionConstant.ADMIN_USER_DESTROY},
{"管理员角色", "0", "管理员角色", BPermissionConstant.ADMIN_ROLE},
{"部门", "5", "部门-添加", BPermissionConstant.DEPARTMENT_STORE},
{"部门", "10", "部门-编辑", BPermissionConstant.DEPARTMENT_UPDATE},
{"部门", "15", "部门-删除", BPermissionConstant.DEPARTMENT_DESTROY},
{"资源分类", "0", "资源分类", BPermissionConstant.RESOURCE_CATEGORY},
{"学员", "0", "学员-查看", BPermissionConstant.USER_INDEX},
{"学员", "5", "学员-添加", BPermissionConstant.USER_STORE},
{"学员", "10", "学员-编辑", BPermissionConstant.USER_UPDATE},
{"学员", "15", "学员-删除", BPermissionConstant.USER_DESTROY},
{"课程分类", "0", "课程分类", BPermissionConstant.COURSE_CATEGORY},
{"课程", "0", "课程", BPermissionConstant.COURSE},
};
private final String[][] DATA_PERMISSIONS = {
{"管理员", "0", "邮箱", BPermissionConstant.DATA_ADMIN_EMAIL},
{"学员", "0", "邮箱", BPermissionConstant.DATA_USER_EMAIL},
{"学员", "10", "姓名", BPermissionConstant.DATA_USER_NAME},
{"学员", "10", "身份证号", BPermissionConstant.DATA_USER_ID_CARD},
};
private final Map<String, Map<String, AdminPermission[]>> permissions = new HashMap<>() {{
put(BPermissionConstant.TYPE_ACTION, new HashMap<>() {{
put("管理员", new AdminPermission[]{
new AdminPermission() {{
setSort(0);
setName("列表");
setSlug(BPermissionConstant.ADMIN_USER_INDEX);
}},
new AdminPermission() {{
setSort(10);
setName("新增|编辑|删除");
setSlug(BPermissionConstant.ADMIN_USER_CUD);
}},
});
put("管理员角色", new AdminPermission[]{
new AdminPermission() {{
setSort(0);
setName("列表|新增|编辑|删除");
setSlug(BPermissionConstant.ADMIN_ROLE);
}},
});
put("部门", new AdminPermission[]{
new AdminPermission() {{
setSort(0);
setName("新增|编辑|删除");
setSlug(BPermissionConstant.DEPARTMENT_CUD);
}},
});
put("资源分类", new AdminPermission[]{
new AdminPermission() {{
setSort(0);
setName("新增|编辑|删除");
setSlug(BPermissionConstant.RESOURCE_CATEGORY);
}},
});
put("学员", new AdminPermission[]{
new AdminPermission() {{
setSort(0);
setName("列表");
setSlug(BPermissionConstant.USER_INDEX);
}},
new AdminPermission() {{
setSort(10);
setName("新增");
setSlug(BPermissionConstant.USER_STORE);
}},
new AdminPermission() {{
setSort(20);
setName("新增");
setSlug(BPermissionConstant.USER_STORE);
}},
new AdminPermission() {{
setSort(30);
setName("删除");
setSlug(BPermissionConstant.USER_DESTROY);
}},
});
put("课程", new AdminPermission[]{
new AdminPermission() {{
setSort(0);
setName("新增|编辑|删除");
setSlug(BPermissionConstant.COURSE);
}},
});
}});
put(BPermissionConstant.TYPE_DATA, new HashMap<>() {{
put("管理员", new AdminPermission[]{
new AdminPermission() {{
setSort(0);
setName("邮箱");
setSlug(BPermissionConstant.DATA_ADMIN_EMAIL);
}},
});
put("学员", new AdminPermission[]{
new AdminPermission() {{
setSort(0);
setName("邮箱");
setSlug(BPermissionConstant.DATA_USER_EMAIL);
}},
new AdminPermission() {{
setSort(10);
setName("姓名");
setSlug(BPermissionConstant.DATA_USER_NAME);
}},
new AdminPermission() {{
setSort(20);
setName("身份证号");
setSlug(BPermissionConstant.DATA_USER_ID_CARD);
}},
});
}});
}};
@Override
public void run(ApplicationArguments args) throws Exception {
public void run(ApplicationArguments args) {
HashMap<String, Boolean> slugs = permissionService.allSlugs();
List<AdminPermission> list = new ArrayList<>();
Date now = new Date();
for (int i = 0; i < ACTION_PERMISSIONS.length; i++) {
String[] item = ACTION_PERMISSIONS[i];
String tmpSlug = item[3];
if (slugs.get(tmpSlug) != null) {//已经存在
continue;
}
AdminPermission permission = new AdminPermission();
permissions.forEach((typeValue, group) -> {
group.forEach((groupNameValue, item) -> {
for (int i = 0; i < item.length; i++) {
AdminPermission permissionItem = item[i];
permission.setGroupName(item[0]);
permission.setSort(Integer.valueOf(item[1]));
permission.setName(item[2]);
permission.setSlug(tmpSlug);
permission.setType(BPermissionConstant.TYPE_ACTION);
permission.setCreatedAt(now);
if (slugs.get(permissionItem.getSlug()) != null) {
continue;
}
list.add(permission);
// 不存在
list.add(new AdminPermission() {{
setType(typeValue);
setGroupName(groupNameValue);
setSort(permissionItem.getSort());
setName(permissionItem.getName());
setSlug(permissionItem.getSlug());
setCreatedAt(now);
}});
}
});
});
if (list.size() > 0) {
permissionService.saveBatch(list);
}
for (int i = 0; i < DATA_PERMISSIONS.length; i++) {
String[] item = DATA_PERMISSIONS[i];
String tmpSlug = item[3];
if (slugs.get(tmpSlug) != null) {//已经存在
continue;
}
AdminPermission permission = new AdminPermission();
permission.setGroupName(item[0]);
permission.setSort(Integer.valueOf(item[1]));
permission.setName(item[2]);
permission.setSlug(tmpSlug);
permission.setType(BPermissionConstant.TYPE_DATA);
permission.setCreatedAt(now);
list.add(permission);
}
permissionService.saveBatch(list);
}
}

View File

@ -20,20 +20,20 @@ public class AdminRoleCheck implements ApplicationRunner {
@Autowired
private AdminRoleService adminRoleService;
private final AdminRole superRole = new AdminRole() {{
setName("超级管理员");
setSlug(BackendConstant.SUPER_ADMIN_ROLE);
setCreatedAt(new Date());
setCreatedAt(new Date());
}};
@Override
public void run(ApplicationArguments args) throws Exception {
AdminRole adminRole = adminRoleService.getBySlug(BackendConstant.SUPER_ADMIN_ROLE);
if (adminRole != null) {//已存在超级管理权限
return;
}
adminRole = new AdminRole();
adminRole.setName("超级管理角色");
adminRole.setSlug(BackendConstant.SUPER_ADMIN_ROLE);
adminRole.setCreatedAt(new Date());
adminRole.setUpdatedAt(new Date());
adminRoleService.save(adminRole);
adminRoleService.save(superRole);
}
}

View File

@ -10,15 +10,11 @@ public class BPermissionConstant {
public final static String TYPE_DATA = "data";
public final static String ADMIN_USER_INDEX = "admin-user-index";
public final static String ADMIN_USER_STORE = "admin-user-store";
public final static String ADMIN_USER_UPDATE = "admin-user-update";
public final static String ADMIN_USER_DESTROY = "admin-user-destroy";
public final static String ADMIN_USER_CUD = "admin-user-cud";
public final static String ADMIN_ROLE = "admin-role";
public final static String DEPARTMENT_STORE = "department-store";
public final static String DEPARTMENT_UPDATE = "department-update";
public final static String DEPARTMENT_DESTROY = "department-destroy";
public final static String DEPARTMENT_CUD = "department-cud";
public final static String RESOURCE_CATEGORY = "resource-category";
@ -27,13 +23,12 @@ public class BPermissionConstant {
public final static String USER_UPDATE = "user-update";
public final static String USER_DESTROY = "user-destroy";
public final static String COURSE_CATEGORY = "course—category";
public final static String COURSE = "course";
public final static String DATA_USER_NAME = "data-user-name";
public final static String DATA_USER_EMAIL = "data-user-email";
public final static String DATA_USER_ID_CARD = "data-user-id-card";
public final static String DATA_ADMIN_EMAIL = "data-admin-email";
}

View File

@ -49,7 +49,7 @@ public class AdminUserController {
return JsonResponse.data(result);
}
@BackendPermissionMiddleware(slug = BPermissionConstant.ADMIN_USER_STORE)
@BackendPermissionMiddleware(slug = BPermissionConstant.ADMIN_USER_CUD)
@GetMapping("/create")
public JsonResponse create() {
List<AdminRole> roles = roleService.list();
@ -60,7 +60,7 @@ public class AdminUserController {
return JsonResponse.data(data);
}
@BackendPermissionMiddleware(slug = BPermissionConstant.ADMIN_USER_STORE)
@BackendPermissionMiddleware(slug = BPermissionConstant.ADMIN_USER_CUD)
@PostMapping("/create")
public JsonResponse store(@RequestBody @Validated AdminUserRequest req) throws ServiceException {
if (req.getPassword() == null || req.getPassword().length() == 0) {
@ -72,7 +72,7 @@ public class AdminUserController {
return JsonResponse.success();
}
@BackendPermissionMiddleware(slug = BPermissionConstant.ADMIN_USER_UPDATE)
@BackendPermissionMiddleware(slug = BPermissionConstant.ADMIN_USER_CUD)
@GetMapping("/{id}")
public JsonResponse edit(@PathVariable Integer id) throws NotFoundException {
AdminUser adminUser = adminUserService.findOrFail(id);
@ -85,7 +85,7 @@ public class AdminUserController {
return JsonResponse.data(data);
}
@BackendPermissionMiddleware(slug = BPermissionConstant.ADMIN_USER_UPDATE)
@BackendPermissionMiddleware(slug = BPermissionConstant.ADMIN_USER_CUD)
@PutMapping("/{id}")
public JsonResponse update(@PathVariable Integer id, @RequestBody @Validated AdminUserRequest req) throws NotFoundException, ServiceException {
AdminUser adminUser = adminUserService.findOrFail(id);
@ -93,7 +93,7 @@ public class AdminUserController {
return JsonResponse.success();
}
@BackendPermissionMiddleware(slug = BPermissionConstant.ADMIN_USER_DESTROY)
@BackendPermissionMiddleware(slug = BPermissionConstant.ADMIN_USER_CUD)
@DeleteMapping("/{id}")
public JsonResponse destroy(@PathVariable Integer id) {
adminUserService.removeWithRoleIds(id);

View File

@ -0,0 +1,24 @@
package xyz.playedu.api.controller.backend;
import org.springframework.web.bind.annotation.*;
import xyz.playedu.api.types.JsonResponse;
/**
* @Author 杭州白书科技有限公司
* @create 2023/3/9 11:14
*/
@RestController
@RequestMapping("/backend/v1/app-config")
public class AppConfigController {
@GetMapping("/index")
public JsonResponse index() {
return JsonResponse.data(null);
}
@PutMapping("/index")
public JsonResponse save() {
return JsonResponse.data(null);
}
}

View File

@ -43,7 +43,7 @@ public class DepartmentController {
return JsonResponse.data(data);
}
@BackendPermissionMiddleware(slug = BPermissionConstant.DEPARTMENT_STORE)
@BackendPermissionMiddleware(slug = BPermissionConstant.DEPARTMENT_CUD)
@GetMapping("/create")
public JsonResponse create() {
Map<Integer, List<Department>> departments = departmentService.all().stream().collect(Collectors.groupingBy(Department::getParentId));
@ -54,21 +54,21 @@ public class DepartmentController {
return JsonResponse.data(data);
}
@BackendPermissionMiddleware(slug = BPermissionConstant.DEPARTMENT_STORE)
@BackendPermissionMiddleware(slug = BPermissionConstant.DEPARTMENT_CUD)
@PostMapping("/create")
public JsonResponse store(@RequestBody @Validated DepartmentRequest req) throws NotFoundException {
departmentService.create(req.getName(), req.getParentId(), req.getSort());
return JsonResponse.success();
}
@BackendPermissionMiddleware(slug = BPermissionConstant.DEPARTMENT_UPDATE)
@BackendPermissionMiddleware(slug = BPermissionConstant.DEPARTMENT_CUD)
@GetMapping("/{id}")
public JsonResponse edit(@PathVariable Integer id) throws NotFoundException {
Department department = departmentService.findOrFail(id);
return JsonResponse.data(department);
}
@BackendPermissionMiddleware(slug = BPermissionConstant.DEPARTMENT_UPDATE)
@BackendPermissionMiddleware(slug = BPermissionConstant.DEPARTMENT_CUD)
@PutMapping("/{id}")
public JsonResponse update(@PathVariable Integer id, @RequestBody DepartmentRequest req) throws NotFoundException {
Department department = departmentService.findOrFail(id);
@ -76,7 +76,7 @@ public class DepartmentController {
return JsonResponse.success();
}
@BackendPermissionMiddleware(slug = BPermissionConstant.DEPARTMENT_DESTROY)
@BackendPermissionMiddleware(slug = BPermissionConstant.DEPARTMENT_CUD)
@DeleteMapping("/{id}")
public JsonResponse destroy(@PathVariable Integer id) throws NotFoundException {
Department department = departmentService.findOrFail(id);

View File

@ -0,0 +1,166 @@
package xyz.playedu.api.domain;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import java.io.Serializable;
import java.util.Date;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
/**
* @TableName app_config
*/
@TableName(value = "app_config")
@Data
public class AppConfig implements Serializable {
/**
*
*/
@TableId(type = IdType.AUTO)
private Long id;
/**
* 分组
*/
private String group;
/**
* 名称
*/
private String name;
/**
* 升序
*/
private Integer sort;
/**
*
*/
@JsonProperty("field_type")
private String fieldType;
/**
*
*/
private String key;
/**
*
*/
private String value;
/**
* 默认值
*/
@JsonProperty("default_value")
private String defaultValue;
/**
* 可选值
*/
@JsonProperty("option_value")
private String optionValue;
/**
* 是否私密信息
*/
@JsonProperty("is_private")
private Integer isPrivate;
/**
* 帮助信息
*/
private String help;
/**
*
*/
@JsonIgnore
private Date createdAt;
/**
* 1显示,0否
*/
@JsonIgnore
private Integer isShow;
@TableField(exist = false)
private static final long serialVersionUID = 1L;
@Override
public boolean equals(Object that) {
if (this == that) {
return true;
}
if (that == null) {
return false;
}
if (getClass() != that.getClass()) {
return false;
}
AppConfig other = (AppConfig) that;
return (this.getId() == null ? other.getId() == null : this.getId().equals(other.getId()))
&& (this.getGroup() == null ? other.getGroup() == null : this.getGroup().equals(other.getGroup()))
&& (this.getName() == null ? other.getName() == null : this.getName().equals(other.getName()))
&& (this.getSort() == null ? other.getSort() == null : this.getSort().equals(other.getSort()))
&& (this.getFieldType() == null ? other.getFieldType() == null : this.getFieldType().equals(other.getFieldType()))
&& (this.getKey() == null ? other.getKey() == null : this.getKey().equals(other.getKey()))
&& (this.getValue() == null ? other.getValue() == null : this.getValue().equals(other.getValue()))
&& (this.getDefaultValue() == null ? other.getDefaultValue() == null : this.getDefaultValue().equals(other.getDefaultValue()))
&& (this.getOptionValue() == null ? other.getOptionValue() == null : this.getOptionValue().equals(other.getOptionValue()))
&& (this.getIsPrivate() == null ? other.getIsPrivate() == null : this.getIsPrivate().equals(other.getIsPrivate()))
&& (this.getHelp() == null ? other.getHelp() == null : this.getHelp().equals(other.getHelp()))
&& (this.getCreatedAt() == null ? other.getCreatedAt() == null : this.getCreatedAt().equals(other.getCreatedAt()))
&& (this.getIsShow() == null ? other.getIsShow() == null : this.getIsShow().equals(other.getIsShow()));
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((getId() == null) ? 0 : getId().hashCode());
result = prime * result + ((getGroup() == null) ? 0 : getGroup().hashCode());
result = prime * result + ((getName() == null) ? 0 : getName().hashCode());
result = prime * result + ((getSort() == null) ? 0 : getSort().hashCode());
result = prime * result + ((getFieldType() == null) ? 0 : getFieldType().hashCode());
result = prime * result + ((getKey() == null) ? 0 : getKey().hashCode());
result = prime * result + ((getValue() == null) ? 0 : getValue().hashCode());
result = prime * result + ((getDefaultValue() == null) ? 0 : getDefaultValue().hashCode());
result = prime * result + ((getOptionValue() == null) ? 0 : getOptionValue().hashCode());
result = prime * result + ((getIsPrivate() == null) ? 0 : getIsPrivate().hashCode());
result = prime * result + ((getHelp() == null) ? 0 : getHelp().hashCode());
result = prime * result + ((getCreatedAt() == null) ? 0 : getCreatedAt().hashCode());
result = prime * result + ((getIsShow() == null) ? 0 : getIsShow().hashCode());
return result;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(getClass().getSimpleName());
sb.append(" [");
sb.append("Hash = ").append(hashCode());
sb.append(", id=").append(id);
sb.append(", group=").append(group);
sb.append(", name=").append(name);
sb.append(", sort=").append(sort);
sb.append(", fieldType=").append(fieldType);
sb.append(", key=").append(key);
sb.append(", value=").append(value);
sb.append(", defaultValue=").append(defaultValue);
sb.append(", optionValue=").append(optionValue);
sb.append(", isPrivate=").append(isPrivate);
sb.append(", help=").append(help);
sb.append(", createdAt=").append(createdAt);
sb.append(", isShow=").append(isShow);
sb.append(", serialVersionUID=").append(serialVersionUID);
sb.append("]");
return sb.toString();
}
}

View File

@ -0,0 +1,20 @@
package xyz.playedu.api.mapper;
import org.apache.ibatis.annotations.Mapper;
import xyz.playedu.api.domain.AppConfig;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
/**
* @author tengteng
* @description 针对表app_config的数据库操作Mapper
* @createDate 2023-03-09 11:13:33
* @Entity xyz.playedu.api.domain.AppConfig
*/
@Mapper
public interface AppConfigMapper extends BaseMapper<AppConfig> {
}

View File

@ -0,0 +1,13 @@
package xyz.playedu.api.service;
import xyz.playedu.api.domain.AppConfig;
import com.baomidou.mybatisplus.extension.service.IService;
/**
* @author tengteng
* @description 针对表app_config的数据库操作Service
* @createDate 2023-03-09 11:13:33
*/
public interface AppConfigService extends IService<AppConfig> {
}

View File

@ -0,0 +1,22 @@
package xyz.playedu.api.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import xyz.playedu.api.domain.AppConfig;
import xyz.playedu.api.service.AppConfigService;
import xyz.playedu.api.mapper.AppConfigMapper;
import org.springframework.stereotype.Service;
/**
* @author tengteng
* @description 针对表app_config的数据库操作Service实现
* @createDate 2023-03-09 11:13:33
*/
@Service
public class AppConfigServiceImpl extends ServiceImpl<AppConfigMapper, AppConfig>
implements AppConfigService{
}

View File

@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="xyz.playedu.api.mapper.AppConfigMapper">
<resultMap id="BaseResultMap" type="xyz.playedu.api.domain.AppConfig">
<id property="id" column="id" jdbcType="BIGINT"/>
<result property="group" column="group" jdbcType="VARCHAR"/>
<result property="name" column="name" jdbcType="VARCHAR"/>
<result property="sort" column="sort" jdbcType="INTEGER"/>
<result property="fieldType" column="field_type" jdbcType="VARCHAR"/>
<result property="key" column="key" jdbcType="VARCHAR"/>
<result property="value" column="value" jdbcType="VARCHAR"/>
<result property="defaultValue" column="default_value" jdbcType="VARCHAR"/>
<result property="optionValue" column="option_value" jdbcType="VARCHAR"/>
<result property="isPrivate" column="is_private" jdbcType="TINYINT"/>
<result property="help" column="help" jdbcType="VARCHAR"/>
<result property="createdAt" column="created_at" jdbcType="TIMESTAMP"/>
<result property="isShow" column="is_show" jdbcType="TINYINT"/>
</resultMap>
<sql id="Base_Column_List">
id,group,name,
sort,field_type,key,
value,default_value,option_value,
is_private,help,created_at,
is_show
</sql>
</mapper>