mirror of
https://github.com/PlayEdu/PlayEdu
synced 2025-06-08 02:04:04 +08:00
管理员与角色绑定
This commit is contained in:
parent
e69798cdcb
commit
5cce777034
@ -2,10 +2,15 @@ package xyz.playedu.api.controller.backend;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
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.AdminRole;
|
||||
import xyz.playedu.api.domain.AdminUser;
|
||||
import xyz.playedu.api.domain.AdminUserRole;
|
||||
import xyz.playedu.api.request.backend.AdminUserRequest;
|
||||
import xyz.playedu.api.service.AdminRoleService;
|
||||
import xyz.playedu.api.service.AdminUserRoleService;
|
||||
import xyz.playedu.api.service.AdminUserService;
|
||||
import xyz.playedu.api.types.PaginationResult;
|
||||
import xyz.playedu.api.types.JsonResponse;
|
||||
@ -13,6 +18,8 @@ import xyz.playedu.api.util.HelperUtil;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@Slf4j
|
||||
@ -22,6 +29,12 @@ public class AdminUserController {
|
||||
@Autowired
|
||||
private AdminUserService adminUserService;
|
||||
|
||||
@Autowired
|
||||
private AdminRoleService roleService;
|
||||
|
||||
@Autowired
|
||||
private AdminUserRoleService userRoleService;
|
||||
|
||||
@GetMapping("/index")
|
||||
public JsonResponse Index(@RequestParam(name = "page", defaultValue = "1") Integer page, @RequestParam(name = "size", defaultValue = "10") Integer size) {
|
||||
PaginationResult<AdminUser> result = adminUserService.paginate(page, size, null);
|
||||
@ -39,10 +52,15 @@ public class AdminUserController {
|
||||
|
||||
@GetMapping("/create")
|
||||
public JsonResponse create() {
|
||||
return JsonResponse.success();
|
||||
List<AdminRole> roles = roleService.list();
|
||||
HashMap<String, Object> data = new HashMap<>();
|
||||
data.put("roles", roles);
|
||||
return JsonResponse.data(data);
|
||||
|
||||
}
|
||||
|
||||
@PostMapping("/create")
|
||||
@Transactional
|
||||
public JsonResponse store(@RequestBody @Validated AdminUserRequest request) {
|
||||
if (request.getPassword() == null || request.getPassword().length() == 0) {
|
||||
return JsonResponse.error("请输入密码");
|
||||
@ -67,6 +85,18 @@ public class AdminUserController {
|
||||
return JsonResponse.error("添加管理员失败");
|
||||
}
|
||||
|
||||
if (request.getRoleIds().length > 0) {
|
||||
List<AdminUserRole> userRoles = new ArrayList<>();
|
||||
for (int i = 0; i < request.getRoleIds().length; i++) {
|
||||
AdminUserRole userRole = new AdminUserRole();
|
||||
userRole.setAdminId(adminUser.getId());
|
||||
userRole.setRoleId(request.getRoleIds()[i]);
|
||||
userRoles.add(userRole);
|
||||
}
|
||||
userRoleService.saveBatch(userRoles);
|
||||
}
|
||||
|
||||
|
||||
return JsonResponse.success();
|
||||
}
|
||||
|
||||
@ -82,6 +112,7 @@ public class AdminUserController {
|
||||
}
|
||||
|
||||
@PutMapping("/{id}")
|
||||
@Transactional
|
||||
public JsonResponse update(@PathVariable Integer id, @RequestBody @Validated AdminUserRequest request) {
|
||||
AdminUser adminUser = adminUserService.findById(id);
|
||||
if (adminUser == null) {
|
||||
@ -110,14 +141,31 @@ public class AdminUserController {
|
||||
return JsonResponse.error("更新管理员资料失败");
|
||||
}
|
||||
|
||||
if (request.getRoleIds().length > 0) {
|
||||
// 先删除管理员与权限的已有关联关系
|
||||
userRoleService.removeUserRolesByUserId(adminUser.getId());
|
||||
|
||||
// 重新绑定关联关系
|
||||
List<AdminUserRole> userRoles = new ArrayList<>();
|
||||
for (int i = 0; i < request.getRoleIds().length; i++) {
|
||||
AdminUserRole userRole = new AdminUserRole();
|
||||
userRole.setAdminId(adminUser.getId());
|
||||
userRole.setRoleId(request.getRoleIds()[i]);
|
||||
userRoles.add(userRole);
|
||||
}
|
||||
userRoleService.saveBatch(userRoles);
|
||||
}
|
||||
|
||||
return JsonResponse.success();
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
@Transactional
|
||||
public JsonResponse destroy(@PathVariable Integer id) {
|
||||
if (!adminUserService.removeById(id)) {
|
||||
return JsonResponse.error("删除管理员失败");
|
||||
}
|
||||
userRoleService.removeUserRolesByUserId(id);
|
||||
return JsonResponse.success();
|
||||
}
|
||||
|
||||
|
67
src/main/java/xyz/playedu/api/domain/AdminUserRole.java
Normal file
67
src/main/java/xyz/playedu/api/domain/AdminUserRole.java
Normal 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_user_role
|
||||
*/
|
||||
@TableName(value ="admin_user_role")
|
||||
@Data
|
||||
public class AdminUserRole implements Serializable {
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private Integer adminId;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private Integer roleId;
|
||||
|
||||
@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;
|
||||
}
|
||||
AdminUserRole other = (AdminUserRole) that;
|
||||
return (this.getAdminId() == null ? other.getAdminId() == null : this.getAdminId().equals(other.getAdminId()))
|
||||
&& (this.getRoleId() == null ? other.getRoleId() == null : this.getRoleId().equals(other.getRoleId()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((getAdminId() == null) ? 0 : getAdminId().hashCode());
|
||||
result = prime * result + ((getRoleId() == null) ? 0 : getRoleId().hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append(getClass().getSimpleName());
|
||||
sb.append(" [");
|
||||
sb.append("Hash = ").append(hashCode());
|
||||
sb.append(", adminId=").append(adminId);
|
||||
sb.append(", roleId=").append(roleId);
|
||||
sb.append(", serialVersionUID=").append(serialVersionUID);
|
||||
sb.append("]");
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
package xyz.playedu.api.mapper;
|
||||
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import xyz.playedu.api.domain.AdminUserRole;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
|
||||
/**
|
||||
* @author tengteng
|
||||
* @description 针对表【admin_user_role】的数据库操作Mapper
|
||||
* @createDate 2023-02-21 16:25:43
|
||||
* @Entity xyz.playedu.api.domain.AdminUserRole
|
||||
*/
|
||||
@Mapper
|
||||
public interface AdminUserRoleMapper extends BaseMapper<AdminUserRole> {
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
@ -1,5 +1,6 @@
|
||||
package xyz.playedu.api.request.backend;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import jakarta.validation.constraints.Email;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import lombok.Data;
|
||||
@ -28,6 +29,11 @@ public class AdminUserRequest implements Serializable {
|
||||
|
||||
private String password;
|
||||
|
||||
@JsonProperty("is_ban_login")
|
||||
@NotNull(message = "is_ban_login参数不存在")
|
||||
private Integer isBanLogin;
|
||||
|
||||
@JsonProperty("role_ids")
|
||||
@NotNull(message = "role_ids参数不存在")
|
||||
private Integer[] roleIds;
|
||||
}
|
||||
|
@ -0,0 +1,15 @@
|
||||
package xyz.playedu.api.service;
|
||||
|
||||
import xyz.playedu.api.domain.AdminUserRole;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
|
||||
/**
|
||||
* @author tengteng
|
||||
* @description 针对表【admin_user_role】的数据库操作Service
|
||||
* @createDate 2023-02-21 16:25:43
|
||||
*/
|
||||
public interface AdminUserRoleService extends IService<AdminUserRole> {
|
||||
|
||||
void removeUserRolesByUserId(Integer userId);
|
||||
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
package xyz.playedu.api.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import xyz.playedu.api.domain.AdminUserRole;
|
||||
import xyz.playedu.api.service.AdminUserRoleService;
|
||||
import xyz.playedu.api.mapper.AdminUserRoleMapper;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* @author tengteng
|
||||
* @description 针对表【admin_user_role】的数据库操作Service实现
|
||||
* @createDate 2023-02-21 16:25:43
|
||||
*/
|
||||
@Service
|
||||
public class AdminUserRoleServiceImpl extends ServiceImpl<AdminUserRoleMapper, AdminUserRole>
|
||||
implements AdminUserRoleService {
|
||||
|
||||
@Override
|
||||
public void removeUserRolesByUserId(Integer userId) {
|
||||
remove(query().getWrapper().eq("admin_id", userId));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
15
src/main/resources/mapper/AdminUserRoleMapper.xml
Normal file
15
src/main/resources/mapper/AdminUserRoleMapper.xml
Normal 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.AdminUserRoleMapper">
|
||||
|
||||
<resultMap id="BaseResultMap" type="xyz.playedu.api.domain.AdminUserRole">
|
||||
<result property="adminId" column="admin_id" jdbcType="INTEGER"/>
|
||||
<result property="roleId" column="role_id" jdbcType="INTEGER"/>
|
||||
</resultMap>
|
||||
|
||||
<sql id="Base_Column_List">
|
||||
admin_id,role_id
|
||||
</sql>
|
||||
</mapper>
|
Loading…
x
Reference in New Issue
Block a user