功能(auth):实现用户注册并分配默认角色
- 添加AppConfig类以启用事务管理 - 在application.yaml中配置默认角色代码 - 在AuthServiceImpl中注入RoleMapper和defRoleCode - 在注册过程中为新用户分配默认角色 - 添加用户注册和登录事件的日志记录 - 在登录过程中处理异常并进行适当的错误日志记录 - 创建用户、角色、权限及关联关系的数据库表 - 添加用户角色实体类和用户-角色关系映射器 - 实现用户角色服务以管理用户角色分配 - 添加通过SetUserRoleRequest设置用户角色的API端点 - 更新用户实体类并添加适当的字段和注解 - 实现包含角色信息的用户信息查询方法 - 将用户转换逻辑重构为可复用方法 - 为SetUserRoleRequest DTO添加校验约束}```
This commit is contained in:
@@ -46,18 +46,7 @@ public class UserInfoServiceImpl implements UserInfoService {
|
||||
}
|
||||
|
||||
return allUserWithRoles.stream().map(user -> {
|
||||
UserinfoResponse userinfoResponse = new UserinfoResponse();
|
||||
userinfoResponse.setId(user.getId());
|
||||
userinfoResponse.setUsername(user.getUsername());
|
||||
userinfoResponse.setRoles(
|
||||
user.getRoles().stream().map(role -> {
|
||||
RoleResponse roleResponse = new RoleResponse();
|
||||
roleResponse.setId(role.getId());
|
||||
roleResponse.setName(role.getName());
|
||||
return roleResponse;
|
||||
}).toList()
|
||||
);
|
||||
return userinfoResponse;
|
||||
return userConverterUserinfo(user);
|
||||
}).toList();
|
||||
}
|
||||
|
||||
@@ -89,5 +78,42 @@ public class UserInfoServiceImpl implements UserInfoService {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public UserinfoResponse findUserWithRoles(String username) {
|
||||
if (username == null || username.isEmpty()){
|
||||
throw new BizException(ErrorCode.USER_NOT_EXISTS);
|
||||
}
|
||||
log.info("查询用户名: {}", username);
|
||||
|
||||
User user = userMapper.findUserWithRolesByUsername(username);
|
||||
|
||||
log.info("查询用户名: {} 结果: {}", username, user);
|
||||
if (user == null) {
|
||||
throw new BizException(ErrorCode.USER_NOT_EXISTS);
|
||||
}
|
||||
return userConverterUserinfo(user);
|
||||
}
|
||||
|
||||
/**
|
||||
* User 转 UserinfoResponse
|
||||
* @param user
|
||||
* @return
|
||||
*/
|
||||
private UserinfoResponse userConverterUserinfo(User user) {
|
||||
UserinfoResponse userinfoResponse = new UserinfoResponse();
|
||||
userinfoResponse.setId(user.getId());
|
||||
userinfoResponse.setUsername(user.getUsername());
|
||||
userinfoResponse.setRoles(
|
||||
user.getRoles().stream().map(role -> {
|
||||
RoleResponse roleResponse = new RoleResponse();
|
||||
roleResponse.setId(role.getId());
|
||||
roleResponse.setName(role.getName());
|
||||
return roleResponse;
|
||||
}).toList()
|
||||
);
|
||||
return userinfoResponse;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
package cn.nopj.chaos_api.service.impl;
|
||||
|
||||
import cn.nopj.chaos_api.common.constants.ErrorCode;
|
||||
import cn.nopj.chaos_api.common.exceotion.BizException;
|
||||
import cn.nopj.chaos_api.domain.entity.UserRole;
|
||||
import cn.nopj.chaos_api.dto.request.SetUserRoleRequest;
|
||||
import cn.nopj.chaos_api.mapper.UserRoleMapper;
|
||||
import cn.nopj.chaos_api.service.UserRoleService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
public class UserRoleServiceImpl implements UserRoleService {
|
||||
|
||||
@Autowired
|
||||
private UserRoleMapper userRoleMapper;
|
||||
|
||||
@Override
|
||||
|
||||
public void setUserRole(Long userId, List<Long> roles_id) {
|
||||
|
||||
for (Long role_id : roles_id) {
|
||||
if (role_id == null || role_id == 0L) {
|
||||
throw new BizException(ErrorCode.ROLE_ID_INVALID);
|
||||
}else {
|
||||
userRoleMapper.insert(new UserRole(userId, role_id));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
@Override
|
||||
public void setUserRole(SetUserRoleRequest request) {
|
||||
if (request == null || request.getUserId() == null ){
|
||||
throw new BizException(ErrorCode.USER_ID_INVALID);
|
||||
}
|
||||
if (request.getRolesId() == null || request.getRolesId().isEmpty()){
|
||||
throw new BizException(ErrorCode.ROLE_ID_INVALID);
|
||||
}
|
||||
|
||||
Long userId = request.getUserId();
|
||||
List<Long> roles_id = request.getRolesId();
|
||||
setUserRole(userId, roles_id);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user