mirror of
https://github.com/PlayEdu/PlayEdu
synced 2025-12-16 08:04:22 +08:00
管理员与角色绑定
This commit is contained in:
@@ -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();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user