管理员角色控制

This commit is contained in:
none 2023-02-21 16:21:48 +08:00
parent f3e1a6a0de
commit e69798cdcb
15 changed files with 471 additions and 30 deletions

View File

@ -29,6 +29,8 @@ public class AdminPermissionCheck implements ApplicationRunner {
{"管理员", "10", "管理员-编辑", BPermissionConstant.ADMIN_USER_UPDATE},
{"管理员", "15", "管理员-删除", BPermissionConstant.ADMIN_USER_DESTROY},
{"管理员角色", "0", "管理员角色", BPermissionConstant.ADMIN_ROLE},
{"部门", "0", "部门-查看", BPermissionConstant.DEPARTMENT_INDEX},
{"部门", "5", "部门-添加", BPermissionConstant.DEPARTMENT_STORE},
{"部门", "10", "部门-编辑", BPermissionConstant.DEPARTMENT_UPDATE},

View File

@ -14,6 +14,8 @@ public class BPermissionConstant {
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_ROLE = "admin-role";
public final static String DEPARTMENT_INDEX = "department-index";
public final static String DEPARTMENT_STORE = "department-store";
public final static String DEPARTMENT_UPDATE = "department-update";

View File

@ -1,30 +0,0 @@
package xyz.playedu.api.controller.backend;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import xyz.playedu.api.domain.AdminPermission;
import xyz.playedu.api.service.AdminPermissionService;
import xyz.playedu.api.types.JsonResponse;
import java.util.List;
/**
* @Author 杭州白书科技有限公司
* @create 2023/2/20 14:19
*/
@RestController
@RequestMapping("/backend/v1/admin-permission")
public class AdminPermissionController {
@Autowired
private AdminPermissionService permissionService;
@GetMapping("/index")
public JsonResponse index() {
List<AdminPermission> data = permissionService.listOrderBySortAsc();
return JsonResponse.data(data);
}
}

View File

@ -0,0 +1,127 @@
package xyz.playedu.api.controller.backend;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import xyz.playedu.api.domain.AdminPermission;
import xyz.playedu.api.domain.AdminRole;
import xyz.playedu.api.domain.AdminRolePermission;
import xyz.playedu.api.request.backend.AdminRoleRequest;
import xyz.playedu.api.service.AdminPermissionService;
import xyz.playedu.api.service.AdminRolePermissionService;
import xyz.playedu.api.service.AdminRoleService;
import xyz.playedu.api.types.JsonResponse;
import xyz.playedu.api.util.HelperUtil;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
/**
* @Author 杭州白书科技有限公司
* @create 2023/2/21 15:56
*/
@RestController
@RequestMapping("/backend/v1/admin-role")
public class AdminRoleController {
@Autowired
private AdminRoleService roleService;
@Autowired
private AdminPermissionService permissionService;
@Autowired
private AdminRolePermissionService rolePermissionService;
@GetMapping("/index")
public JsonResponse index() {
List<AdminRole> data = roleService.list();
return JsonResponse.data(data);
}
@GetMapping("/create")
public JsonResponse create() {
List<AdminPermission> permissions = permissionService.listOrderBySortAsc();
HashMap<String, Object> data = new HashMap<>();
data.put("permissions", permissions);
return JsonResponse.data(data);
}
@PostMapping("/create")
@Transactional
public JsonResponse store(@RequestBody @Validated AdminRoleRequest request) {
AdminRole role = new AdminRole();
role.setName(request.getName());
role.setSlug(HelperUtil.randomString(12));
role.setCreatedAt(new Date());
role.setUpdatedAt(new Date());
roleService.save(role);
if (request.getPermissionIds().length > 0) {
List<AdminRolePermission> rolePermissions = new ArrayList<>();
for (int i = 0; i < request.getPermissionIds().length; i++) {
AdminRolePermission rolePermission = new AdminRolePermission();
rolePermission.setRoleId(role.getId());
rolePermission.setPermId(request.getPermissionIds()[i]);
rolePermissions.add(rolePermission);
}
rolePermissionService.saveBatch(rolePermissions);
}
return JsonResponse.success();
}
@GetMapping("/{id}")
public JsonResponse edit(@PathVariable(name = "id") Integer id) {
AdminRole role = roleService.getById(id);
if (role == null) {
return JsonResponse.error("管理角色不存在");
}
return JsonResponse.data(role);
}
@PutMapping("/{id}")
@Transactional
public JsonResponse update(@PathVariable(name = "id") Integer id, @RequestBody @Validated AdminRoleRequest request) {
AdminRole role = roleService.getById(id);
if (role == null) {
return JsonResponse.error("管理角色不存在");
}
AdminRole newRole = new AdminRole();
newRole.setId(role.getId());
newRole.setName(request.getName());
roleService.updateById(newRole);
if (request.getPermissionIds().length > 0) {
// 先清空已有的权限
rolePermissionService.removeRolePermissionsByRoleId(role.getId());
// 重新关联权限
List<AdminRolePermission> rolePermissions = new ArrayList<>();
for (int i = 0; i < request.getPermissionIds().length; i++) {
AdminRolePermission rolePermission = new AdminRolePermission();
rolePermission.setRoleId(role.getId());
rolePermission.setPermId(request.getPermissionIds()[i]);
rolePermissions.add(rolePermission);
}
rolePermissionService.saveBatch(rolePermissions);
}
return JsonResponse.success();
}
@DeleteMapping("/{id}")
@Transactional
public JsonResponse destroy(@PathVariable(name = "id") Integer id) {
rolePermissionService.removeRolePermissionsByRoleId(id);
roleService.removeById(id);
return JsonResponse.success();
}
}

View File

@ -0,0 +1,93 @@
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 lombok.Data;
/**
*
* @TableName admin_roles
*/
@TableName(value ="admin_roles")
@Data
public class AdminRole implements Serializable {
/**
*
*/
@TableId(type = IdType.AUTO)
private Integer id;
/**
* 角色名
*/
private String name;
/**
* slug
*/
private String slug;
/**
*
*/
private Date createdAt;
/**
*
*/
private Date updatedAt;
@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;
}
AdminRole other = (AdminRole) that;
return (this.getId() == null ? other.getId() == null : this.getId().equals(other.getId()))
&& (this.getName() == null ? other.getName() == null : this.getName().equals(other.getName()))
&& (this.getSlug() == null ? other.getSlug() == null : this.getSlug().equals(other.getSlug()))
&& (this.getCreatedAt() == null ? other.getCreatedAt() == null : this.getCreatedAt().equals(other.getCreatedAt()))
&& (this.getUpdatedAt() == null ? other.getUpdatedAt() == null : this.getUpdatedAt().equals(other.getUpdatedAt()));
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((getId() == null) ? 0 : getId().hashCode());
result = prime * result + ((getName() == null) ? 0 : getName().hashCode());
result = prime * result + ((getSlug() == null) ? 0 : getSlug().hashCode());
result = prime * result + ((getCreatedAt() == null) ? 0 : getCreatedAt().hashCode());
result = prime * result + ((getUpdatedAt() == null) ? 0 : getUpdatedAt().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(", name=").append(name);
sb.append(", slug=").append(slug);
sb.append(", createdAt=").append(createdAt);
sb.append(", updatedAt=").append(updatedAt);
sb.append(", serialVersionUID=").append(serialVersionUID);
sb.append("]");
return sb.toString();
}
}

View File

@ -0,0 +1,67 @@
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 lombok.Data;
/**
*
* @TableName admin_role_permission
*/
@TableName(value ="admin_role_permission")
@Data
public class AdminRolePermission implements Serializable {
/**
*
*/
private Integer roleId;
/**
*
*/
private Integer permId;
@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;
}
AdminRolePermission other = (AdminRolePermission) that;
return (this.getRoleId() == null ? other.getRoleId() == null : this.getRoleId().equals(other.getRoleId()))
&& (this.getPermId() == null ? other.getPermId() == null : this.getPermId().equals(other.getPermId()));
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((getRoleId() == null) ? 0 : getRoleId().hashCode());
result = prime * result + ((getPermId() == null) ? 0 : getPermId().hashCode());
return result;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(getClass().getSimpleName());
sb.append(" [");
sb.append("Hash = ").append(hashCode());
sb.append(", roleId=").append(roleId);
sb.append(", permId=").append(permId);
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.AdminRole;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
/**
* @author tengteng
* @description 针对表admin_roles的数据库操作Mapper
* @createDate 2023-02-21 15:53:27
* @Entity xyz.playedu.api.domain.AdminRole
*/
@Mapper
public interface AdminRoleMapper extends BaseMapper<AdminRole> {
}

View File

@ -0,0 +1,20 @@
package xyz.playedu.api.mapper;
import org.apache.ibatis.annotations.Mapper;
import xyz.playedu.api.domain.AdminRolePermission;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
/**
* @author tengteng
* @description 针对表admin_role_permission的数据库操作Mapper
* @createDate 2023-02-21 16:07:01
* @Entity xyz.playedu.api.domain.AdminRolePermission
*/
@Mapper
public interface AdminRolePermissionMapper extends BaseMapper<AdminRolePermission> {
}

View File

@ -0,0 +1,29 @@
package xyz.playedu.api.request.backend;
import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
import org.hibernate.validator.constraints.Length;
import java.io.Serial;
import java.io.Serializable;
/**
* @Author 杭州白书科技有限公司
* @create 2023/2/21 16:00
*/
@Data
public class AdminRoleRequest implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
@NotNull(message = "请输入管理角色名")
@Length(min = 1, max = 12, message = "管理角色名长度在1-16个字符之间")
private String name;
@JsonProperty("permission_ids")
@NotNull(message = "请选择权限")
private Integer[] permissionIds;
}

View File

@ -0,0 +1,17 @@
package xyz.playedu.api.service;
import org.springframework.stereotype.Service;
import xyz.playedu.api.domain.AdminRolePermission;
import com.baomidou.mybatisplus.extension.service.IService;
/**
* @author tengteng
* @description 针对表admin_role_permission的数据库操作Service
* @createDate 2023-02-21 16:07:01
*/
public interface AdminRolePermissionService extends IService<AdminRolePermission> {
void removeRolePermissionsByRoleId(Integer roleId);
}

View File

@ -0,0 +1,13 @@
package xyz.playedu.api.service;
import xyz.playedu.api.domain.AdminRole;
import com.baomidou.mybatisplus.extension.service.IService;
/**
* @author tengteng
* @description 针对表admin_roles的数据库操作Service
* @createDate 2023-02-21 15:53:27
*/
public interface AdminRoleService extends IService<AdminRole> {
}

View File

@ -0,0 +1,25 @@
package xyz.playedu.api.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import xyz.playedu.api.domain.AdminRolePermission;
import xyz.playedu.api.service.AdminRolePermissionService;
import xyz.playedu.api.mapper.AdminRolePermissionMapper;
import org.springframework.stereotype.Service;
/**
* @author tengteng
* @description 针对表admin_role_permission的数据库操作Service实现
* @createDate 2023-02-21 16:07:01
*/
@Service
public class AdminRolePermissionServiceImpl extends ServiceImpl<AdminRolePermissionMapper, AdminRolePermission>
implements AdminRolePermissionService {
@Override
public void removeRolePermissionsByRoleId(Integer roleId) {
remove(query().getWrapper().eq("role_id", roleId));
}
}

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.AdminRole;
import xyz.playedu.api.service.AdminRoleService;
import xyz.playedu.api.mapper.AdminRoleMapper;
import org.springframework.stereotype.Service;
/**
* @author tengteng
* @description 针对表admin_roles的数据库操作Service实现
* @createDate 2023-02-21 15:53:27
*/
@Service
public class AdminRoleServiceImpl extends ServiceImpl<AdminRoleMapper, AdminRole>
implements AdminRoleService{
}

View File

@ -0,0 +1,19 @@
<?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.AdminRoleMapper">
<resultMap id="BaseResultMap" type="xyz.playedu.api.domain.AdminRole">
<id property="id" column="id" jdbcType="INTEGER"/>
<result property="name" column="name" jdbcType="VARCHAR"/>
<result property="slug" column="slug" jdbcType="VARCHAR"/>
<result property="createdAt" column="created_at" jdbcType="TIMESTAMP"/>
<result property="updatedAt" column="updated_at" jdbcType="TIMESTAMP"/>
</resultMap>
<sql id="Base_Column_List">
id,name,slug,
created_at,updated_at
</sql>
</mapper>

View File

@ -0,0 +1,15 @@
<?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.AdminRolePermissionMapper">
<resultMap id="BaseResultMap" type="xyz.playedu.api.domain.AdminRolePermission">
<result property="roleId" column="role_id" jdbcType="INTEGER"/>
<result property="permId" column="perm_id" jdbcType="INTEGER"/>
</resultMap>
<sql id="Base_Column_List">
role_id,perm_id
</sql>
</mapper>