refactor(org): 迁移账户模块至 company/account,重构公司拥有者创建账户逻辑并统一拥有者文案与注释

This commit is contained in:
小蟑螂
2026-05-25 18:13:53 +08:00
parent 2f44eaa9d2
commit d4bfa4d257
21 changed files with 156 additions and 53 deletions
@@ -29,7 +29,7 @@ public @interface RequireRole {
boolean allowSuper() default true;
/**
* 是否允许公司管理员访问(默认允许)
* 是否允许公司拥有者访问(默认允许)
*/
boolean allowCompany() default true;
@@ -56,12 +56,12 @@ public class RoleAspect {
hasPermission = annotation.allowSuper();
break;
case "user":
// 普通用户检查公司成员角色,如果是 owner 则视为公司管理员
// 普通用户检查公司成员角色,如果是 owner 则视为公司拥有者
if (annotation.allowCompany()) {
String companyRole = currentUserContext.getCurrentCompanyRole();
if ("owner".equals(companyRole)) {
hasPermission = true;
log.debug("角色权限验证 - 用户是公司拥有者,视为公司管理员");
log.debug("角色权限验证 - 用户是公司拥有者");
} else {
hasPermission = annotation.allowUser();
}
@@ -16,9 +16,11 @@ import top.xyquan.ecomdmbbackend.modules.org.account.model.vo.AccountVO;
import top.xyquan.ecomdmbbackend.modules.org.account.service.AccountService;
/**
* 账户管理控制器
* 公司账户管理控制器
* <p>
* 供公司拥有者管理本公司成员账户;需 org:company:manage 权限,超管可访问全部接口。
*/
@Tag(name = "组织-账户管理", description = "账户管理相关接口")
@Tag(name = "组织-账户管理", description = "公司拥有者管理本公司账户")
@RestController
@RequestMapping("/company/account")
public class AccountController {
@@ -29,7 +31,8 @@ public class AccountController {
this.accountService = accountService;
}
@Operation(summary = "创建账户(管理员)", description = "公司管理员创建本公司账户")
/** 创建账户:手机号已注册则拉入公司,未注册则新建用户(密码必填) */
@Operation(summary = "创建账户(拥有者)", description = "公司拥有者创建本公司账户;手机号已存在时将用户拉入本公司,不存在则新建用户")
@PostMapping("/create")
@RequirePermission(value = "org:company:manage", description = "创建账户", allowSuper = true)
public ApiResponse<AccountVO> createAccountForCompany(@Valid @RequestBody CreateAccountDTO createAccountDTO) {
@@ -37,7 +40,8 @@ public class AccountController {
return ApiResponse.success("账户创建成功", accountVO);
}
@Operation(summary = "分页查询账户(管理员)", description = "公司管理员分页查询本公司账户")
/** 分页查询当前公司成员对应的账户列表 */
@Operation(summary = "分页查询账户(拥有者)", description = "公司拥有者分页查询本公司账户")
@PostMapping("/list")
@RequirePermission(value = "org:company:manage", description = "查询账户列表", allowSuper = true)
public ApiResponse<PageResultVO<AccountVO>> getAccountPageForCompany(@Valid @RequestBody PageQueryDTO<AccountFilterDTO> pageQuery) {
@@ -45,7 +49,8 @@ public class AccountController {
return ApiResponse.success("获取账户列表成功", pageResult);
}
@Operation(summary = "更新账户信息", description = "公司管理员更新本公司账户信息")
/** 更新账户基本信息及职位、部门关联 */
@Operation(summary = "更新账户信息", description = "公司拥有者更新本公司账户信息")
@PutMapping("/update/{accountId}")
@RequirePermission(value = "org:company:manage", description = "更新账户", allowSuper = true)
public ApiResponse<AccountVO> updateAccount(
@@ -55,6 +60,7 @@ public class AccountController {
return ApiResponse.success("账户更新成功", accountVO);
}
/** 逻辑删除:停用 sys_user 并移除公司/部门成员关系 */
@Operation(summary = "删除账户", description = "逻辑删除账户")
@DeleteMapping("/delete/{accountId}")
@RequirePermission(value = "org:company:manage", description = "删除账户", allowSuper = true)
@@ -2,21 +2,28 @@ package top.xyquan.ecomdmbbackend.modules.org.account.model.dto;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Pattern;
import jakarta.validation.constraints.Size;
import lombok.Data;
/**
* 创建账户DTO
* 创建公司账户请求 DTO
* <p>
* 由公司拥有者调用;手机号决定走「拉入已有用户」还是「新建用户」分支。
*/
@Data
@Schema(description = "创建账户请求")
public class CreateAccountDTO {
@NotBlank(message = "密码不能为空")
/**
* 登录密码
* 仅手机号未注册时必填;已注册用户加入公司时可留空
*/
@Size(min = 6, max = 20, message = "密码长度为6-20个字符")
@Schema(description = "登录密码", example = "123456")
@Schema(description = "登录密码(手机号未注册时必填)", example = "123456")
private String password;
/** 手机号,同时作为新用户的登录账号(login_code) */
@NotBlank(message = "手机号不能为空")
@Schema(description = "手机号码", example = "13800138000")
private String phone;
@@ -25,6 +32,14 @@ public class CreateAccountDTO {
@Schema(description = "用户姓名", example = "张三")
private String realName;
/**
* 加入公司后的成员角色,默认 member
* owner=公司拥有者,member=普通成员
*/
@Schema(description = "公司成员角色: owner=公司拥有者, member=成员", example = "member")
@Pattern(regexp = "^(owner|member)$", message = "公司成员角色必须是 owner 或 member")
private String companyRole = "member";
@Schema(description = "部门ID", example = "1")
private Long departmentId;
@@ -87,9 +87,9 @@ public class AccountVO {
@Schema(description = "更新时间", example = "2023-12-01T10:00:00")
private LocalDateTime updateTime;
@Schema(description = "创建者", example = "管理员")
@Schema(description = "创建者", example = "拥有者")
private String createByName;
@Schema(description = "更新者", example = "管理员")
@Schema(description = "更新者", example = "拥有者")
private String updateByName;
}
@@ -15,7 +15,10 @@ import top.xyquan.ecomdmbbackend.modules.org.account.model.vo.AccountVO;
public interface AccountService extends IService<UserPO> {
/**
* 创建账户(管理员接口,自动设置公司ID)
* 创建公司账户(拥有者接口)
* <p>
* 公司 ID 从当前登录上下文获取;手机号已存在则拉入公司,不存在则新建用户。
* 仅公司拥有者可调用,超管不受限。
*
* @param createAccountDTO 创建账户信息
* @return 账户信息
@@ -23,7 +26,7 @@ public interface AccountService extends IService<UserPO> {
AccountVO createAccountForCompany(CreateAccountDTO createAccountDTO);
/**
* 更新账户信息(管理员接口)
* 更新账户信息(拥有者接口)
*
* @param updateAccountDTO 更新账户信息
* @param accountId 账户ID
@@ -55,7 +58,7 @@ public interface AccountService extends IService<UserPO> {
PageResultVO<AccountVO> getAccountPage(PageQueryDTO<AccountFilterDTO> pageQuery);
/**
* 分页查询账户(管理员接口,只能查询本公司账户)
* 分页查询账户(拥有者接口,只能查询本公司账户)
*
* @param pageQuery 分页查询参数
* @return 分页结果
@@ -35,7 +35,10 @@ import java.util.List;
import java.util.stream.Collectors;
/**
* 账户管理服务实现类
* 公司账户管理服务实现类
* <p>
* 面向公司拥有者(及超管)的本公司账户 CRUD。
* 创建账户时:手机号已注册则拉入当前公司,未注册则新建用户并加入公司。
*/
@Service
public class AccountServiceImpl extends ServiceImpl<UserMapper, UserPO> implements AccountService {
@@ -64,49 +67,125 @@ public class AccountServiceImpl extends ServiceImpl<UserMapper, UserPO> implemen
/**
* 创建公司账户
* <p>
* 业务规则:
* <ul>
* <li>仅公司拥有者可操作,超管不受此限</li>
* <li>手机号已存在:不新建用户,将其加入当前公司(已是成员则报错)</li>
* <li>手机号不存在:新建 sys_user,密码必填,系统角色固定为 user</li>
* <li>companyRole 决定其在公司成员表中的角色(owner / member)</li>
* </ul>
*
* @param createAccountDTO 创建账户DTO
* @return 账户VO
*/
@Override
public AccountVO createAccountForCompany(CreateAccountDTO createAccountDTO) {
// 从用户上下文获取公司ID
Long companyId = currentUserContext.getCurrentCompanyId();
if (companyId == null) {
throw new BusinessException("无法获取公司信息");
}
// 验证手机号码是否已存在
if (StringUtils.hasText(createAccountDTO.getPhone())) {
LambdaQueryWrapper<UserPO> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(UserPO::getPhone, createAccountDTO.getPhone());
if (this.count(wrapper) > 0) {
throw new BusinessException("手机号码已存在");
}
CompanyMemberRole memberRole = CompanyMemberRole.fromCode(createAccountDTO.getCompanyRole());
assertOwnerCanCreateAccount(companyId);
// 按手机号分流:已注册走「拉入公司」,未注册走「新建用户」
UserPO existingUser = findUserByPhone(createAccountDTO.getPhone());
if (existingUser != null) {
return addExistingUserToCompany(existingUser, companyId, createAccountDTO, memberRole);
}
return createNewUserAndAddToCompany(companyId, createAccountDTO, memberRole);
}
/**
* 校验当前用户是否有权在本公司创建账户
* 超管直接放行;普通用户须为该公司拥有者(company_member.role = owner)
*/
private void assertOwnerCanCreateAccount(Long companyId) {
if ("super".equals(currentUserContext.getCurrentUserRole())) {
return;
}
if (!companyMemberService.isOwner(currentUserContext.getCurrentUserId(), companyId)) {
throw new BusinessException("仅公司拥有者可以创建账户");
}
}
/** 按手机号查找已注册用户,未传或查无此人时返回 null */
private UserPO findUserByPhone(String phone) {
if (!StringUtils.hasText(phone)) {
return null;
}
LambdaQueryWrapper<UserPO> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(UserPO::getPhone, phone);
return this.getOne(wrapper);
}
/**
* 将已存在用户加入当前公司
* 不修改密码;可选更新姓名、备注;同时建立公司成员及部门关联
*/
private AccountVO addExistingUserToCompany(
UserPO user,
Long companyId,
CreateAccountDTO createAccountDTO,
CompanyMemberRole memberRole) {
if (user.getStatus() == null || user.getStatus() != 1) {
throw new BusinessException("该手机号对应账户状态异常,无法加入公司");
}
// 创建账户
UserPO user = new UserPO();
BeanUtils.copyProperties(createAccountDTO, user);
user.setPassword(passwordEncoder.encode(createAccountDTO.getPassword()));
user.setLoginCode(createAccountDTO.getPhone()); // 使用手机号作为登录账号
user.setRole("user"); // 默认角色
user.setStatus(1); // 正常状态(与 sys_user 表定义一致:1=正常,0=停用)
user.setPwdSecurityLevel(calculatePasswordSecurityLevel(createAccountDTO.getPassword()));
user.setPwdUpdateTime(LocalDateTime.now());
user.setCreateBy(currentUserContext.getCurrentUserId());
CompanyMemberPO existingMember = companyMemberService.getByUserIdAndCompanyId(user.getId(), companyId);
if (existingMember != null) {
throw new BusinessException("该用户已是公司成员");
}
// 保存账户
this.save(user);
if (StringUtils.hasText(createAccountDTO.getRealName())) {
user.setRealName(createAccountDTO.getRealName());
}
if (StringUtils.hasText(createAccountDTO.getRemark())) {
user.setRemark(createAccountDTO.getRemark());
}
user.setUpdateBy(currentUserContext.getCurrentUserId());
this.updateById(user);
// 创建公司成员关系
companyMemberService.create(user.getId(), companyId, CompanyMemberRole.member, createAccountDTO.getPositionId());
companyMemberService.create(user.getId(), companyId, memberRole, createAccountDTO.getPositionId());
if (createAccountDTO.getDepartmentId() != null) {
departmentMemberService.addMember(createAccountDTO.getDepartmentId(), user.getId());
}
return convertToVO(user);
}
/**
* 新建用户并加入当前公司
* 登录账号与手机号相同,系统角色固定为 user,须传入密码
*/
private AccountVO createNewUserAndAddToCompany(
Long companyId,
CreateAccountDTO createAccountDTO,
CompanyMemberRole memberRole) {
if (!StringUtils.hasText(createAccountDTO.getPassword())) {
throw new BusinessException("新用户必须设置密码");
}
UserPO user = new UserPO();
BeanUtils.copyProperties(createAccountDTO, user);
user.setPassword(passwordEncoder.encode(createAccountDTO.getPassword()));
user.setLoginCode(createAccountDTO.getPhone());
user.setRole("user");
user.setStatus(1);
user.setPwdSecurityLevel(calculatePasswordSecurityLevel(createAccountDTO.getPassword()));
user.setPwdUpdateTime(LocalDateTime.now());
user.setCreateBy(currentUserContext.getCurrentUserId());
this.save(user);
companyMemberService.create(user.getId(), companyId, memberRole, createAccountDTO.getPositionId());
// 如果有部门ID,创建部门成员关系
if (createAccountDTO.getDepartmentId() != null) {
departmentMemberService.addMember(createAccountDTO.getDepartmentId(), user.getId());
}
// 转换为VO并返回
return convertToVO(user);
}
@@ -392,14 +471,14 @@ public class AccountServiceImpl extends ServiceImpl<UserMapper, UserPO> implemen
}
/**
* 获取角色名称
* @param role 角色
* @return 角色名称
* 获取角色中文展示名
* admin 为历史系统角色码,展示为「拥有者」
*/
private String getRoleName(String role) {
if (role == null) return "未知";
return switch (role) {
case "super" -> "超级管理员";
case "admin" -> "拥有者";
case "user" -> "普通用户";
default -> "未知";
};
@@ -440,6 +440,7 @@ public class SuperAccountServiceImpl extends ServiceImpl<UserMapper, UserPO> imp
if (role == null) return "未知";
return switch (role) {
case "super" -> "超级管理员";
case "admin" -> "拥有者";
case "user" -> "普通用户";
default -> "未知";
};
@@ -80,7 +80,7 @@ public class AuthController {
permissions = List.of("*");
break;
case "company":
// 公司管理员拥有公司相关权限
// 公司拥有者拥有公司相关权限
permissions = List.of("company:*");
break;
case "user":
@@ -57,7 +57,7 @@ public class UserPO {
private Long avatarImageId;
/**
* 关联角色(user-普通账户 admin-公司管理员账户 super-超管账户)
* 关联角色(user-普通账户 admin-公司拥有者账户 super-超管账户)
*/
@TableField("role")
private String role;
@@ -67,9 +67,9 @@ public class PermissionUtils implements ApplicationContextAware {
}
/**
* 检查当前用户是否是公司管理员
* 检查当前用户是否是公司拥有者
*
* @return 是否是公司管理员
* @return 是否是公司拥有者
*/
public static boolean isCompany() {
return hasRole("company");