@@ -120,9 +120,9 @@ public class UserController { | |||
* @param username | |||
* @return | |||
*/ | |||
@PostMapping("/init/{username}") | |||
public JsonResult init(@PathVariable("username") String username) { | |||
return userService.init(username); | |||
@PostMapping("/init/{username}/{password}") | |||
public JsonResult init(@PathVariable("username") String username, @PathVariable("password") String password) { | |||
return userService.init(username, password); | |||
} | |||
} |
@@ -33,8 +33,8 @@ public class AddOidcUserService { | |||
@Qualifier("restTemplate") | |||
private RestTemplate restTemplate; | |||
public JsonResult add(User user, Tenant tenant) { | |||
OidcCreateUserRequest request = this.buildOidcCreateUserRequest(user, tenant); | |||
public JsonResult add(User user, String password, Tenant tenant) { | |||
OidcCreateUserRequest request = this.buildOidcCreateUserRequest(user, password, tenant); | |||
//设置请求头 | |||
HttpHeaders resultRequestHeader = new HttpHeaders(); | |||
resultRequestHeader.add("Authorization", "Bearer " + SecurityUserUtils.token()); | |||
@@ -64,10 +64,10 @@ public class AddOidcUserService { | |||
* @param user | |||
* @return | |||
*/ | |||
private OidcCreateUserRequest buildOidcCreateUserRequest(User user, Tenant tenant) { | |||
private OidcCreateUserRequest buildOidcCreateUserRequest(User user, String password, Tenant tenant) { | |||
OidcCreateUserRequest oidcCreateUserRequest = new OidcCreateUserRequest(); | |||
oidcCreateUserRequest.setUsername(user.getUsername()); | |||
oidcCreateUserRequest.setPassword(SystemConstant.INIT_PASSWORD); | |||
oidcCreateUserRequest.setPassword(password); | |||
if (ObjectUtil.isNull(tenant)) { | |||
// 普通用户新增 | |||
oidcCreateUserRequest.setTenantFlag(0); |
@@ -6,7 +6,7 @@ import com.tuoheng.common.core.utils.JsonResult; | |||
public interface OidcService { | |||
JsonResult addUser(User user, Tenant tenant); | |||
JsonResult addUser(User user, String password, Tenant tenant); | |||
JsonResult editUser(User user); | |||
@@ -20,8 +20,8 @@ public class OidcServiceImpl implements OidcService { | |||
@Autowired | |||
private EditOidcUserPasswordService editOidcUserPasswordService; | |||
public JsonResult addUser(User user, Tenant tenant) { | |||
return addOidcUserService.add(user, tenant); | |||
public JsonResult addUser(User user, String password, Tenant tenant) { | |||
return addOidcUserService.add(user, password, tenant); | |||
} | |||
public JsonResult editUser(User user) { |
@@ -85,6 +85,6 @@ public interface IUserService { | |||
/** | |||
* 初始化系统超级管理员用户 | |||
*/ | |||
JsonResult init(String username); | |||
JsonResult init(String username, String password); | |||
} |
@@ -153,8 +153,8 @@ public class UserServiceImpl implements IUserService { | |||
* 初始化系统超级管理员用户 | |||
*/ | |||
@Override | |||
public JsonResult init(String username) { | |||
return systemUserInitService.init(username); | |||
public JsonResult init(String username, String password) { | |||
return systemUserInitService.init(username, password); | |||
} | |||
} |
@@ -61,7 +61,7 @@ public class AddUserService { | |||
Tenant tenant = tenantMapper.selectById(tenantId); | |||
// 添加oidc | |||
result = oidcService.addUser(user, tenant); | |||
result = oidcService.addUser(user, SystemConstant.INIT_PASSWORD, tenant); | |||
if (0 != result.getCode()) { | |||
log.info("添加用户业务接口:添加oidc数据失败:{}", result.getMsg()); | |||
return result; |
@@ -2,7 +2,6 @@ package com.tuoheng.admin.service.user.init; | |||
import cn.hutool.core.util.ObjectUtil; | |||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; | |||
import com.tuoheng.admin.constant.SystemConstant; | |||
import com.tuoheng.admin.entity.Role; | |||
import com.tuoheng.admin.entity.Tenant; | |||
import com.tuoheng.admin.entity.User; | |||
@@ -46,46 +45,39 @@ public class SystemUserInitService { | |||
* | |||
* @return | |||
*/ | |||
public JsonResult init(String username) { | |||
log.info("进入添加用户业务接口"); | |||
public JsonResult init(String username, String password) { | |||
log.info("进入初始化系统用户业务接口"); | |||
Tenant tenant = tenantMapper.selectOne(new LambdaQueryWrapper<Tenant>() | |||
.eq(Tenant::getMark, 1)); | |||
String tenantId = ""; | |||
if (ObjectUtil.isNotNull(tenant)) { | |||
tenantId = tenant.getId(); | |||
} | |||
User user = this.buildUser(username, tenantId); | |||
JsonResult result = this.check(user, tenantId); | |||
User user = this.buildUser(username, password, tenantId); | |||
JsonResult result = this.check(username, password, tenantId); | |||
if (0 != result.getCode()) { | |||
log.info("初始化系统用户业务接口:校验失败:{}", result.getMsg()); | |||
return result; | |||
} | |||
// 添加oidc | |||
result = oidcService.addUser(user, tenant); | |||
result = oidcService.addUser(user, password, tenant); | |||
if (0 != result.getCode()) { | |||
log.info("初始化系统用户业务接口:添加oidc数据失败:{}", result.getMsg()); | |||
return result; | |||
} | |||
Integer rowCount = userMapper.insert(user); | |||
if (rowCount <= 0) { | |||
return JsonResult.error(400, "初始化系统用户失败"); | |||
} | |||
return JsonResult.success(); | |||
} | |||
/** | |||
* | |||
* 构建实体字段数据 | |||
* | |||
*/ | |||
private User buildUser(String username, String tenantId) { | |||
private User buildUser(String username, String password, String tenantId) { | |||
if (StringUtils.isEmpty(username)) { | |||
username = "gs"; | |||
username = "THJS"; | |||
} | |||
User user = new User(); | |||
user.setCode("gs"); | |||
@@ -93,7 +85,7 @@ public class SystemUserInitService { | |||
user.setRealname("超级管理员"); | |||
user.setNickname("超级管理员"); | |||
user.setCityName(""); | |||
user.setPassword(CommonUtils.password(SystemConstant.INIT_PASSWORD)); | |||
user.setPassword(CommonUtils.password(password)); | |||
user.setAvatar("imagedir/n1o1j9ndau_1670488066287.png"); | |||
user.setClientId("1,2"); | |||
user.setRoleId(1); | |||
@@ -107,21 +99,27 @@ public class SystemUserInitService { | |||
/** | |||
* 检查参数 | |||
* | |||
* @param user | |||
* @param username | |||
* @param password | |||
* @return | |||
*/ | |||
private JsonResult check(User user, String tenantId) { | |||
private JsonResult check(String username, String password, String tenantId) { | |||
if (StringUtils.isEmpty(username)) { | |||
return JsonResult.error(400, "用户名为空"); | |||
} | |||
if (StringUtils.isEmpty(password)) { | |||
return JsonResult.error(400, "密码为空"); | |||
} | |||
Integer count = userMapper.selectCount(new LambdaQueryWrapper<User>() | |||
.eq(User::getTenantId, tenantId) | |||
.eq(User::getRoleId, 1) | |||
.eq(User::getMark, 1)); | |||
.eq(User::getTenantId, tenantId) | |||
.eq(User::getRoleId, 1) | |||
.eq(User::getMark, 1)); | |||
if (count > 0) { | |||
return JsonResult.error(400, "系统中已经存在超级管理员"); | |||
} | |||
count = userMapper.selectCount(new LambdaQueryWrapper<User>() | |||
.eq(User::getTenantId, tenantId) | |||
.eq(User::getUsername, user.getUsername()) | |||
.eq(User::getUsername, username) | |||
.eq(User::getMark, 1)); | |||
if (count > 0) { | |||
return JsonResult.error(400, "系统中已经存在该用户名"); |