添加分配角色功能.

This commit is contained in:
lijiahang
2023-08-16 17:06:08 +08:00
parent 5ca656409d
commit 2521c856e2
23 changed files with 443 additions and 40 deletions

View File

@@ -70,19 +70,19 @@ public class SystemUserController {
@Operation(summary = "修改用户角色")
@PreAuthorize("@ss.hasPermission('infra:system-user:update-role')")
public Integer updateUserRole(@Validated @RequestBody SystemUserUpdateRoleRequest request) {
if (Lists.isEmpty(request.getRoles())) {
if (Lists.isEmpty(request.getRoleIdList())) {
// 删除用户角色
return systemUserRoleService.deleteUserRoles(request);
} else {
// 更新用户
// 更新用户
return systemUserRoleService.updateUserRoles(request);
}
}
@PutMapping("/reset-password")
@Operation(summary = "重置密码")
@Operation(summary = "重置用户密码")
@PreAuthorize("@ss.hasPermission('infra:system-user:reset-password')")
public HttpWrapper<?> resetPassword(@Validated @RequestBody UserResetPasswordRequest request) {
public HttpWrapper<?> resetUserPassword(@Validated @RequestBody UserResetPasswordRequest request) {
systemUserService.resetPassword(request);
return HttpWrapper.ok();
}
@@ -104,6 +104,14 @@ public class SystemUserController {
return systemUserService.getSystemUserList();
}
@IgnoreLog(IgnoreLogMode.RET)
@GetMapping("/get-roles")
@Operation(summary = "查询用户的角色id")
@PreAuthorize("@ss.hasPermission('infra:system-user:query')")
public List<Long> getUserRoleIdList(@RequestParam("userId") Long userId) {
return systemUserRoleService.getUserRoleIdList(userId);
}
@IgnoreLog(IgnoreLogMode.RET)
@PostMapping("/query")
@Operation(summary = "分页查询用户")

View File

@@ -7,6 +7,7 @@ import lombok.Data;
import lombok.NoArgsConstructor;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import java.io.Serializable;
@@ -26,6 +27,7 @@ public class SystemUserCreateRequest implements Serializable {
@NotBlank
@Size(max = 32)
@Pattern(regexp = "^[a-zA-Z0-9]{4,32}$")
@Schema(description = "用户名")
private String username;

View File

@@ -8,7 +8,7 @@ import lombok.NoArgsConstructor;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.Set;
import java.util.List;
/**
* 用户 更新角色请求对象
@@ -28,7 +28,7 @@ public class SystemUserUpdateRoleRequest implements Serializable {
@Schema(description = "id")
private Long id;
@Schema(description = "角色编码")
private Set<String> roles;
@Schema(description = "roleIdList")
private List<Long> roleIdList;
}

View File

@@ -31,9 +31,6 @@ public class SystemUserVO implements Serializable {
@Schema(description = "用户名")
private String username;
@Schema(description = "密码")
private String password;
@Schema(description = "花名")
private String nickname;

View File

@@ -13,6 +13,14 @@ import java.util.List;
*/
public interface SystemUserRoleService {
/**
* 查询用户 roleId
*
* @param userId userId
* @return roleId
*/
List<Long> getUserRoleIdList(Long userId);
/**
* 删除用户角色
*

View File

@@ -20,9 +20,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
/**
@@ -45,6 +43,11 @@ public class SystemUserRoleServiceImpl implements SystemUserRoleService {
@Resource
private SystemUserRoleDAO systemUserRoleDAO;
@Override
public List<Long> getUserRoleIdList(Long userId) {
return systemUserRoleDAO.selectRoleIdByUserId(userId);
}
@Override
public Integer deleteUserRoles(SystemUserUpdateRoleRequest request) {
Long userId = request.getId();
@@ -61,20 +64,20 @@ public class SystemUserRoleServiceImpl implements SystemUserRoleService {
@Transactional(rollbackFor = Exception.class)
public Integer updateUserRoles(SystemUserUpdateRoleRequest request) {
Long userId = request.getId();
Set<String> roleCodeList = request.getRoles();
List<Long> roleIdList = request.getRoleIdList();
// 查询用户
SystemUserDO record = systemUserDAO.selectById(userId);
Valid.notNull(record, ErrorMessage.USER_ABSENT);
// 查询角色
List<SystemRoleDO> userRoles = systemRoleDAO.selectByCodeList(roleCodeList);
List<SystemRoleDO> userRoles = systemRoleDAO.selectBatchIds(roleIdList);
// 检查角色是否存在
if (userRoles.size() != roleCodeList.size()) {
if (roleIdList.size() != userRoles.size()) {
// 有不存在的角色
List<String> userRoleCodes = userRoles.stream()
.map(SystemRoleDO::getCode)
List<Long> userRoleIdLists = userRoles.stream()
.map(SystemRoleDO::getId)
.collect(Collectors.toList());
for (String roleCode : roleCodeList) {
Valid.in(roleCode, userRoleCodes, ErrorMessage.ROLE_CODE_ABSENT, roleCode);
for (Long roleId : roleIdList) {
Valid.in(roleId, userRoleIdLists, ErrorMessage.ROLE_CODE_ABSENT, roleId);
}
}
// 删除用户角色关联
@@ -90,7 +93,10 @@ public class SystemUserRoleServiceImpl implements SystemUserRoleService {
effect += addUserRoles.size();
// 更新缓存中的角色
RedisUtils.<LoginUser>processSetJson(UserCacheKeyDefine.USER_INFO, s -> {
s.setRoles(new ArrayList<>(roleCodeList));
List<String> roleCodeList = userRoles.stream()
.map(SystemRoleDO::getCode)
.collect(Collectors.toList());
s.setRoles(roleCodeList);
}, userId);
return effect;
}

View File

@@ -144,8 +144,7 @@ public class SystemUserServiceImpl implements SystemUserService {
.like(SystemUserDO::getNickname, request.getNickname())
.like(SystemUserDO::getMobile, request.getMobile())
.like(SystemUserDO::getEmail, request.getEmail())
.eq(SystemUserDO::getStatus, request.getStatus())
.orderByDesc(SystemUserDO::getId);
.eq(SystemUserDO::getStatus, request.getStatus());
// 查询
return systemUserDAO.of()
.wrapper(wrapper)