use of com.hccake.ballcat.system.event.UserCreatedEvent in project ballcat by ballcat-projects.
the class SysUserServiceImpl method addSysUser.
/**
* 新增系统用户
* @param sysUserDto 系统用户DTO
* @return 添加成功:true , 失败:false
*/
@Override
@Transactional(rollbackFor = Exception.class)
public boolean addSysUser(SysUserDTO sysUserDto) {
SysUser sysUser = SysUserConverter.INSTANCE.dtoToPo(sysUserDto);
sysUser.setStatus(SysUserConst.Status.NORMAL.getValue());
sysUser.setType(SysUserConst.Type.SYSTEM.getValue());
// 对密码进行加密
String rawPassword = sysUserDto.getPassword();
String encodedPassword = PasswordUtils.encode(rawPassword);
sysUser.setPassword(encodedPassword);
// 保存用户
boolean insertSuccess = SqlHelper.retBool(baseMapper.insert(sysUser));
Assert.isTrue(insertSuccess, () -> {
log.error("[addSysUser] 数据插入系统用户表失败,user:{}", sysUserDto);
return new BusinessException(BaseResultCode.UPDATE_DATABASE_ERROR.getCode(), "数据插入系统用户表失败");
});
// 新增用户角色关联
List<String> roleCodes = sysUserDto.getRoleCodes();
if (CollectionUtil.isNotEmpty(roleCodes)) {
boolean addUserRoleSuccess = sysUserRoleService.addUserRoles(sysUser.getUserId(), roleCodes);
Assert.isTrue(addUserRoleSuccess, () -> {
log.error("[addSysUser] 更新用户角色信息失败,user:{}, roleCodes: {}", sysUserDto, roleCodes);
return new BusinessException(BaseResultCode.UPDATE_DATABASE_ERROR.getCode(), "更新用户角色信息失败");
});
}
// 发布用户创建事件
publisher.publishEvent(new UserCreatedEvent(sysUser, sysUserDto.getRoleCodes()));
return true;
}
Aggregations