实现用户/角色服务.

This commit is contained in:
lijiahang
2023-07-17 18:23:06 +08:00
parent 9c3fd1ef6e
commit e7c1995dab
52 changed files with 635 additions and 1358 deletions

View File

@@ -1,10 +1,10 @@
{
"local": {
"baseUrl": "http://127.0.0.1:9200/orion-api",
"token": "Bearer YzIckpmbXSoL6hYUsuqqZHJ8PxUeY19R"
"token": "Bearer YQJ3IpwJJv5HujIWY6ZTNDgUxXRY6aDt"
},
"gateway": {
"baseUrl": "http://127.0.0.1:9200/orion-api",
"token": "Bearer YzIckpmbXSoL6hYUsuqqZHJ8PxUeY19R"
"token": "Bearer YQJ3IpwJJv5HujIWY6ZTNDgUxXRY6aDt"
}
}

View File

@@ -41,11 +41,17 @@ public enum ErrorCode implements CodeInfo {
OTHER_DEVICE_LOGIN(700, "该账号于 {} 已在其他设备登陆 {}({})"),
USER_DISABLED(701, "当前用户已禁用"),
USER_LOCKED(702, "当前用户已被锁定"),
ROLE_PRESENT(703, "角色 [{}] 不存在"),
// -------------------- 自定义 - 通用 --------------------
NETWORK_FLUCTUATION(900, "当前环境网路波动"),
HTTP_API(901, "api 调用异常"),
HTTP_API_REQUEST_ERROR(901, "api 调用异常"),
IO_EXCEPTION(902, "网络异常"),
@@ -75,6 +81,8 @@ public enum ErrorCode implements CodeInfo {
DIABLED_ERROR(915, "数据已被禁用"),
UNSUPPOETED(916, "不支持此操作"),
;
ErrorCode(int code, String message) {

View File

@@ -13,7 +13,7 @@ public interface ErrorMessage {
String ID_MISSING = "id 不能为空";
String INVALID_PARAM = "参数错误";
String INVALID_PARAM = "参数验证失败";
String DATA_PRESENT = "数据已存在";
@@ -21,12 +21,20 @@ public interface ErrorMessage {
String CODE_PRESENT = "编码已存在";
String NICKNAME_PRESENT = "花名已存在";
String USERNAME_PRESENT = "用户名已存在";
String ROLE_ABSENT = "角色已存在";
String DATA_ABSENT = "数据不存在";
String USERNAME_PASSWORD_ERROR = "用户名或密码错误";
String MAX_LOGIN_FAILED = "登陆失败次数已上限";
String USER_ABSENT = "用户不存在";
String USER_DISABLED = "用户已被禁用";
String USER_LOCKED = "用户已被锁定";

View File

@@ -1,6 +1,7 @@
package com.orion.ops.framework.redis.config;
import com.orion.ops.framework.common.constant.AutoConfigureOrderConst;
import com.orion.ops.framework.redis.core.utils.RedisUtils;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.boot.autoconfigure.cache.CacheProperties;
@@ -29,18 +30,19 @@ public class OrionRedisAutoConfiguration {
/**
* @param redisConnectionFactory factory
* @param <T> T
* @return RedisTemplate
*/
@Bean
public <T> RedisTemplate<String, T> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, T> redisTemplate = new RedisTemplate<>();
public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, String> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
redisTemplate.setKeySerializer(RedisSerializer.string());
redisTemplate.setValueSerializer(RedisSerializer.string());
redisTemplate.setHashKeySerializer(RedisSerializer.string());
redisTemplate.setHashValueSerializer(RedisSerializer.string());
redisTemplate.afterPropertiesSet();
// 将其设置到 RedisUtils
RedisUtils.setRedisTemplate(redisTemplate);
return redisTemplate;
}

View File

@@ -1,5 +1,7 @@
package com.orion.ops.framework.redis.core.utils;
import com.alibaba.fastjson.JSON;
import com.orion.lang.define.cache.CacheKeyDefine;
import com.orion.lang.utils.io.Streams;
import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.RedisCallback;
@@ -8,6 +10,7 @@ import org.springframework.data.redis.core.ScanOptions;
import java.util.HashSet;
import java.util.Set;
import java.util.function.Consumer;
/**
* redis 工具类
@@ -18,32 +21,32 @@ import java.util.Set;
*/
public class RedisUtils {
private static RedisTemplate<String, String> redisTemplate;
private RedisUtils() {
}
/**
* 扫描key
* 扫描 key
*
* @param redisTemplate redisTemplate
* @param match 匹配值
* @param count 数量
* @param match 匹配值
* @param count 数量
* @return keys
*/
public static Set<String> scanKeys(RedisTemplate<?, ?> redisTemplate, String match, int count) {
return scanKeys(redisTemplate, ScanOptions.scanOptions()
public static Set<String> scanKeys(String match, int count) {
return scanKeys(ScanOptions.scanOptions()
.match(match)
.count(count)
.build());
}
/**
* 扫描key
* 扫描 key
*
* @param redisTemplate redisTemplate
* @param scanOptions scan
* @param scanOptions scan
* @return keys
*/
public static Set<String> scanKeys(RedisTemplate<?, ?> redisTemplate, ScanOptions scanOptions) {
public static Set<String> scanKeys(ScanOptions scanOptions) {
return redisTemplate.execute((RedisCallback<Set<String>>) connection -> {
Set<String> keys = new HashSet<>();
Cursor<byte[]> cursor = connection.scan(scanOptions);
@@ -55,4 +58,76 @@ public class RedisUtils {
});
}
/**
* 获取并且设置 json
*
* @param define define
* @param type type
* @param processor processor
* @param params params
* @param <T> type
*/
public static <T> void processSetJson(CacheKeyDefine define, Class<T> type, Consumer<T> processor, Object... params) {
processSetJson(define.format(params), define, type, processor);
}
/**
* 获取并且设置 json
*
* @param key key
* @param define define
* @param type type
* @param processor processor
* @param <T> type
*/
public static <T> void processSetJson(String key, CacheKeyDefine define, Class<T> type, Consumer<T> processor) {
String value = redisTemplate.opsForValue().get(key);
if (value == null) {
return;
}
// 转换
T cache = JSON.parseObject(value, type);
// 执行处理逻辑
processor.accept(cache);
// 重新设置
setJson(key, define, cache);
}
/**
* 设置 json
*
* @param key key
* @param define define
* @param value value
*/
public static void setJson(String key, CacheKeyDefine define, Object value) {
if (define.getTimeout() == 0) {
// 不过期
redisTemplate.opsForValue().set(key, JSON.toJSONString(value));
} else {
// 过期
redisTemplate.opsForValue().set(key, JSON.toJSONString(value),
define.getTimeout(),
define.getUnit());
}
}
/**
* 设置过期时间
*
* @param key key
* @param define define
*/
public static void setExpire(String key, CacheKeyDefine define) {
if (define.getTimeout() != 0) {
// 设置过期时间
redisTemplate.expire(key, define.getTimeout(), define.getUnit());
}
}
public static void setRedisTemplate(RedisTemplate<String, String> redisTemplate) {
RedisUtils.redisTemplate = redisTemplate;
}
}

View File

@@ -206,7 +206,7 @@ public class GlobalExceptionHandler {
@ExceptionHandler(value = {HttpRequestException.class})
public HttpWrapper<?> httpApiRequestExceptionHandler(Exception ex) {
log.error("httpApiRequestExceptionHandler", ex);
return ErrorCode.HTTP_API.getWrapper();
return ErrorCode.HTTP_API_REQUEST_ERROR.getWrapper();
}
@ExceptionHandler(value = VcsException.class)

View File

@@ -1,46 +0,0 @@
package com.orion.ops.module.infra.entity.dto;
import io.swagger.v3.oas.annotations.media.Schema;
import java.io.Serializable;
import lombok.*;
import java.util.*;
/**
* 角色菜单关联 业务对象
*
* @author Jiahang Li
* @version 1.0.0
* @since 2023-7-16 01:19
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Schema(name = "SystemRoleMenuDTO", description = "角色菜单关联 业务对象")
public class SystemRoleMenuDTO implements Serializable {
private static final long serialVersionUID = 1L;
@Schema(description = "id")
private Long id;
@Schema(description = "角色id")
private Long roleId;
@Schema(description = "菜单id")
private Long menuId;
@Schema(description = "创建时间")
private Date createTime;
@Schema(description = "修改时间")
private Date updateTime;
@Schema(description = "创建人")
private String creator;
@Schema(description = "修改人")
private String updater;
}

View File

@@ -1,46 +0,0 @@
package com.orion.ops.module.infra.entity.dto;
import io.swagger.v3.oas.annotations.media.Schema;
import java.io.Serializable;
import lombok.*;
import java.util.*;
/**
* 用户角色关联 业务对象
*
* @author Jiahang Li
* @version 1.0.0
* @since 2023-7-16 01:19
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Schema(name = "SystemUserRoleDTO", description = "用户角色关联 业务对象")
public class SystemUserRoleDTO implements Serializable {
private static final long serialVersionUID = 1L;
@Schema(description = "id")
private Long id;
@Schema(description = "用户id")
private Long userId;
@Schema(description = "角色id")
private Long roleId;
@Schema(description = "创建时间")
private Date createTime;
@Schema(description = "修改时间")
private Date updateTime;
@Schema(description = "创建人")
private String creator;
@Schema(description = "修改人")
private String updater;
}

View File

@@ -3,9 +3,12 @@ package com.orion.ops.module.infra.controller;
import com.orion.lang.define.wrapper.HttpWrapper;
import com.orion.ops.framework.common.annotation.IgnoreLog;
import com.orion.ops.framework.common.annotation.RestWrapper;
import com.orion.ops.module.infra.entity.request.auth.UserLoginRequest;
import com.orion.ops.framework.security.core.utils.SecurityUtils;
import com.orion.ops.module.infra.entity.request.user.UserLoginRequest;
import com.orion.ops.module.infra.entity.request.user.UserResetPasswordRequest;
import com.orion.ops.module.infra.entity.vo.UserLoginVO;
import com.orion.ops.module.infra.service.AuthenticationService;
import com.orion.ops.module.infra.service.SystemUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
@@ -35,6 +38,9 @@ public class AuthenticationController {
@Resource
private AuthenticationService authenticationService;
@Resource
private SystemUserService systemUserService;
@PermitAll
@Operation(summary = "登陆")
@PostMapping("/login")
@@ -53,16 +59,13 @@ public class AuthenticationController {
return HttpWrapper.ok();
}
@Operation(summary = "登陆")
@PutMapping("/reset-password")
public UserLoginVO resetPassword(@Validated @RequestBody UserLoginRequest request,
HttpServletRequest servletRequest) {
String token = authenticationService.login(request, servletRequest);
return UserLoginVO.builder().token(token).build();
@Operation(summary = "修改密码")
@PutMapping("/update-password")
public HttpWrapper<?> updatePassword(@Validated @RequestBody UserResetPasswordRequest request) {
// 当前用户id
request.setId(SecurityUtils.getLoginUserId());
systemUserService.resetPassword(request);
return HttpWrapper.ok();
}
// 修改密码
// 重置密码
}

View File

@@ -54,7 +54,7 @@ public class SystemRoleController {
@PutMapping("/update-status")
@Operation(summary = "通过 id 更新角色状态")
@PreAuthorize("@ss.hasPermission('infra:system-role:update')")
@PreAuthorize("@ss.hasPermission('infra:system-role:update-status')")
public Integer updateRoleStatus(@Validated @RequestBody SystemRoleStatusRequest request) {
return systemRoleService.updateRoleStatus(request);
}
@@ -90,13 +90,5 @@ public class SystemRoleController {
return systemRoleService.deleteSystemRole(id);
}
@PutMapping("/delete-batch")
@Operation(summary = "通过 id 批量删除角色")
@Parameter(name = "idList", description = "idList", required = true)
@PreAuthorize("@ss.hasPermission('infra:system-role:delete')")
public Integer batchDeleteSystemRole(@RequestParam("idList") List<Long> idList) {
return systemRoleService.batchDeleteSystemRole(idList);
}
}

View File

@@ -1,56 +0,0 @@
### 创建角色菜单关联
POST {{baseUrl}}/infra/system-role-menu/create
Content-Type: application/json
Authorization: {{token}}
{
"roleId": "",
"menuId": ""
}
### 通过 id 更新角色菜单关联
PUT {{baseUrl}}/infra/system-role-menu/update
Content-Type: application/json
Authorization: {{token}}
{
"id": "",
"roleId": "",
"menuId": ""
}
### 通过 id 查询角色菜单关联
GET {{baseUrl}}/infra/system-role-menu/get?id=1
Authorization: {{token}}
### 通过 id 批量查询角色菜单关联
GET {{baseUrl}}/infra/system-role-menu/list?idList=1,2,3
Authorization: {{token}}
### 分页查询角色菜单关联
POST {{baseUrl}}/infra/system-role-menu/query
Content-Type: application/json
Authorization: {{token}}
{
"id": "",
"roleId": "",
"menuId": ""
}
### 通过 id 删除角色菜单关联
DELETE {{baseUrl}}/infra/system-role-menu/delete?id=1
Authorization: {{token}}
### 通过 id 批量删除角色菜单关联
DELETE {{baseUrl}}/infra/system-role-menu/delete-batch?idList=1,2,3
Authorization: {{token}}

View File

@@ -1,94 +0,0 @@
package com.orion.ops.module.infra.controller;
import com.orion.lang.define.wrapper.DataGrid;
import com.orion.ops.framework.common.annotation.RestWrapper;
import com.orion.ops.module.infra.entity.request.menu.SystemRoleMenuCreateRequest;
import com.orion.ops.module.infra.entity.request.menu.SystemRoleMenuQueryRequest;
import com.orion.ops.module.infra.entity.request.menu.SystemRoleMenuUpdateRequest;
import com.orion.ops.module.infra.entity.vo.SystemRoleMenuVO;
import com.orion.ops.module.infra.service.SystemRoleMenuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
/**
* 角色菜单关联 api
*
* @author Jiahang Li
* @version 1.0.0
* @since 2023-7-16 01:19
*/
@Tag(name = "infra - 角色菜单关联服务")
@Slf4j
@Validated
@RestWrapper
@RestController
@RequestMapping("/infra/system-role-menu")
@SuppressWarnings({"ELValidationInJSP", "SpringElInspection"})
public class SystemRoleMenuController {
@Resource
private SystemRoleMenuService systemRoleMenuService;
@PostMapping("/create")
@Operation(summary = "创建角色菜单关联")
@PreAuthorize("@ss.hasPermission('infra:system-role-menu:create')")
public Long createSystemRoleMenu(@Validated @RequestBody SystemRoleMenuCreateRequest request) {
return systemRoleMenuService.createSystemRoleMenu(request);
}
@PutMapping("/update")
@Operation(summary = "通过 id 更新角色菜单关联")
@PreAuthorize("@ss.hasPermission('infra:system-role-menu:update')")
public Integer updateSystemRoleMenu(@Validated @RequestBody SystemRoleMenuUpdateRequest request) {
return systemRoleMenuService.updateSystemRoleMenu(request);
}
@GetMapping("/get")
@Operation(summary = "通过 id 查询角色菜单关联")
@Parameter(name = "id", description = "id", required = true)
@PreAuthorize("@ss.hasPermission('infra:system-role-menu:query')")
public SystemRoleMenuVO getSystemRoleMenu(@RequestParam("id") Long id) {
return systemRoleMenuService.getSystemRoleMenu(id);
}
@GetMapping("/list")
@Operation(summary = "通过 id 批量查询角色菜单关联")
@Parameter(name = "idList", description = "idList", required = true)
@PreAuthorize("@ss.hasPermission('infra:system-role-menu:query')")
public List<SystemRoleMenuVO> getSystemRoleMenuList(@RequestParam("idList") List<Long> idList) {
return systemRoleMenuService.getSystemRoleMenuList(idList);
}
@PostMapping("/query")
@Operation(summary = "分页查询角色菜单关联")
@PreAuthorize("@ss.hasPermission('infra:system-role-menu:query')")
public DataGrid<SystemRoleMenuVO> getSystemRoleMenuPage(@Validated @RequestBody SystemRoleMenuQueryRequest request) {
return systemRoleMenuService.getSystemRoleMenuPage(request);
}
@PutMapping("/delete")
@Operation(summary = "通过 id 删除角色菜单关联")
@Parameter(name = "id", description = "id", required = true)
@PreAuthorize("@ss.hasPermission('infra:system-role-menu:delete')")
public Integer deleteSystemRoleMenu(@RequestParam("id") Long id) {
return systemRoleMenuService.deleteSystemRoleMenu(id);
}
@PutMapping("/delete-batch")
@Operation(summary = "通过 id 批量删除角色菜单关联")
@Parameter(name = "idList", description = "idList", required = true)
@PreAuthorize("@ss.hasPermission('infra:system-role-menu:delete')")
public Integer batchDeleteSystemRoleMenu(@RequestParam("idList") List<Long> idList) {
return systemRoleMenuService.batchDeleteSystemRoleMenu(idList);
}
}

View File

@@ -1,11 +1,12 @@
package com.orion.ops.module.infra.controller;
import com.orion.lang.define.wrapper.DataGrid;
import com.orion.lang.define.wrapper.HttpWrapper;
import com.orion.lang.utils.collect.Lists;
import com.orion.ops.framework.common.annotation.RestWrapper;
import com.orion.ops.module.infra.entity.request.user.SystemUserCreateRequest;
import com.orion.ops.module.infra.entity.request.user.SystemUserQueryRequest;
import com.orion.ops.module.infra.entity.request.user.SystemUserUpdateRequest;
import com.orion.ops.module.infra.entity.request.user.*;
import com.orion.ops.module.infra.entity.vo.SystemUserVO;
import com.orion.ops.module.infra.service.SystemUserRoleService;
import com.orion.ops.module.infra.service.SystemUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
@@ -37,6 +38,9 @@ public class SystemUserController {
@Resource
private SystemUserService systemUserService;
@Resource
private SystemUserRoleService systemUserRoleService;
@PostMapping("/create")
@Operation(summary = "创建用户")
@PreAuthorize("@ss.hasPermission('infra:system-user:create')")
@@ -51,6 +55,36 @@ public class SystemUserController {
return systemUserService.updateSystemUser(request);
}
// TODO 修改头像
@PutMapping("/update-status")
@Operation(summary = "修改用户状态")
@PreAuthorize("@ss.hasPermission('infra:system-user:update-status')")
public Integer updateUserStatus(@Validated @RequestBody SystemUserUpdateStatusRequest request) {
return systemUserService.updateUserStatus(request);
}
@PutMapping("/update-role")
@Operation(summary = "修改用户角色")
@PreAuthorize("@ss.hasPermission('infra:system-user:update-role')")
public Integer updateUserRole(@Validated @RequestBody SystemUserUpdateRoleRequest request) {
if (Lists.isEmpty(request.getRoles())) {
// 删除用户角色
return systemUserRoleService.deleteUserRoles(request);
} else {
// 更新用户橘色
return systemUserRoleService.updateUserRoles(request);
}
}
@PutMapping("/reset-password")
@Operation(summary = "重置密码")
@PreAuthorize("@ss.hasPermission('infra:system-user:reset-password')")
public HttpWrapper<?> resetPassword(@Validated @RequestBody UserResetPasswordRequest request) {
systemUserService.resetPassword(request);
return HttpWrapper.ok();
}
@GetMapping("/get")
@Operation(summary = "通过 id 查询用户")
@Parameter(name = "id", description = "id", required = true)
@@ -82,16 +116,5 @@ public class SystemUserController {
return systemUserService.deleteSystemUser(id);
}
@PutMapping("/delete-batch")
@Operation(summary = "通过 id 批量删除用户")
@Parameter(name = "idList", description = "idList", required = true)
@PreAuthorize("@ss.hasPermission('infra:system-user:delete')")
public Integer batchDeleteSystemUser(@RequestParam("idList") List<Long> idList) {
return systemUserService.batchDeleteSystemUser(idList);
}
// 修改状态
// 设置角色
}

View File

@@ -1,56 +0,0 @@
### 创建用户角色关联
POST {{baseUrl}}/infra/system-user-role/create
Content-Type: application/json
Authorization: {{token}}
{
"userId": "",
"roleId": ""
}
### 通过 id 更新用户角色关联
PUT {{baseUrl}}/infra/system-user-role/update
Content-Type: application/json
Authorization: {{token}}
{
"id": "",
"userId": "",
"roleId": ""
}
### 通过 id 查询用户角色关联
GET {{baseUrl}}/infra/system-user-role/get?id=1
Authorization: {{token}}
### 通过 id 批量查询用户角色关联
GET {{baseUrl}}/infra/system-user-role/list?idList=1,2,3
Authorization: {{token}}
### 分页查询用户角色关联
POST {{baseUrl}}/infra/system-user-role/query
Content-Type: application/json
Authorization: {{token}}
{
"id": "",
"userId": "",
"roleId": ""
}
### 通过 id 删除用户角色关联
DELETE {{baseUrl}}/infra/system-user-role/delete?id=1
Authorization: {{token}}
### 通过 id 批量删除用户角色关联
DELETE {{baseUrl}}/infra/system-user-role/delete-batch?idList=1,2,3
Authorization: {{token}}

View File

@@ -1,94 +0,0 @@
package com.orion.ops.module.infra.controller;
import com.orion.lang.define.wrapper.DataGrid;
import com.orion.ops.framework.common.annotation.RestWrapper;
import com.orion.ops.module.infra.entity.request.role.SystemUserRoleCreateRequest;
import com.orion.ops.module.infra.entity.request.role.SystemUserRoleQueryRequest;
import com.orion.ops.module.infra.entity.request.role.SystemUserRoleUpdateRequest;
import com.orion.ops.module.infra.entity.vo.SystemUserRoleVO;
import com.orion.ops.module.infra.service.SystemUserRoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
/**
* 用户角色关联 api
*
* @author Jiahang Li
* @version 1.0.0
* @since 2023-7-16 01:19
*/
@Tag(name = "infra - 用户角色关联服务")
@Slf4j
@Validated
@RestWrapper
@RestController
@RequestMapping("/infra/system-user-role")
@SuppressWarnings({"ELValidationInJSP", "SpringElInspection"})
public class SystemUserRoleController {
@Resource
private SystemUserRoleService systemUserRoleService;
@PostMapping("/create")
@Operation(summary = "创建用户角色关联")
@PreAuthorize("@ss.hasPermission('infra:system-user-role:create')")
public Long createSystemUserRole(@Validated @RequestBody SystemUserRoleCreateRequest request) {
return systemUserRoleService.createSystemUserRole(request);
}
@PutMapping("/update")
@Operation(summary = "通过 id 更新用户角色关联")
@PreAuthorize("@ss.hasPermission('infra:system-user-role:update')")
public Integer updateSystemUserRole(@Validated @RequestBody SystemUserRoleUpdateRequest request) {
return systemUserRoleService.updateSystemUserRole(request);
}
@GetMapping("/get")
@Operation(summary = "通过 id 查询用户角色关联")
@Parameter(name = "id", description = "id", required = true)
@PreAuthorize("@ss.hasPermission('infra:system-user-role:query')")
public SystemUserRoleVO getSystemUserRole(@RequestParam("id") Long id) {
return systemUserRoleService.getSystemUserRole(id);
}
@GetMapping("/list")
@Operation(summary = "通过 id 批量查询用户角色关联")
@Parameter(name = "idList", description = "idList", required = true)
@PreAuthorize("@ss.hasPermission('infra:system-user-role:query')")
public List<SystemUserRoleVO> getSystemUserRoleList(@RequestParam("idList") List<Long> idList) {
return systemUserRoleService.getSystemUserRoleList(idList);
}
@PostMapping("/query")
@Operation(summary = "分页查询用户角色关联")
@PreAuthorize("@ss.hasPermission('infra:system-user-role:query')")
public DataGrid<SystemUserRoleVO> getSystemUserRolePage(@Validated @RequestBody SystemUserRoleQueryRequest request) {
return systemUserRoleService.getSystemUserRolePage(request);
}
@PutMapping("/delete")
@Operation(summary = "通过 id 删除用户角色关联")
@Parameter(name = "id", description = "id", required = true)
@PreAuthorize("@ss.hasPermission('infra:system-user-role:delete')")
public Integer deleteSystemUserRole(@RequestParam("id") Long id) {
return systemUserRoleService.deleteSystemUserRole(id);
}
@PutMapping("/delete-batch")
@Operation(summary = "通过 id 批量删除用户角色关联")
@Parameter(name = "idList", description = "idList", required = true)
@PreAuthorize("@ss.hasPermission('infra:system-user-role:delete')")
public Integer batchDeleteSystemUserRole(@RequestParam("idList") List<Long> idList) {
return systemUserRoleService.batchDeleteSystemUserRole(idList);
}
}

View File

@@ -1,35 +0,0 @@
package com.orion.ops.module.infra.convert;
import com.orion.ops.module.infra.entity.domain.SystemRoleMenuDO;
import com.orion.ops.module.infra.entity.request.menu.SystemRoleMenuCreateRequest;
import com.orion.ops.module.infra.entity.request.menu.SystemRoleMenuQueryRequest;
import com.orion.ops.module.infra.entity.request.menu.SystemRoleMenuUpdateRequest;
import com.orion.ops.module.infra.entity.vo.SystemRoleMenuVO;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
import java.util.List;
/**
* 角色菜单关联 内部对象转换器
*
* @author Jiahang Li
* @version 1.0.0
* @since 2023-7-16 01:19
*/
@Mapper
public interface SystemRoleMenuConvert {
SystemRoleMenuConvert MAPPER = Mappers.getMapper(SystemRoleMenuConvert.class);
SystemRoleMenuDO to(SystemRoleMenuCreateRequest request);
SystemRoleMenuDO to(SystemRoleMenuUpdateRequest request);
SystemRoleMenuDO to(SystemRoleMenuQueryRequest request);
SystemRoleMenuVO to(SystemRoleMenuDO domain);
List<SystemRoleMenuVO> to(List<SystemRoleMenuDO> list);
}

View File

@@ -1,32 +0,0 @@
package com.orion.ops.module.infra.convert;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
import com.orion.ops.module.infra.entity.domain.*;
import com.orion.ops.module.infra.entity.vo.*;
import com.orion.ops.module.infra.entity.dto.*;
import com.orion.ops.module.infra.entity.request.*;
import com.orion.ops.module.infra.convert.*;
import java.util.List;
/**
* 角色菜单关联 暴露服务转换器
*
* @author Jiahang Li
* @version 1.0.0
* @since 2023-7-16 01:19
*/
@Mapper
public interface SystemRoleMenuProviderConvert {
SystemRoleMenuProviderConvert MAPPER = Mappers.getMapper(SystemRoleMenuProviderConvert.class);
SystemRoleMenuDO to(SystemRoleMenuDTO dto);
SystemRoleMenuDTO to(SystemRoleMenuDO domain);
List<SystemRoleMenuDO> toDO(List<SystemRoleMenuDTO> list);
List<SystemRoleMenuDTO> toDTO(List<SystemRoleMenuDO> list);
}

View File

@@ -5,6 +5,7 @@ import com.orion.ops.module.infra.entity.domain.SystemUserDO;
import com.orion.ops.module.infra.entity.request.user.SystemUserCreateRequest;
import com.orion.ops.module.infra.entity.request.user.SystemUserQueryRequest;
import com.orion.ops.module.infra.entity.request.user.SystemUserUpdateRequest;
import com.orion.ops.module.infra.entity.request.user.SystemUserUpdateStatusRequest;
import com.orion.ops.module.infra.entity.vo.SystemUserVO;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
@@ -29,6 +30,8 @@ public interface SystemUserConvert {
SystemUserDO to(SystemUserQueryRequest request);
SystemUserDO to(SystemUserUpdateStatusRequest request);
SystemUserVO to(SystemUserDO domain);
List<SystemUserVO> to(List<SystemUserDO> list);

View File

@@ -1,35 +0,0 @@
package com.orion.ops.module.infra.convert;
import com.orion.ops.module.infra.entity.domain.SystemUserRoleDO;
import com.orion.ops.module.infra.entity.request.role.SystemUserRoleCreateRequest;
import com.orion.ops.module.infra.entity.request.role.SystemUserRoleQueryRequest;
import com.orion.ops.module.infra.entity.request.role.SystemUserRoleUpdateRequest;
import com.orion.ops.module.infra.entity.vo.SystemUserRoleVO;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
import java.util.List;
/**
* 用户角色关联 内部对象转换器
*
* @author Jiahang Li
* @version 1.0.0
* @since 2023-7-16 01:19
*/
@Mapper
public interface SystemUserRoleConvert {
SystemUserRoleConvert MAPPER = Mappers.getMapper(SystemUserRoleConvert.class);
SystemUserRoleDO to(SystemUserRoleCreateRequest request);
SystemUserRoleDO to(SystemUserRoleUpdateRequest request);
SystemUserRoleDO to(SystemUserRoleQueryRequest request);
SystemUserRoleVO to(SystemUserRoleDO domain);
List<SystemUserRoleVO> to(List<SystemUserRoleDO> list);
}

View File

@@ -1,32 +0,0 @@
package com.orion.ops.module.infra.convert;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
import com.orion.ops.module.infra.entity.domain.*;
import com.orion.ops.module.infra.entity.vo.*;
import com.orion.ops.module.infra.entity.dto.*;
import com.orion.ops.module.infra.entity.request.*;
import com.orion.ops.module.infra.convert.*;
import java.util.List;
/**
* 用户角色关联 暴露服务转换器
*
* @author Jiahang Li
* @version 1.0.0
* @since 2023-7-16 01:19
*/
@Mapper
public interface SystemUserRoleProviderConvert {
SystemUserRoleProviderConvert MAPPER = Mappers.getMapper(SystemUserRoleProviderConvert.class);
SystemUserRoleDO to(SystemUserRoleDTO dto);
SystemUserRoleDTO to(SystemUserRoleDO domain);
List<SystemUserRoleDO> toDO(List<SystemUserRoleDTO> list);
List<SystemUserRoleDTO> toDTO(List<SystemUserRoleDO> list);
}

View File

@@ -5,6 +5,9 @@ import com.orion.ops.framework.mybatis.core.mapper.IMapper;
import com.orion.ops.module.infra.entity.domain.SystemRoleDO;
import org.apache.ibatis.annotations.Mapper;
import java.util.Collection;
import java.util.List;
/**
* 角色 Mapper 接口
*
@@ -29,4 +32,16 @@ public interface SystemRoleDAO extends IMapper<SystemRoleDO> {
.eq(SystemRoleDO::getStatus, entity.getStatus());
}
/**
* 通过编码查询角色
*
* @param codeList codeList
* @return roles
*/
default List<SystemRoleDO> selectByCodeList(Collection<String> codeList) {
LambdaQueryWrapper<SystemRoleDO> wrapper = this.wrapper()
.in(SystemRoleDO::getCode, codeList);
return this.selectList(wrapper);
}
}

View File

@@ -28,4 +28,16 @@ public interface SystemRoleMenuDAO extends IMapper<SystemRoleMenuDO> {
.eq(SystemRoleMenuDO::getMenuId, entity.getMenuId());
}
/**
* 通过 roleId 删除
*
* @param roleId roleId
* @return effect
*/
default Integer deleteByRoleId(Long roleId) {
LambdaQueryWrapper<SystemRoleMenuDO> wrapper = this.wrapper()
.eq(SystemRoleMenuDO::getRoleId, roleId);
return this.delete(wrapper);
}
}

View File

@@ -32,12 +32,12 @@ public interface SystemUserRoleDAO extends IMapper<SystemUserRoleDO> {
}
/**
* 查询用户的全部角色id
* 查询用户的全部 roleId
*
* @param userId userId
* @return roleId
*/
default List<Long> selectRoleByUserId(Long userId) {
default List<Long> selectRoleIdByUserId(Long userId) {
LambdaQueryWrapper<SystemUserRoleDO> wrapper = this.wrapper()
.select(SystemUserRoleDO::getRoleId)
.eq(SystemUserRoleDO::getUserId, userId);
@@ -46,4 +46,43 @@ public interface SystemUserRoleDAO extends IMapper<SystemUserRoleDO> {
.collect(Collectors.toList());
}
/**
* 查询角色的全部 userId
*
* @param roleId roleId
* @return userId
*/
default List<Long> selectUserIdByRoleId(Long roleId) {
LambdaQueryWrapper<SystemUserRoleDO> wrapper = this.wrapper()
.select(SystemUserRoleDO::getUserId)
.eq(SystemUserRoleDO::getRoleId, roleId);
return this.selectList(wrapper).stream()
.map(SystemUserRoleDO::getUserId)
.collect(Collectors.toList());
}
/**
* 通过 userId 删除
*
* @param userId userId
* @return effect
*/
default Integer deleteByUserId(Long userId) {
LambdaQueryWrapper<SystemUserRoleDO> wrapper = this.wrapper()
.eq(SystemUserRoleDO::getUserId, userId);
return this.delete(wrapper);
}
/**
* 通过 roleId 删除
*
* @param roleId roleId
* @return effect
*/
default Integer deleteByRoleId(Long roleId) {
LambdaQueryWrapper<SystemUserRoleDO> wrapper = this.wrapper()
.eq(SystemUserRoleDO::getRoleId, roleId);
return this.delete(wrapper);
}
}

View File

@@ -13,7 +13,7 @@ import java.util.concurrent.TimeUnit;
*/
public interface UserCacheKeyDefine {
CacheKeyDefine USER_INFO = new CacheKeyDefine("user:info:{}", "用户信息 ${id}", 30, TimeUnit.DAYS);
CacheKeyDefine USER_INFO = new CacheKeyDefine("user:info:{}", "用户信息 ${id}");
CacheKeyDefine LOGIN_FAILED_COUNT = new CacheKeyDefine("user:failed:{}", "用户登陆失败次数 ${username}", 3, TimeUnit.DAYS);

View File

@@ -1,34 +0,0 @@
package com.orion.ops.module.infra.entity.request.menu;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
/**
* 角色菜单关联 创建请求对象
*
* @author Jiahang Li
* @version 1.0.0
* @since 2023-7-16 01:19
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Schema(name = "SystemRoleMenuCreateRequest", description = "角色菜单关联 创建请求对象")
public class SystemRoleMenuCreateRequest implements Serializable {
@NotNull
@Schema(description = "角色id")
private Long roleId;
@NotNull
@Schema(description = "菜单id")
private Long menuId;
}

View File

@@ -1,31 +0,0 @@
package com.orion.ops.module.infra.entity.request.menu;
import com.orion.ops.framework.common.entity.PageRequest;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;
/**
* 角色菜单关联 查询请求对象
*
* @author Jiahang Li
* @version 1.0.0
* @since 2023-7-16 01:19
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
@Schema(name = "SystemRoleMenuQueryRequest", description = "角色菜单关联 查询请求对象")
public class SystemRoleMenuQueryRequest extends PageRequest {
@Schema(description = "id")
private Long id;
@Schema(description = "角色id")
private Long roleId;
@Schema(description = "菜单id")
private Long menuId;
}

View File

@@ -1,38 +0,0 @@
package com.orion.ops.module.infra.entity.request.menu;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
/**
* 角色菜单关联 更新请求对象
*
* @author Jiahang Li
* @version 1.0.0
* @since 2023-7-16 01:19
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Schema(name = "SystemRoleMenuUpdateRequest", description = "角色菜单关联 更新请求对象")
public class SystemRoleMenuUpdateRequest implements Serializable {
@NotNull
@Schema(description = "id")
private Long id;
@NotNull
@Schema(description = "角色id")
private Long roleId;
@NotNull
@Schema(description = "菜单id")
private Long menuId;
}

View File

@@ -1,34 +0,0 @@
package com.orion.ops.module.infra.entity.request.role;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
/**
* 用户角色关联 创建请求对象
*
* @author Jiahang Li
* @version 1.0.0
* @since 2023-7-16 01:19
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Schema(name = "SystemUserRoleCreateRequest", description = "用户角色关联 创建请求对象")
public class SystemUserRoleCreateRequest implements Serializable {
@NotNull
@Schema(description = "用户id")
private Long userId;
@NotNull
@Schema(description = "角色id")
private Long roleId;
}

View File

@@ -1,31 +0,0 @@
package com.orion.ops.module.infra.entity.request.role;
import com.orion.ops.framework.common.entity.PageRequest;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;
/**
* 用户角色关联 查询请求对象
*
* @author Jiahang Li
* @version 1.0.0
* @since 2023-7-16 01:19
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
@Schema(name = "SystemUserRoleQueryRequest", description = "用户角色关联 查询请求对象")
public class SystemUserRoleQueryRequest extends PageRequest {
@Schema(description = "id")
private Long id;
@Schema(description = "用户id")
private Long userId;
@Schema(description = "角色id")
private Long roleId;
}

View File

@@ -1,38 +0,0 @@
package com.orion.ops.module.infra.entity.request.role;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
/**
* 用户角色关联 更新请求对象
*
* @author Jiahang Li
* @version 1.0.0
* @since 2023-7-16 01:19
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Schema(name = "SystemUserRoleUpdateRequest", description = "用户角色关联 更新请求对象")
public class SystemUserRoleUpdateRequest implements Serializable {
@NotNull
@Schema(description = "id")
private Long id;
@NotNull
@Schema(description = "用户id")
private Long userId;
@NotNull
@Schema(description = "角色id")
private Long roleId;
}

View File

@@ -7,10 +7,8 @@ import lombok.Data;
import lombok.NoArgsConstructor;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.Date;
/**
* 用户 创建请求对象
@@ -41,27 +39,12 @@ public class SystemUserCreateRequest implements Serializable {
@Schema(description = "花名")
private String nickname;
@Size(max = 500)
@NotBlank
@Schema(description = "头像地址")
private String avatar;
@Size(max = 15)
@NotBlank
@Schema(description = "手机号")
private String mobile;
@Size(max = 64)
@NotBlank
@Schema(description = "邮箱")
private String email;
@NotNull
@Schema(description = "用户状态 0停用 1启用 2锁定")
private Integer status;
@NotNull
@Schema(description = "最后登录时间")
private Date lastLoginTime;
}

View File

@@ -5,7 +5,6 @@ import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;
import javax.validation.constraints.Size;
import java.util.Date;
/**
* 用户 查询请求对象
@@ -29,18 +28,10 @@ public class SystemUserQueryRequest extends PageRequest {
@Schema(description = "用户名")
private String username;
@Size(max = 64)
@Schema(description = "密码")
private String password;
@Size(max = 16)
@Schema(description = "花名")
private String nickname;
@Size(max = 500)
@Schema(description = "头像地址")
private String avatar;
@Size(max = 15)
@Schema(description = "手机号")
private String mobile;
@@ -52,7 +43,4 @@ public class SystemUserQueryRequest extends PageRequest {
@Schema(description = "用户状态 0停用 1启用 2锁定")
private Integer status;
@Schema(description = "最后登录时间")
private Date lastLoginTime;
}

View File

@@ -10,7 +10,6 @@ import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.Date;
/**
* 用户 更新请求对象
@@ -30,42 +29,17 @@ public class SystemUserUpdateRequest implements Serializable {
@Schema(description = "id")
private Long id;
@Size(max = 32)
@NotBlank
@Schema(description = "用户名")
private String username;
@Size(max = 64)
@NotBlank
@Schema(description = "密码")
private String password;
@Size(max = 16)
@NotBlank
@Schema(description = "花名")
private String nickname;
@Size(max = 500)
@NotBlank
@Schema(description = "头像地址")
private String avatar;
@Size(max = 15)
@NotBlank
@Schema(description = "手机号")
private String mobile;
@Size(max = 64)
@NotBlank
@Schema(description = "邮箱")
private String email;
@NotNull
@Schema(description = "用户状态 0停用 1启用 2锁定")
private Integer status;
@NotNull
@Schema(description = "最后登录时间")
private Date lastLoginTime;
}

View File

@@ -0,0 +1,34 @@
package com.orion.ops.module.infra.entity.request.user;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.Set;
/**
* 用户 更新角色请求对象
*
* @author Jiahang Li
* @version 1.0.0
* @since 2023-7-16 00:03
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Schema(name = "SystemUserUpdateRoleRequest", description = "用户 更新角色请求对象")
public class SystemUserUpdateRoleRequest implements Serializable {
@NotNull
@Schema(description = "id")
private Long id;
@Schema(description = "角色编码")
private Set<String> roles;
}

View File

@@ -0,0 +1,34 @@
package com.orion.ops.module.infra.entity.request.user;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
/**
* 用户 更新状态请求对象
*
* @author Jiahang Li
* @version 1.0.0
* @since 2023-7-16 00:03
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Schema(name = "SystemUserUpdateStatusRequest", description = "用户 更新状态请求对象")
public class SystemUserUpdateStatusRequest implements Serializable {
@NotNull
@Schema(description = "id")
private Long id;
@NotNull
@Schema(description = "用户状态 0停用 1启用 2锁定")
private Integer status;
}

View File

@@ -1,4 +1,4 @@
package com.orion.ops.module.infra.entity.request.auth;
package com.orion.ops.module.infra.entity.request.user;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
@@ -13,6 +13,7 @@ import javax.validation.constraints.NotEmpty;
* @since 2023/7/13 22:16
*/
@Data
@Schema(name = "UserLoginRequest", description = "登陆请求")
public class UserLoginRequest {
@NotEmpty

View File

@@ -0,0 +1,26 @@
package com.orion.ops.module.infra.entity.request.user;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import javax.validation.constraints.NotEmpty;
/**
* 重置密码请求
*
* @author Jiahang Li
* @version 1.0.0
* @since 2023/7/17 12:19
*/
@Data
@Schema(name = "UserResetPasswordRequest", description = "重置密码请求")
public class UserResetPasswordRequest {
@Schema(description = "id")
private Long id;
@NotEmpty
@Schema(description = "密码")
private String password;
}

View File

@@ -1,46 +0,0 @@
package com.orion.ops.module.infra.entity.vo;
import io.swagger.v3.oas.annotations.media.Schema;
import java.io.Serializable;
import lombok.*;
import java.util.*;
/**
* 角色菜单关联 视图响应对象
*
* @author Jiahang Li
* @version 1.0.0
* @since 2023-7-16 01:19
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Schema(name = "SystemRoleMenuVO", description = "角色菜单关联 视图响应对象")
public class SystemRoleMenuVO implements Serializable {
private static final long serialVersionUID = 1L;
@Schema(description = "id")
private Long id;
@Schema(description = "角色id")
private Long roleId;
@Schema(description = "菜单id")
private Long menuId;
@Schema(description = "创建时间")
private Date createTime;
@Schema(description = "修改时间")
private Date updateTime;
@Schema(description = "创建人")
private String creator;
@Schema(description = "修改人")
private String updater;
}

View File

@@ -1,46 +0,0 @@
package com.orion.ops.module.infra.entity.vo;
import io.swagger.v3.oas.annotations.media.Schema;
import java.io.Serializable;
import lombok.*;
import java.util.*;
/**
* 用户角色关联 视图响应对象
*
* @author Jiahang Li
* @version 1.0.0
* @since 2023-7-16 01:19
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Schema(name = "SystemUserRoleVO", description = "用户角色关联 视图响应对象")
public class SystemUserRoleVO implements Serializable {
private static final long serialVersionUID = 1L;
@Schema(description = "id")
private Long id;
@Schema(description = "用户id")
private Long userId;
@Schema(description = "角色id")
private Long roleId;
@Schema(description = "创建时间")
private Date createTime;
@Schema(description = "修改时间")
private Date updateTime;
@Schema(description = "创建人")
private String creator;
@Schema(description = "修改人")
private String updater;
}

View File

@@ -33,4 +33,16 @@ public enum UserStatusEnum {
private final Integer status;
public static UserStatusEnum of(Integer status) {
if (status == null) {
return null;
}
for (UserStatusEnum value : values()) {
if (value.status.equals(status)) {
return value;
}
}
return null;
}
}

View File

@@ -7,6 +7,7 @@ import com.orion.ops.framework.security.core.service.SecurityFrameworkService;
import com.orion.ops.framework.security.core.utils.SecurityUtils;
import com.orion.ops.module.infra.entity.dto.LoginTokenDTO;
import com.orion.ops.module.infra.enums.LoginTokenStatusEnum;
import com.orion.ops.module.infra.enums.UserStatusEnum;
import com.orion.ops.module.infra.service.AuthenticationService;
import com.orion.ops.module.infra.service.PermissionService;
import org.springframework.stereotype.Service;
@@ -66,7 +67,10 @@ public class SecurityFrameworkServiceImpl implements SecurityFrameworkService {
// 检查 token 状态
this.checkTokenStatus(tokenInfo);
// 获取登陆信息
return authenticationService.getLoginUser(tokenInfo.getId());
LoginUser user = authenticationService.getLoginUser(tokenInfo.getId());
// 检查用户状态
this.checkUserStatus(user);
return user;
}
/**
@@ -89,4 +93,17 @@ public class SecurityFrameworkServiceImpl implements SecurityFrameworkService {
}
}
/**
* 检查用户状态
*
* @param user user
*/
private void checkUserStatus(LoginUser user) {
if (UserStatusEnum.DISABLED.getStatus().equals(user.getStatus())) {
throw ErrorCode.USER_DISABLED.exception();
} else if (UserStatusEnum.LOCKED.getStatus().equals(user.getStatus())) {
throw ErrorCode.USER_LOCKED.exception();
}
}
}

View File

@@ -2,7 +2,7 @@ package com.orion.ops.module.infra.service;
import com.orion.ops.framework.common.security.LoginUser;
import com.orion.ops.module.infra.entity.dto.LoginTokenDTO;
import com.orion.ops.module.infra.entity.request.auth.UserLoginRequest;
import com.orion.ops.module.infra.entity.request.user.UserLoginRequest;
import javax.servlet.http.HttpServletRequest;
@@ -15,6 +15,16 @@ import javax.servlet.http.HttpServletRequest;
*/
public interface AuthenticationService {
// TODO 配置化
// 允许多端登陆
boolean allowMultiDevice = true;
// 允许凭证续签
boolean allowRefresh = true;
// 凭证续签最大次数
int maxRefreshCount = 3;
// 失败锁定次数
int maxFailedLoginCount = 5;
/**
* 登陆
*

View File

@@ -1,13 +1,5 @@
package com.orion.ops.module.infra.service;
import com.orion.lang.define.wrapper.DataGrid;
import com.orion.ops.module.infra.entity.request.menu.SystemRoleMenuCreateRequest;
import com.orion.ops.module.infra.entity.request.menu.SystemRoleMenuQueryRequest;
import com.orion.ops.module.infra.entity.request.menu.SystemRoleMenuUpdateRequest;
import com.orion.ops.module.infra.entity.vo.SystemRoleMenuVO;
import java.util.List;
/**
* 角色菜单关联 服务类
*
@@ -17,60 +9,4 @@ import java.util.List;
*/
public interface SystemRoleMenuService {
/**
* 创建角色菜单关联
*
* @param request request
* @return id
*/
Long createSystemRoleMenu(SystemRoleMenuCreateRequest request);
/**
* 通过 id 更新角色菜单关联
*
* @param request request
* @return effect
*/
Integer updateSystemRoleMenu(SystemRoleMenuUpdateRequest request);
/**
* 通过 id 查询角色菜单关联
*
* @param id id
* @return row
*/
SystemRoleMenuVO getSystemRoleMenu(Long id);
/**
* 通过 id 批量查询角色菜单关联
*
* @param idList idList
* @return rows
*/
List<SystemRoleMenuVO> getSystemRoleMenuList(List<Long> idList);
/**
* 分页查询角色菜单关联
*
* @param request request
* @return rows
*/
DataGrid<SystemRoleMenuVO> getSystemRoleMenuPage(SystemRoleMenuQueryRequest request);
/**
* 通过 id 删除角色菜单关联
*
* @param id id
* @return effect
*/
Integer deleteSystemRoleMenu(Long id);
/**
* 通过 id 批量删除角色菜单关联
*
* @param idList idList
* @return effect
*/
Integer batchDeleteSystemRoleMenu(List<Long> idList);
}

View File

@@ -74,12 +74,4 @@ public interface SystemRoleService {
*/
Integer deleteSystemRole(Long id);
/**
* 通过 id 批量删除角色
*
* @param idList idList
* @return effect
*/
Integer batchDeleteSystemRole(List<Long> idList);
}

View File

@@ -1,10 +1,6 @@
package com.orion.ops.module.infra.service;
import com.orion.lang.define.wrapper.DataGrid;
import com.orion.ops.module.infra.entity.request.role.SystemUserRoleCreateRequest;
import com.orion.ops.module.infra.entity.request.role.SystemUserRoleQueryRequest;
import com.orion.ops.module.infra.entity.request.role.SystemUserRoleUpdateRequest;
import com.orion.ops.module.infra.entity.vo.SystemUserRoleVO;
import com.orion.ops.module.infra.entity.request.user.SystemUserUpdateRoleRequest;
import java.util.List;
@@ -18,59 +14,27 @@ import java.util.List;
public interface SystemUserRoleService {
/**
* 创建用户角色关联
*
* @param request request
* @return id
*/
Long createSystemUserRole(SystemUserRoleCreateRequest request);
/**
* 通过 id 更新用户角色关联
* 删除用户角色
*
* @param request request
* @return effect
*/
Integer updateSystemUserRole(SystemUserRoleUpdateRequest request);
Integer deleteUserRoles(SystemUserUpdateRoleRequest request);
/**
* 通过 id 查询用户角色关联
*
* @param id id
* @return row
*/
SystemUserRoleVO getSystemUserRole(Long id);
/**
* 通过 id 批量查询用户角色关联
*
* @param idList idList
* @return rows
*/
List<SystemUserRoleVO> getSystemUserRoleList(List<Long> idList);
/**
* 分页查询用户角色关联
* 更新用户角色
*
* @param request request
* @return rows
*/
DataGrid<SystemUserRoleVO> getSystemUserRolePage(SystemUserRoleQueryRequest request);
/**
* 通过 id 删除用户角色关联
*
* @param id id
* @return effect
*/
Integer deleteSystemUserRole(Long id);
Integer updateUserRoles(SystemUserUpdateRoleRequest request);
/**
* 通过 id 批量删除用户角色关联
* 删除用户缓存中的角色
*
* @param idList idList
* @return effect
* @param roleCode roleCode
* @param userIdList userIdList
*/
Integer batchDeleteSystemUserRole(List<Long> idList);
void asyncDeleteUserCacheRole(String roleCode, List<Long> userIdList);
}

View File

@@ -1,9 +1,7 @@
package com.orion.ops.module.infra.service;
import com.orion.lang.define.wrapper.DataGrid;
import com.orion.ops.module.infra.entity.request.user.SystemUserCreateRequest;
import com.orion.ops.module.infra.entity.request.user.SystemUserQueryRequest;
import com.orion.ops.module.infra.entity.request.user.SystemUserUpdateRequest;
import com.orion.ops.module.infra.entity.request.user.*;
import com.orion.ops.module.infra.entity.vo.SystemUserVO;
import java.util.List;
@@ -33,6 +31,14 @@ public interface SystemUserService {
*/
Integer updateSystemUser(SystemUserUpdateRequest request);
/**
* 修改用户状态
*
* @param request request
* @return effect
*/
Integer updateUserStatus(SystemUserUpdateStatusRequest request);
/**
* 通过 id 查询用户
*
@@ -66,11 +72,10 @@ public interface SystemUserService {
Integer deleteSystemUser(Long id);
/**
* 通过 id 批量删除用户
* 重置密码
*
* @param idList idList
* @return effect
* @param request request
*/
Integer batchDeleteSystemUser(List<Long> idList);
void resetPassword(UserResetPasswordRequest request);
}

View File

@@ -23,7 +23,7 @@ import com.orion.ops.module.infra.define.UserCacheKeyDefine;
import com.orion.ops.module.infra.entity.domain.SystemRoleDO;
import com.orion.ops.module.infra.entity.domain.SystemUserDO;
import com.orion.ops.module.infra.entity.dto.LoginTokenDTO;
import com.orion.ops.module.infra.entity.request.auth.UserLoginRequest;
import com.orion.ops.module.infra.entity.request.user.UserLoginRequest;
import com.orion.ops.module.infra.enums.LoginTokenStatusEnum;
import com.orion.ops.module.infra.enums.UserStatusEnum;
import com.orion.ops.module.infra.service.AuthenticationService;
@@ -51,16 +51,6 @@ import java.util.stream.Collectors;
@Service
public class AuthenticationServiceImpl implements AuthenticationService {
// TODO 想想看 如何配置化
// 允许多端登陆
private final boolean allowMultiDevice = true;
// 允许凭证续签
private final boolean allowRefresh = true;
// 凭证续签最大次数
private final int maxRefreshCount = 3;
// 失败锁定次数
private final int maxFailedLoginCount = 5;
@Resource
private ValueCrypto valueCrypto;
@@ -162,14 +152,10 @@ public class AuthenticationServiceImpl implements AuthenticationService {
int refreshCount = refresh.getRefreshCount() + 1;
refresh.setRefreshCount(refreshCount);
// 设置登陆缓存
redisTemplate.opsForValue().set(loginKey, JSON.toJSONString(refresh),
UserCacheKeyDefine.LOGIN_TOKEN.getTimeout(),
UserCacheKeyDefine.LOGIN_TOKEN.getUnit());
RedisUtils.setJson(loginKey, UserCacheKeyDefine.LOGIN_TOKEN, refresh);
if (refreshCount < maxRefreshCount) {
// 小于续签最大次数 则再次设置 refreshToken
redisTemplate.opsForValue().set(refreshKey, JSON.toJSONString(refresh),
UserCacheKeyDefine.LOGIN_REFRESH.getTimeout(),
UserCacheKeyDefine.LOGIN_REFRESH.getUnit());
RedisUtils.setJson(refreshKey, UserCacheKeyDefine.LOGIN_REFRESH, refresh);
} else {
// 大于等于续签最大次数 则删除
redisTemplate.delete(refreshKey);
@@ -229,6 +215,7 @@ public class AuthenticationServiceImpl implements AuthenticationService {
// 刷新登陆失败缓存
String failedCountKey = UserCacheKeyDefine.LOGIN_FAILED_COUNT.format(request.getUsername());
Long failedLoginCount = redisTemplate.opsForValue().increment(failedCountKey);
RedisUtils.setExpire(failedCountKey, UserCacheKeyDefine.LOGIN_FAILED_COUNT);
// 用户不存在
if (user == null) {
return false;
@@ -250,9 +237,7 @@ public class AuthenticationServiceImpl implements AuthenticationService {
// 修改缓存状态
LoginUser loginUser = JSON.parseObject(userInfoCache, LoginUser.class);
loginUser.setStatus(UserStatusEnum.LOCKED.getStatus());
redisTemplate.opsForValue().set(userInfoKey, JSON.toJSONString(loginUser),
UserCacheKeyDefine.USER_INFO.getTimeout(),
UserCacheKeyDefine.USER_INFO.getUnit());
RedisUtils.setJson(userInfoKey, UserCacheKeyDefine.USER_INFO, loginUser);
}
return false;
}
@@ -302,7 +287,7 @@ public class AuthenticationServiceImpl implements AuthenticationService {
// 设置缓存
LoginUser loginUser = SystemUserConvert.MAPPER.toLoginUser(user);
// 查询用户角色
List<Long> roleIds = systemUserRoleDAO.selectRoleByUserId(id);
List<Long> roleIds = systemUserRoleDAO.selectRoleIdByUserId(id);
List<String> roleCodeList = permissionService.getRoleCache()
.values()
.stream()
@@ -310,9 +295,7 @@ public class AuthenticationServiceImpl implements AuthenticationService {
.map(SystemRoleDO::getCode)
.collect(Collectors.toList());
loginUser.setRoles(roleCodeList);
redisTemplate.opsForValue().set(userInfoKey, JSON.toJSONString(loginUser),
UserCacheKeyDefine.USER_INFO.getTimeout(),
UserCacheKeyDefine.USER_INFO.getUnit());
RedisUtils.setJson(userInfoKey, UserCacheKeyDefine.USER_INFO, loginUser);
return loginUser;
}
@@ -327,7 +310,7 @@ public class AuthenticationServiceImpl implements AuthenticationService {
private void invalidOtherDeviceToken(Long id, long loginTime, String remoteAddr, String location) {
String loginKey = UserCacheKeyDefine.LOGIN_TOKEN.format(id, "*");
// 获取登陆信息
Set<String> loginKeyList = RedisUtils.scanKeys(redisTemplate, loginKey, 100);
Set<String> loginKeyList = RedisUtils.scanKeys(loginKey, 100);
if (!loginKeyList.isEmpty()) {
// 获取有效登陆信息
List<LoginTokenDTO> loginTokenInfoList = redisTemplate.opsForValue()
@@ -344,15 +327,13 @@ public class AuthenticationServiceImpl implements AuthenticationService {
loginTokenInfo.setLoginTime(loginTime);
loginTokenInfo.setIp(remoteAddr);
loginTokenInfo.setLocation(location);
redisTemplate.opsForValue().set(deviceLoginKey, JSON.toJSONString(loginTokenInfo),
UserCacheKeyDefine.LOGIN_TOKEN.getTimeout(),
UserCacheKeyDefine.LOGIN_TOKEN.getUnit());
RedisUtils.setJson(deviceLoginKey, UserCacheKeyDefine.LOGIN_TOKEN, loginTokenInfo);
}
}
// 删除续签信息
if (allowRefresh) {
String refreshKey = UserCacheKeyDefine.LOGIN_REFRESH.format(id, "*");
Set<String> refreshKeyList = RedisUtils.scanKeys(redisTemplate, refreshKey, 100);
Set<String> refreshKeyList = RedisUtils.scanKeys(refreshKey, 100);
if (!refreshKeyList.isEmpty()) {
redisTemplate.delete(refreshKeyList);
}
@@ -381,15 +362,11 @@ public class AuthenticationServiceImpl implements AuthenticationService {
.loginTime(loginTime)
.location(location)
.build();
redisTemplate.opsForValue().set(loginKey, JSON.toJSONString(loginValue),
UserCacheKeyDefine.LOGIN_TOKEN.getTimeout(),
UserCacheKeyDefine.LOGIN_TOKEN.getUnit());
RedisUtils.setJson(loginKey, UserCacheKeyDefine.LOGIN_TOKEN, loginValue);
// 生成 refreshToken
if (allowRefresh) {
String refreshKey = UserCacheKeyDefine.LOGIN_REFRESH.format(id, loginTime);
redisTemplate.opsForValue().set(refreshKey, JSON.toJSONString(loginValue),
UserCacheKeyDefine.LOGIN_REFRESH.getTimeout(),
UserCacheKeyDefine.LOGIN_REFRESH.getUnit());
RedisUtils.setJson(refreshKey, UserCacheKeyDefine.LOGIN_REFRESH, loginValue);
}
// 返回token
return CryptoUtils.encryptBase62(id + ":" + loginTime);

View File

@@ -25,8 +25,6 @@ import java.util.stream.Collectors;
/**
* 权限服务
* <p>
* TODO 分布式缓存解决方案?
*
* @author Jiahang Li
* @version 1.0.0

View File

@@ -1,24 +1,11 @@
package com.orion.ops.module.infra.service.impl;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.orion.lang.define.wrapper.DataGrid;
import com.orion.lang.utils.Valid;
import com.orion.lang.utils.collect.Lists;
import com.orion.ops.framework.common.constant.ErrorMessage;
import com.orion.ops.module.infra.convert.SystemRoleMenuConvert;
import com.orion.ops.module.infra.dao.SystemRoleMenuDAO;
import com.orion.ops.module.infra.entity.domain.SystemRoleMenuDO;
import com.orion.ops.module.infra.entity.request.menu.SystemRoleMenuCreateRequest;
import com.orion.ops.module.infra.entity.request.menu.SystemRoleMenuQueryRequest;
import com.orion.ops.module.infra.entity.request.menu.SystemRoleMenuUpdateRequest;
import com.orion.ops.module.infra.entity.vo.SystemRoleMenuVO;
import com.orion.ops.module.infra.service.SystemRoleMenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
/**
* 角色菜单关联 服务实现类
@@ -34,100 +21,4 @@ public class SystemRoleMenuServiceImpl implements SystemRoleMenuService {
@Resource
private SystemRoleMenuDAO systemRoleMenuDAO;
@Override
public Long createSystemRoleMenu(SystemRoleMenuCreateRequest request) {
// 转换
SystemRoleMenuDO record = SystemRoleMenuConvert.MAPPER.to(request);
record.setId(null);
// 查询数据是否冲突
this.checkSystemRoleMenuPresent(record);
// 插入
int effect = systemRoleMenuDAO.insert(record);
log.info("SystemRoleMenuService-createSystemRoleMenu effect: {}, record: {}", effect, JSON.toJSONString(record));
return record.getId();
}
@Override
public Integer updateSystemRoleMenu(SystemRoleMenuUpdateRequest request) {
// 查询
Long id = Valid.notNull(request.getId(), ErrorMessage.ID_MISSING);
SystemRoleMenuDO record = systemRoleMenuDAO.selectById(id);
Valid.notNull(record, ErrorMessage.DATA_ABSENT);
// 转换
SystemRoleMenuDO updateRecord = SystemRoleMenuConvert.MAPPER.to(request);
// 查询数据是否冲突
this.checkSystemRoleMenuPresent(updateRecord);
// 更新
int effect = systemRoleMenuDAO.updateById(updateRecord);
log.info("SystemRoleMenuService-updateSystemRoleMenu effect: {}, updateRecord: {}", effect, JSON.toJSONString(updateRecord));
return effect;
}
@Override
public SystemRoleMenuVO getSystemRoleMenu(Long id) {
// 查询
SystemRoleMenuDO record = systemRoleMenuDAO.selectById(id);
Valid.notNull(record, ErrorMessage.DATA_ABSENT);
// 转换
return SystemRoleMenuConvert.MAPPER.to(record);
}
@Override
public List<SystemRoleMenuVO> getSystemRoleMenuList(List<Long> idList) {
// 查询
List<SystemRoleMenuDO> records = systemRoleMenuDAO.selectBatchIds(idList);
if (records.isEmpty()) {
return Lists.empty();
}
// 转换
return SystemRoleMenuConvert.MAPPER.to(records);
}
@Override
public DataGrid<SystemRoleMenuVO> getSystemRoleMenuPage(SystemRoleMenuQueryRequest request) {
// 构造条件
LambdaQueryWrapper<SystemRoleMenuDO> wrapper = systemRoleMenuDAO.wrapper()
.eq(SystemRoleMenuDO::getId, request.getId())
.eq(SystemRoleMenuDO::getRoleId, request.getRoleId())
.eq(SystemRoleMenuDO::getMenuId, request.getMenuId())
.orderByDesc(SystemRoleMenuDO::getId);
// 查询
return systemRoleMenuDAO.of()
.wrapper(wrapper)
.page(request)
.dataGrid(SystemRoleMenuConvert.MAPPER::to);
}
@Override
public Integer deleteSystemRoleMenu(Long id) {
int effect = systemRoleMenuDAO.deleteById(id);
log.info("SystemRoleMenuService-deleteSystemRoleMenu id: {}, effect: {}", id, effect);
return effect;
}
@Override
public Integer batchDeleteSystemRoleMenu(List<Long> idList) {
int effect = systemRoleMenuDAO.deleteBatchIds(idList);
log.info("SystemRoleMenuService-batchDeleteSystemRoleMenu idList: {}, effect: {}", JSON.toJSONString(idList), effect);
return effect;
}
/**
* 检测对象是否存在
*
* @param domain domain
*/
private void checkSystemRoleMenuPresent(SystemRoleMenuDO domain) {
// 构造条件
LambdaQueryWrapper<SystemRoleMenuDO> wrapper = systemRoleMenuDAO.wrapper()
// 更新时忽略当前记录
.ne(SystemRoleMenuDO::getId, domain.getId())
// 用其他字段做重复校验
.eq(SystemRoleMenuDO::getRoleId, domain.getRoleId())
.eq(SystemRoleMenuDO::getMenuId, domain.getMenuId());
// 检查是否存在
boolean present = systemRoleMenuDAO.of().wrapper(wrapper).present();
Valid.isFalse(present, ErrorMessage.DATA_PRESENT);
}
}

View File

@@ -8,6 +8,8 @@ import com.orion.lang.utils.collect.Lists;
import com.orion.ops.framework.common.constant.ErrorMessage;
import com.orion.ops.module.infra.convert.SystemRoleConvert;
import com.orion.ops.module.infra.dao.SystemRoleDAO;
import com.orion.ops.module.infra.dao.SystemRoleMenuDAO;
import com.orion.ops.module.infra.dao.SystemUserRoleDAO;
import com.orion.ops.module.infra.entity.domain.SystemRoleDO;
import com.orion.ops.module.infra.entity.request.role.SystemRoleCreateRequest;
import com.orion.ops.module.infra.entity.request.role.SystemRoleQueryRequest;
@@ -17,12 +19,13 @@ import com.orion.ops.module.infra.entity.vo.SystemRoleVO;
import com.orion.ops.module.infra.enums.RoleStatusEnum;
import com.orion.ops.module.infra.service.PermissionService;
import com.orion.ops.module.infra.service.SystemRoleService;
import com.orion.ops.module.infra.service.SystemUserRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
/**
* 角色 服务实现类
@@ -38,9 +41,18 @@ public class SystemRoleServiceImpl implements SystemRoleService {
@Resource
private SystemRoleDAO systemRoleDAO;
@Resource
private SystemUserRoleDAO systemUserRoleDAO;
@Resource
private SystemRoleMenuDAO systemRoleMenuDAO;
@Resource
private PermissionService permissionService;
@Resource
private SystemUserRoleService systemUserRoleService;
@Override
public Long createSystemRole(SystemRoleCreateRequest request) {
// 转换
@@ -63,13 +75,11 @@ public class SystemRoleServiceImpl implements SystemRoleService {
// 查询
Long id = Valid.notNull(request.getId(), ErrorMessage.ID_MISSING);
SystemRoleDO record = systemRoleDAO.selectById(id);
Valid.notNull(record, ErrorMessage.DATA_ABSENT);
Valid.notNull(record, ErrorMessage.ROLE_ABSENT);
// 转换
SystemRoleDO updateRecord = SystemRoleConvert.MAPPER.to(request);
// 查询名称是否存在
this.checkNamePresent(updateRecord);
// 查询编码是否存在
this.checkCodePresent(updateRecord);
// 更新
int effect = systemRoleDAO.updateById(updateRecord);
log.info("SystemRoleService-updateSystemRole effect: {}, updateRecord: {}", effect, JSON.toJSONString(updateRecord));
@@ -100,9 +110,9 @@ public class SystemRoleServiceImpl implements SystemRoleService {
@Override
public SystemRoleVO getSystemRole(Long id) {
// 查询
// 查询角色
SystemRoleDO record = systemRoleDAO.selectById(id);
Valid.notNull(record, ErrorMessage.DATA_ABSENT);
Valid.notNull(record, ErrorMessage.ROLE_ABSENT);
// 转换
return SystemRoleConvert.MAPPER.to(record);
}
@@ -123,7 +133,7 @@ public class SystemRoleServiceImpl implements SystemRoleService {
// 构造条件
LambdaQueryWrapper<SystemRoleDO> wrapper = systemRoleDAO.wrapper()
.eq(SystemRoleDO::getId, request.getId())
.eq(SystemRoleDO::getName, request.getName())
.like(SystemRoleDO::getName, request.getName())
.eq(SystemRoleDO::getCode, request.getCode())
.eq(SystemRoleDO::getStatus, request.getStatus())
.orderByDesc(SystemRoleDO::getId);
@@ -135,30 +145,27 @@ public class SystemRoleServiceImpl implements SystemRoleService {
}
@Override
@Transactional(rollbackFor = Exception.class)
public Integer deleteSystemRole(Long id) {
// 查询角色
SystemRoleDO record = systemRoleDAO.selectById(id);
Valid.notNull(record, ErrorMessage.DATA_ABSENT);
String code = record.getCode();
// 删除角色
int effect = systemRoleDAO.deleteById(id);
log.info("SystemRoleService-deleteSystemRole id: {}, effect: {}", id, effect);
// 删除缓存
Map<String, SystemRoleDO> roleCache = permissionService.getRoleCache();
roleCache.values()
.stream()
.filter(s -> s.getId().equals(id))
.findFirst()
.ifPresent(s -> roleCache.remove(s.getCode()));
return effect;
}
@Override
public Integer batchDeleteSystemRole(List<Long> idList) {
int effect = systemRoleDAO.deleteBatchIds(idList);
log.info("SystemRoleService-batchDeleteSystemRole idList: {}, effect: {}", JSON.toJSONString(idList), effect);
// 删除缓存
Map<String, SystemRoleDO> roleCache = permissionService.getRoleCache();
roleCache.values()
.stream()
.filter(s -> idList.contains(s.getId()))
.map(SystemRoleDO::getCode)
.forEach(roleCache::remove);
// 查询用户角色关联
List<Long> userIdList = systemUserRoleDAO.selectUserIdByRoleId(id);
// 删除用户角色关联
effect += systemUserRoleDAO.deleteByRoleId(id);
// 删除角色菜单关联
effect += systemRoleMenuDAO.deleteByRoleId(id);
// 删除角色缓存
permissionService.getRoleCache().remove(code);
// 删除菜单缓存
permissionService.getRoleMenuCache().remove(code);
// 删除用户缓存中的角色
systemUserRoleService.asyncDeleteUserCacheRole(code, userIdList);
return effect;
}

View File

@@ -1,24 +1,30 @@
package com.orion.ops.module.infra.service.impl;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.orion.lang.define.wrapper.DataGrid;
import com.orion.lang.utils.Valid;
import com.orion.lang.utils.collect.Lists;
import com.orion.ops.framework.common.constant.ErrorCode;
import com.orion.ops.framework.common.constant.ErrorMessage;
import com.orion.ops.module.infra.convert.SystemUserRoleConvert;
import com.orion.ops.framework.common.security.LoginUser;
import com.orion.ops.framework.redis.core.utils.RedisUtils;
import com.orion.ops.module.infra.dao.SystemRoleDAO;
import com.orion.ops.module.infra.dao.SystemUserDAO;
import com.orion.ops.module.infra.dao.SystemUserRoleDAO;
import com.orion.ops.module.infra.define.UserCacheKeyDefine;
import com.orion.ops.module.infra.entity.domain.SystemRoleDO;
import com.orion.ops.module.infra.entity.domain.SystemUserDO;
import com.orion.ops.module.infra.entity.domain.SystemUserRoleDO;
import com.orion.ops.module.infra.entity.request.role.SystemUserRoleCreateRequest;
import com.orion.ops.module.infra.entity.request.role.SystemUserRoleQueryRequest;
import com.orion.ops.module.infra.entity.request.role.SystemUserRoleUpdateRequest;
import com.orion.ops.module.infra.entity.vo.SystemUserRoleVO;
import com.orion.ops.module.infra.entity.request.user.SystemUserUpdateRoleRequest;
import com.orion.ops.module.infra.service.SystemUserRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
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;
/**
* 用户角色关联 服务实现类
@@ -31,103 +37,80 @@ import java.util.List;
@Service
public class SystemUserRoleServiceImpl implements SystemUserRoleService {
@Resource
private SystemUserDAO systemUserDAO;
@Resource
private SystemRoleDAO systemRoleDAO;
@Resource
private SystemUserRoleDAO systemUserRoleDAO;
@Override
public Long createSystemUserRole(SystemUserRoleCreateRequest request) {
// 转换
SystemUserRoleDO record = SystemUserRoleConvert.MAPPER.to(request);
record.setId(null);
// 查询数据是否冲突
this.checkSystemUserRolePresent(record);
// 插入
int effect = systemUserRoleDAO.insert(record);
log.info("SystemUserRoleService-createSystemUserRole effect: {}, record: {}", effect, JSON.toJSONString(record));
return record.getId();
}
@Override
public Integer updateSystemUserRole(SystemUserRoleUpdateRequest request) {
// 查询
Long id = Valid.notNull(request.getId(), ErrorMessage.ID_MISSING);
SystemUserRoleDO record = systemUserRoleDAO.selectById(id);
Valid.notNull(record, ErrorMessage.DATA_ABSENT);
// 转换
SystemUserRoleDO updateRecord = SystemUserRoleConvert.MAPPER.to(request);
// 查询数据是否冲突
this.checkSystemUserRolePresent(updateRecord);
// 更新
int effect = systemUserRoleDAO.updateById(updateRecord);
log.info("SystemUserRoleService-updateSystemUserRole effect: {}, updateRecord: {}", effect, JSON.toJSONString(updateRecord));
public Integer deleteUserRoles(SystemUserUpdateRoleRequest request) {
Long userId = request.getId();
// 删除用户关联
Integer effect = systemUserRoleDAO.deleteByUserId(userId);
// 更新缓存中的角色
RedisUtils.processSetJson(UserCacheKeyDefine.USER_INFO, LoginUser.class, s -> {
s.setRoles(null);
}, userId);
return effect;
}
@Override
public SystemUserRoleVO getSystemUserRole(Long id) {
// 查询
SystemUserRoleDO record = systemUserRoleDAO.selectById(id);
Valid.notNull(record, ErrorMessage.DATA_ABSENT);
// 转换
return SystemUserRoleConvert.MAPPER.to(record);
}
@Override
public List<SystemUserRoleVO> getSystemUserRoleList(List<Long> idList) {
// 查询
List<SystemUserRoleDO> records = systemUserRoleDAO.selectBatchIds(idList);
if (records.isEmpty()) {
return Lists.empty();
@Transactional(rollbackFor = Exception.class)
public Integer updateUserRoles(SystemUserUpdateRoleRequest request) {
Long userId = request.getId();
Set<String> roleCodeList = request.getRoles();
// 查询用户
SystemUserDO record = systemUserDAO.selectById(userId);
Valid.notNull(record, ErrorMessage.USER_ABSENT);
// 查询角色
List<SystemRoleDO> userRoles = systemRoleDAO.selectByCodeList(roleCodeList);
// 检查角色是否存在
if (userRoles.size() != roleCodeList.size()) {
// 有不存在的角色
List<String> userRoleCodes = userRoles.stream()
.map(SystemRoleDO::getCode)
.collect(Collectors.toList());
for (String roleCode : roleCodeList) {
// 角色不存在
if (!userRoleCodes.contains(roleCode)) {
throw ErrorCode.ROLE_PRESENT.exception(roleCode);
}
}
}
// 转换
return SystemUserRoleConvert.MAPPER.to(records);
}
@Override
public DataGrid<SystemUserRoleVO> getSystemUserRolePage(SystemUserRoleQueryRequest request) {
// 构造条件
LambdaQueryWrapper<SystemUserRoleDO> wrapper = systemUserRoleDAO.wrapper()
.eq(SystemUserRoleDO::getId, request.getId())
.eq(SystemUserRoleDO::getUserId, request.getUserId())
.eq(SystemUserRoleDO::getRoleId, request.getRoleId())
.orderByDesc(SystemUserRoleDO::getId);
// 查询
return systemUserRoleDAO.of()
.wrapper(wrapper)
.page(request)
.dataGrid(SystemUserRoleConvert.MAPPER::to);
}
@Override
public Integer deleteSystemUserRole(Long id) {
int effect = systemUserRoleDAO.deleteById(id);
log.info("SystemUserRoleService-deleteSystemUserRole id: {}, effect: {}", id, effect);
// 删除用户角色关联
Integer effect = systemUserRoleDAO.deleteByUserId(userId);
// 重新添加用户角色关联
List<SystemUserRoleDO> addUserRoles = userRoles.stream().map(s -> {
SystemUserRoleDO ur = new SystemUserRoleDO();
ur.setUserId(userId);
ur.setRoleId(s.getId());
return ur;
}).collect(Collectors.toList());
systemUserRoleDAO.insertBatch(addUserRoles);
// 更新缓存中的角色
RedisUtils.processSetJson(UserCacheKeyDefine.USER_INFO, LoginUser.class, s -> {
s.setRoles(new ArrayList<>(roleCodeList));
}, userId);
return effect;
}
@Async("asyncExecutor")
@Override
public Integer batchDeleteSystemUserRole(List<Long> idList) {
int effect = systemUserRoleDAO.deleteBatchIds(idList);
log.info("SystemUserRoleService-batchDeleteSystemUserRole idList: {}, effect: {}", JSON.toJSONString(idList), effect);
return effect;
}
/**
* 检测对象是否存在
*
* @param domain domain
*/
private void checkSystemUserRolePresent(SystemUserRoleDO domain) {
// 构造条件
LambdaQueryWrapper<SystemUserRoleDO> wrapper = systemUserRoleDAO.wrapper()
// 更新时忽略当前记录
.ne(SystemUserRoleDO::getId, domain.getId())
// 用其他字段做重复校验
.eq(SystemUserRoleDO::getUserId, domain.getUserId())
.eq(SystemUserRoleDO::getRoleId, domain.getRoleId());
// 检查是否存在
boolean present = systemUserRoleDAO.of().wrapper(wrapper).present();
Valid.isFalse(present, ErrorMessage.DATA_PRESENT);
public void asyncDeleteUserCacheRole(String roleCode, List<Long> userIdList) {
for (Long userId : userIdList) {
RedisUtils.processSetJson(UserCacheKeyDefine.USER_INFO, LoginUser.class, s -> {
List<String> roles = s.getRoles();
if (Lists.isEmpty(roles)) {
return;
}
// 移除角色
roles.remove(roleCode);
}, userId);
}
}
}

View File

@@ -5,20 +5,30 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.orion.lang.define.wrapper.DataGrid;
import com.orion.lang.utils.Valid;
import com.orion.lang.utils.collect.Lists;
import com.orion.lang.utils.crypto.Signatures;
import com.orion.ops.framework.common.constant.ErrorCode;
import com.orion.ops.framework.common.constant.ErrorMessage;
import com.orion.ops.framework.common.security.LoginUser;
import com.orion.ops.framework.redis.core.utils.RedisUtils;
import com.orion.ops.framework.security.core.utils.SecurityUtils;
import com.orion.ops.module.infra.convert.SystemUserConvert;
import com.orion.ops.module.infra.dao.SystemUserDAO;
import com.orion.ops.module.infra.dao.SystemUserRoleDAO;
import com.orion.ops.module.infra.define.UserCacheKeyDefine;
import com.orion.ops.module.infra.entity.domain.SystemUserDO;
import com.orion.ops.module.infra.entity.request.user.SystemUserCreateRequest;
import com.orion.ops.module.infra.entity.request.user.SystemUserQueryRequest;
import com.orion.ops.module.infra.entity.request.user.SystemUserUpdateRequest;
import com.orion.ops.module.infra.entity.request.user.*;
import com.orion.ops.module.infra.entity.vo.SystemUserVO;
import com.orion.ops.module.infra.enums.UserStatusEnum;
import com.orion.ops.module.infra.service.AuthenticationService;
import com.orion.ops.module.infra.service.SystemUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
/**
* 用户 服务实现类
@@ -34,13 +44,21 @@ public class SystemUserServiceImpl implements SystemUserService {
@Resource
private SystemUserDAO systemUserDAO;
@Resource
private SystemUserRoleDAO systemUserRoleDAO;
@Resource
private RedisTemplate<String, String> redisTemplate;
@Override
public Long createSystemUser(SystemUserCreateRequest request) {
// 转换
SystemUserDO record = SystemUserConvert.MAPPER.to(request);
record.setId(null);
// 查询数据是否冲突
this.checkSystemUserPresent(record);
// 查询用户名称是否存在
this.checkUsernamePresent(record);
// 查询花名是否存在
this.checkNicknamePresent(record);
// 插入
int effect = systemUserDAO.insert(record);
log.info("SystemUserService-createSystemUser effect: {}, record: {}", effect, JSON.toJSONString(record));
@@ -52,14 +70,49 @@ public class SystemUserServiceImpl implements SystemUserService {
// 查询
Long id = Valid.notNull(request.getId(), ErrorMessage.ID_MISSING);
SystemUserDO record = systemUserDAO.selectById(id);
Valid.notNull(record, ErrorMessage.DATA_ABSENT);
Valid.notNull(record, ErrorMessage.USER_ABSENT);
// 转换
SystemUserDO updateRecord = SystemUserConvert.MAPPER.to(request);
// 查询数据是否冲突
this.checkSystemUserPresent(updateRecord);
// 查询花名是否存在
this.checkNicknamePresent(updateRecord);
// 更新
int effect = systemUserDAO.updateById(updateRecord);
log.info("SystemUserService-updateSystemUser effect: {}, updateRecord: {}", effect, JSON.toJSONString(updateRecord));
// 更新缓存中的花名
RedisUtils.processSetJson(UserCacheKeyDefine.USER_INFO, LoginUser.class, s -> {
s.setNickname(request.getNickname());
}, id);
return effect;
}
@Override
public Integer updateUserStatus(SystemUserUpdateStatusRequest request) {
Long id = request.getId();
if (id.equals(SecurityUtils.getLoginUserId())) {
throw ErrorCode.UNSUPPOETED.exception();
}
// 检查状态
UserStatusEnum status = UserStatusEnum.of(request.getStatus());
Valid.notNull(status, ErrorMessage.INVALID_PARAM);
if (!status.equals(UserStatusEnum.DISABLED) && !status.equals(UserStatusEnum.ENABLED)) {
throw ErrorCode.BAD_REQUEST.exception();
}
// 查询用户
SystemUserDO record = systemUserDAO.selectById(id);
Valid.notNull(record, ErrorMessage.USER_ABSENT);
// 转换
SystemUserDO updateRecord = SystemUserConvert.MAPPER.to(request);
// 更新用户
int effect = systemUserDAO.updateById(updateRecord);
log.info("SystemUserService-updateUserStatus effect: {}, updateRecord: {}", effect, JSON.toJSONString(updateRecord));
// 如果之前是锁定则删除登陆失败次数缓存
if (UserStatusEnum.LOCKED.getStatus().equals(record.getStatus())) {
redisTemplate.delete(UserCacheKeyDefine.LOGIN_FAILED_COUNT.format(record.getUsername()));
}
// 更新缓存中的status
RedisUtils.processSetJson(UserCacheKeyDefine.USER_INFO, LoginUser.class, s -> {
s.setStatus(request.getStatus());
}, id);
return effect;
}
@@ -67,7 +120,7 @@ public class SystemUserServiceImpl implements SystemUserService {
public SystemUserVO getSystemUser(Long id) {
// 查询
SystemUserDO record = systemUserDAO.selectById(id);
Valid.notNull(record, ErrorMessage.DATA_ABSENT);
Valid.notNull(record, ErrorMessage.USER_ABSENT);
// 转换
return SystemUserConvert.MAPPER.to(record);
}
@@ -88,14 +141,11 @@ public class SystemUserServiceImpl implements SystemUserService {
// 构造条件
LambdaQueryWrapper<SystemUserDO> wrapper = systemUserDAO.wrapper()
.eq(SystemUserDO::getId, request.getId())
.eq(SystemUserDO::getUsername, request.getUsername())
.eq(SystemUserDO::getPassword, request.getPassword())
.eq(SystemUserDO::getNickname, request.getNickname())
.eq(SystemUserDO::getAvatar, request.getAvatar())
.eq(SystemUserDO::getMobile, request.getMobile())
.eq(SystemUserDO::getEmail, request.getEmail())
.like(SystemUserDO::getUsername, request.getUsername())
.like(SystemUserDO::getNickname, request.getNickname())
.like(SystemUserDO::getMobile, request.getMobile())
.like(SystemUserDO::getEmail, request.getEmail())
.eq(SystemUserDO::getStatus, request.getStatus())
.eq(SystemUserDO::getLastLoginTime, request.getLastLoginTime())
.orderByDesc(SystemUserDO::getId);
// 查询
return systemUserDAO.of()
@@ -105,41 +155,80 @@ public class SystemUserServiceImpl implements SystemUserService {
}
@Override
@Transactional(rollbackFor = Exception.class)
public Integer deleteSystemUser(Long id) {
if (id.equals(SecurityUtils.getLoginUserId())) {
throw ErrorCode.UNSUPPOETED.exception();
}
int effect = systemUserDAO.deleteById(id);
log.info("SystemUserService-deleteSystemUser id: {}, effect: {}", id, effect);
// 删除角色关联
effect += systemUserRoleDAO.deleteByUserId(id);
// 删除用户缓存 其他的会自动过期
redisTemplate.delete(UserCacheKeyDefine.USER_INFO.format(id));
return effect;
}
@Override
public Integer batchDeleteSystemUser(List<Long> idList) {
int effect = systemUserDAO.deleteBatchIds(idList);
log.info("SystemUserService-batchDeleteSystemUser idList: {}, effect: {}", JSON.toJSONString(idList), effect);
return effect;
public void resetPassword(UserResetPasswordRequest request) {
Long id = Valid.notNull(request.getId(), ErrorMessage.ID_MISSING);
SystemUserDO record = Valid.notNull(systemUserDAO.selectById(id), ErrorMessage.USER_ABSENT);
// 修改密码
SystemUserDO update = new SystemUserDO();
update.setId(id);
update.setPassword(Signatures.md5(request.getPassword()));
int effect = systemUserDAO.updateById(update);
log.info("SystemUserService-resetPassword record: {}, effect: {}", JSON.toJSONString(update), effect);
// 删除登陆失败次数缓存
redisTemplate.delete(UserCacheKeyDefine.LOGIN_FAILED_COUNT.format(record.getUsername()));
// 删除登陆缓存
String loginKey = UserCacheKeyDefine.LOGIN_TOKEN.format(id, "*");
Set<String> loginKeyList = RedisUtils.scanKeys(loginKey, 100);
if (!loginKeyList.isEmpty()) {
redisTemplate.delete(loginKeyList);
}
// 删除续签信息
if (AuthenticationService.allowRefresh) {
String refreshKey = UserCacheKeyDefine.LOGIN_REFRESH.format(id, "*");
Set<String> refreshKeyList = RedisUtils.scanKeys(refreshKey, 100);
if (!refreshKeyList.isEmpty()) {
redisTemplate.delete(refreshKeyList);
}
}
}
/**
* 检测对象是否存在
* 检测用户名否存在
*
* @param domain domain
*/
private void checkSystemUserPresent(SystemUserDO domain) {
private void checkUsernamePresent(SystemUserDO domain) {
// 构造条件
LambdaQueryWrapper<SystemUserDO> wrapper = systemUserDAO.wrapper()
// 更新时忽略当前记录
.ne(SystemUserDO::getId, domain.getId())
// 用其他字段做重复校验
.eq(SystemUserDO::getUsername, domain.getUsername())
.eq(SystemUserDO::getPassword, domain.getPassword())
.eq(SystemUserDO::getNickname, domain.getNickname())
.eq(SystemUserDO::getAvatar, domain.getAvatar())
.eq(SystemUserDO::getMobile, domain.getMobile())
.eq(SystemUserDO::getEmail, domain.getEmail())
.eq(SystemUserDO::getStatus, domain.getStatus())
.eq(SystemUserDO::getLastLoginTime, domain.getLastLoginTime());
.eq(SystemUserDO::getUsername, domain.getUsername());
// 检查是否存在
boolean present = systemUserDAO.of().wrapper(wrapper).present();
Valid.isFalse(present, ErrorMessage.DATA_PRESENT);
Valid.isFalse(present, ErrorMessage.USERNAME_PRESENT);
}
/**
* 检测花名是否存在
*
* @param domain domain
*/
private void checkNicknamePresent(SystemUserDO domain) {
// 构造条件
LambdaQueryWrapper<SystemUserDO> wrapper = systemUserDAO.wrapper()
// 更新时忽略当前记录
.ne(SystemUserDO::getId, domain.getId())
// 用其他字段做重复校验
.eq(SystemUserDO::getNickname, domain.getNickname());
// 检查是否存在
boolean present = systemUserDAO.of().wrapper(wrapper).present();
Valid.isFalse(present, ErrorMessage.NICKNAME_PRESENT);
}
}