优化:社交登录、用户的管理

This commit is contained in:
YunaiV 2023-11-04 14:37:36 +08:00
parent e9317bf293
commit 87bc3f510c
17 changed files with 77 additions and 148 deletions

View File

@ -116,11 +116,11 @@ public interface ErrorCodeConstants {
// ========== 社交用户 1-002-018-000 ==========
ErrorCode SOCIAL_USER_AUTH_FAILURE = new ErrorCode(1_002_018_000, "社交授权失败,原因是:{}");
ErrorCode SOCIAL_USER_UNBIND_NOT_SELF = new ErrorCode(1_002_018_001, "社交解绑失败,非当前用户绑定");
ErrorCode SOCIAL_USER_NOT_FOUND = new ErrorCode(1_002_018_002, "社交授权失败,找不到对应的用户");
ErrorCode SOCIAL_APP_WEIXIN_MINI_APP_PHONE_CODE_ERROR = new ErrorCode(1_002_018_103, "获得手机号失败");
ErrorCode SOCIAL_CLIENT_NOT_EXISTS = new ErrorCode(1_002_018_104, "社交客户端不存在");
ErrorCode SOCIAL_USER_NOT_EXISTS = new ErrorCode(1_002_018_105, "社交用户不存在");
ErrorCode SOCIAL_USER_NOT_FOUND = new ErrorCode(1_002_018_001, "社交授权失败,找不到对应的用户");
ErrorCode SOCIAL_CLIENT_WEIXIN_MINI_APP_PHONE_CODE_ERROR = new ErrorCode(1_002_018_200, "获得手机号失败");
ErrorCode SOCIAL_CLIENT_NOT_EXISTS = new ErrorCode(1_002_018_201, "社交客户端不存在");
ErrorCode SOCIAL_CLIENT_UNIQUE = new ErrorCode(1_002_018_201, "社交客户端已存在配置");
// ========== 系统敏感词 1-002-019-000 =========
ErrorCode SENSITIVE_WORD_NOT_EXISTS = new ErrorCode(1_002_019_000, "系统敏感词在所有标签中都不存在");

View File

@ -7,7 +7,6 @@ import cn.iocoder.yudao.module.system.controller.admin.socail.vo.SocialUserBindR
import cn.iocoder.yudao.module.system.controller.admin.socail.vo.SocialUserUnbindReqVO;
import cn.iocoder.yudao.module.system.controller.admin.socail.vo.user.SocialUserPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.socail.vo.user.SocialUserRespVO;
import cn.iocoder.yudao.module.system.controller.admin.socail.vo.user.SocialUserUpdateReqVO;
import cn.iocoder.yudao.module.system.convert.social.SocialUserConvert;
import cn.iocoder.yudao.module.system.dal.dataobject.social.SocialUserDO;
import cn.iocoder.yudao.module.system.service.social.SocialUserService;
@ -49,23 +48,6 @@ public class SocialUserController {
// ==================== 社交用户 CRUD ====================
@PutMapping("/update")
@Operation(summary = "更新社交用户")
@PreAuthorize("@ss.hasPermission('system:social-user:update')")
public CommonResult<Boolean> updateSocialUser(@Valid @RequestBody SocialUserUpdateReqVO updateReqVO) {
socialUserService.updateSocialUser(updateReqVO);
return success(true);
}
@DeleteMapping("/delete")
@Operation(summary = "删除社交用户")
@Parameter(name = "id", description = "编号", required = true)
@PreAuthorize("@ss.hasPermission('system:social-user:delete')")
public CommonResult<Boolean> deleteSocialUser(@RequestParam("id") Long id) {
socialUserService.deleteSocialUser(id);
return success(true);
}
@GetMapping("/get")
@Operation(summary = "获得社交用户")
@Parameter(name = "id", description = "编号", required = true, example = "1024")

View File

@ -1,9 +1,17 @@
package cn.iocoder.yudao.module.system.controller.admin.socail.vo.client;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
import cn.iocoder.yudao.framework.common.validation.InEnum;
import cn.iocoder.yudao.module.system.enums.social.SocialTypeEnum;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import javax.validation.constraints.AssertTrue;
import javax.validation.constraints.NotNull;
import java.util.Objects;
/**
* 社交客户端 Base VO提供给添加修改详细的子 VO 使用
@ -18,10 +26,12 @@ public class SocialClientBaseVO {
@Schema(description = "社交平台的类型", requiredMode = Schema.RequiredMode.REQUIRED, example = "31")
@NotNull(message = "社交平台的类型不能为空")
@InEnum(SocialTypeEnum.class)
private Integer socialType;
@Schema(description = "用户类型", requiredMode = Schema.RequiredMode.REQUIRED, example = "2")
@NotNull(message = "用户类型不能为空")
@InEnum(UserTypeEnum.class)
private Integer userType;
@Schema(description = "客户端编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "wwd411c69a39ad2e54")
@ -37,6 +47,19 @@ public class SocialClientBaseVO {
@Schema(description = "状态", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
@NotNull(message = "状态不能为空")
@InEnum(CommonStatusEnum.class)
private Integer status;
@SuppressWarnings("RedundantIfStatement")
@AssertTrue(message = "agentId 不能为空")
@JsonIgnore
public boolean isAgentIdValid() {
// 如果是企业微信必须填写 agentId 属性
if (Objects.equals(socialType, SocialTypeEnum.WECHAT_ENTERPRISE.getType())
&& StrUtil.isEmpty(agentId)) {
return false;
}
return true;
}
}

View File

@ -5,11 +5,6 @@ import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.springframework.format.annotation.DateTimeFormat;
import java.time.LocalDateTime;
import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND;
@Schema(description = "管理后台 - 社交客户端分页 Request VO")
@Data
@ -29,14 +24,7 @@ public class SocialClientPageReqVO extends PageParam {
@Schema(description = "客户端编号", example = "145442115")
private String clientId;
@Schema(description = "客户端密钥", example = "215151515154446")
private String clientSecret;
@Schema(description = "状态", example = "1")
private Integer status;
@Schema(description = "创建时间")
@DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
private LocalDateTime[] createTime;
}

View File

@ -20,10 +20,10 @@ public class SocialUserBaseVO {
@NotNull(message = "社交 openid不能为空")
private String openid;
@Schema(description = "社交 token", example = "666")
@Schema(description = "社交 token", requiredMode = Schema.RequiredMode.REQUIRED, example = "666")
private String token;
@Schema(description = "原始 Token 数据,一般是 JSON 格式", example = "{}")
@Schema(description = "原始 Token 数据,一般是 JSON 格式", requiredMode = Schema.RequiredMode.REQUIRED, example = "{}")
private String rawTokenInfo;
@Schema(description = "用户昵称", requiredMode = Schema.RequiredMode.REQUIRED, example = "芋艿")
@ -33,13 +33,13 @@ public class SocialUserBaseVO {
@Schema(description = "用户头像", example = "https://www.iocoder.cn/xxx.png")
private String avatar;
@Schema(description = "原始用户数据,一般是 JSON 格式", example = "{}")
@Schema(description = "原始用户数据,一般是 JSON 格式", requiredMode = Schema.RequiredMode.REQUIRED, example = "{}")
private String rawUserInfo;
@Schema(description = "最后一次的认证 code", example = "666666")
@Schema(description = "最后一次的认证 code", requiredMode = Schema.RequiredMode.REQUIRED, example = "666666")
private String code;
@Schema(description = "最后一次的认证 state", example = "123456")
@Schema(description = "最后一次的认证 state", requiredMode = Schema.RequiredMode.REQUIRED, example = "123456")
private String state;
}

View File

@ -23,6 +23,9 @@ public class SocialUserPageReqVO extends PageParam {
@Schema(description = "用户昵称", example = "李四")
private String nickname;
@Schema(description = "社交 openid", example = "oz-Jdt0kd_jdhUxJHQdBJMlOFN7w\n")
private String openid;
@Schema(description = "创建时间")
@DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
private LocalDateTime[] createTime;

View File

@ -19,4 +19,7 @@ public class SocialUserRespVO extends SocialUserBaseVO {
@Schema(description = "创建时间", requiredMode = Schema.RequiredMode.REQUIRED)
private LocalDateTime createTime;
@Schema(description = "更新时间", requiredMode = Schema.RequiredMode.REQUIRED)
private LocalDateTime updateTime;
}

View File

@ -1,20 +0,0 @@
package cn.iocoder.yudao.module.system.controller.admin.socail.vo.user;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import javax.validation.constraints.NotNull;
@Schema(description = "管理后台 - 社交用户更新 Request VO")
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public class SocialUserUpdateReqVO extends SocialUserBaseVO {
@Schema(description = "主键(自增策略)", requiredMode = Schema.RequiredMode.REQUIRED, example = "14569")
@NotNull(message = "主键(自增策略)不能为空")
private Long id;
}

View File

@ -6,7 +6,6 @@ import cn.iocoder.yudao.module.system.api.social.dto.SocialUserUnbindReqDTO;
import cn.iocoder.yudao.module.system.controller.admin.socail.vo.SocialUserBindReqVO;
import cn.iocoder.yudao.module.system.controller.admin.socail.vo.SocialUserUnbindReqVO;
import cn.iocoder.yudao.module.system.controller.admin.socail.vo.user.SocialUserRespVO;
import cn.iocoder.yudao.module.system.controller.admin.socail.vo.user.SocialUserUpdateReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.social.SocialUserDO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
@ -24,8 +23,6 @@ public interface SocialUserConvert {
SocialUserUnbindReqDTO convert(Long userId, Integer userType, SocialUserUnbindReqVO reqVO);
SocialUserDO convert(SocialUserUpdateReqVO bean);
SocialUserRespVO convert(SocialUserDO bean);
List<SocialUserRespVO> convertList(List<SocialUserDO> list);

View File

@ -64,8 +64,12 @@ public class SocialClientDO extends TenantBaseDO {
* 客户端 Secret
*/
private String clientSecret;
/**
* 授权方的网页应用 ID
* 代理编号
*
* 目前只有部分社交类型在使用
* 1. 企业微信对应授权方的网页应用 ID
*/
private String agentId;

View File

@ -21,9 +21,7 @@ public interface SocialClientMapper extends BaseMapperX<SocialClientDO> {
.eqIfPresent(SocialClientDO::getSocialType, reqVO.getSocialType())
.eqIfPresent(SocialClientDO::getUserType, reqVO.getUserType())
.eqIfPresent(SocialClientDO::getClientId, reqVO.getClientId())
.eqIfPresent(SocialClientDO::getClientSecret, reqVO.getClientSecret())
.eqIfPresent(SocialClientDO::getStatus, reqVO.getStatus())
.betweenIfPresent(SocialClientDO::getCreateTime, reqVO.getCreateTime())
.orderByDesc(SocialClientDO::getId));
}

View File

@ -28,6 +28,7 @@ public interface SocialUserMapper extends BaseMapperX<SocialUserDO> {
return selectPage(reqVO, new LambdaQueryWrapperX<SocialUserDO>()
.eqIfPresent(SocialUserDO::getType, reqVO.getType())
.likeIfPresent(SocialUserDO::getNickname, reqVO.getNickname())
.likeIfPresent(SocialUserDO::getOpenid, reqVO.getOpenid())
.betweenIfPresent(SocialUserDO::getCreateTime, reqVO.getCreateTime())
.orderByDesc(SocialUserDO::getId));
}

View File

@ -11,8 +11,6 @@ import com.xingyuv.jushauth.model.AuthUser;
import me.chanjar.weixin.common.bean.WxJsapiSignature;
import javax.validation.Valid;
import java.util.Collection;
import java.util.List;
/**
* 社交应用 Service 接口
@ -96,14 +94,6 @@ public interface SocialClientService {
*/
SocialClientDO getSocialClient(Long id);
/**
* 获得社交客户端列表
*
* @param ids 编号
* @return 社交客户端列表
*/
List<SocialClientDO> getSocialClientList(Collection<Long> ids);
/**
* 获得社交客户端分页
*

View File

@ -5,9 +5,8 @@ import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
import cn.binarywang.wx.miniapp.config.impl.WxMaRedisBetterConfigImpl;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
@ -23,6 +22,7 @@ import cn.iocoder.yudao.module.system.dal.mysql.social.SocialClientMapper;
import cn.iocoder.yudao.module.system.enums.social.SocialTypeEnum;
import com.binarywang.spring.starter.wxjava.miniapp.properties.WxMaProperties;
import com.binarywang.spring.starter.wxjava.mp.properties.WxMpProperties;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.xingyuv.jushauth.config.AuthConfig;
@ -44,8 +44,6 @@ import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.time.Duration;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
@ -224,7 +222,7 @@ public class SocialClientServiceImpl implements SocialClientService {
return service.getUserService().getPhoneNoInfo(phoneCode);
} catch (WxErrorException e) {
log.error("[getPhoneNoInfo][userType({}) phoneCode({}) 获得手机号失败]", userType, phoneCode, e);
throw exception(SOCIAL_APP_WEIXIN_MINI_APP_PHONE_CODE_ERROR);
throw exception(SOCIAL_CLIENT_WEIXIN_MINI_APP_PHONE_CODE_ERROR);
}
}
@ -270,6 +268,9 @@ public class SocialClientServiceImpl implements SocialClientService {
@Override
public Long createSocialClient(SocialClientCreateReqVO createReqVO) {
// 校验重复
validateSocialClientUnique(null, createReqVO.getUserType(), createReqVO.getSocialType());
// 插入
SocialClientDO socialClient = SocialClientConvert.INSTANCE.convert(createReqVO);
socialClientMapper.insert(socialClient);
@ -281,6 +282,9 @@ public class SocialClientServiceImpl implements SocialClientService {
public void updateSocialClient(SocialClientUpdateReqVO updateReqVO) {
// 校验存在
validateSocialClientExists(updateReqVO.getId());
// 校验重复
validateSocialClientUnique(updateReqVO.getId(), updateReqVO.getUserType(), updateReqVO.getSocialType());
// 更新
SocialClientDO updateObj = SocialClientConvert.INSTANCE.convert(updateReqVO);
socialClientMapper.updateById(updateObj);
@ -300,17 +304,31 @@ public class SocialClientServiceImpl implements SocialClientService {
}
}
@Override
public SocialClientDO getSocialClient(Long id) {
return socialClientMapper.selectById(id);
/**
* 校验社交应用是否重复需要保证 userType + socialType 唯一
*
* 原因是不同端userType选择某个社交登录socialType需要通过 {@link #buildAuthRequest(Integer, Integer)} 构建对应的请求
*
* @param id 编号
* @param userType 用户类型
* @param socialType 社交类型
*/
@VisibleForTesting
private void validateSocialClientUnique(Long id, Integer userType, Integer socialType) {
SocialClientDO client = socialClientMapper.selectBySocialTypeAndUserType(
socialType, userType);
if (client == null) {
return;
}
if (id == null // 新增时说明重复
|| ObjUtil.notEqual(id, client.getId())) { // 更新时如果 id 不一致说明重复
throw exception(SOCIAL_CLIENT_UNIQUE);
}
}
@Override
public List<SocialClientDO> getSocialClientList(Collection<Long> ids) {
if (CollUtil.isEmpty(ids)) {
return ListUtil.empty();
}
return socialClientMapper.selectBatchIds(ids);
public SocialClientDO getSocialClient(Long id) {
return socialClientMapper.selectById(id);
}
@Override

View File

@ -5,12 +5,10 @@ import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.system.api.social.dto.SocialUserBindReqDTO;
import cn.iocoder.yudao.module.system.api.social.dto.SocialUserRespDTO;
import cn.iocoder.yudao.module.system.controller.admin.socail.vo.user.SocialUserPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.socail.vo.user.SocialUserUpdateReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.social.SocialUserDO;
import cn.iocoder.yudao.module.system.enums.social.SocialTypeEnum;
import javax.validation.Valid;
import java.util.Collection;
import java.util.List;
/**
@ -62,20 +60,6 @@ public interface SocialUserService {
// ==================== 社交用户 CRUD ====================
/**
* 更新社交用户
*
* @param updateReqVO 更新信息
*/
void updateSocialUser(@Valid SocialUserUpdateReqVO updateReqVO);
/**
* 删除社交用户
*
* @param id 编号
*/
void deleteSocialUser(Long id);
/**
* 获得社交用户
*
@ -84,14 +68,6 @@ public interface SocialUserService {
*/
SocialUserDO getSocialUser(Long id);
/**
* 获得社交用户列表
*
* @param ids 编号
* @return 社交用户列表
*/
List<SocialUserDO> getSocialUserList(Collection<Long> ids);
/**
* 获得社交用户分页
*

View File

@ -8,7 +8,6 @@ import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.system.api.social.dto.SocialUserBindReqDTO;
import cn.iocoder.yudao.module.system.api.social.dto.SocialUserRespDTO;
import cn.iocoder.yudao.module.system.controller.admin.socail.vo.user.SocialUserPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.socail.vo.user.SocialUserUpdateReqVO;
import cn.iocoder.yudao.module.system.convert.social.SocialUserConvert;
import cn.iocoder.yudao.module.system.dal.dataobject.social.SocialUserBindDO;
import cn.iocoder.yudao.module.system.dal.dataobject.social.SocialUserDO;
@ -153,42 +152,11 @@ public class SocialUserServiceImpl implements SocialUserService {
// ==================== 社交用户 CRUD ====================
@Override
public void updateSocialUser(SocialUserUpdateReqVO updateReqVO) {
// 校验存在
validateSocialUserExists(updateReqVO.getId());
// 更新
SocialUserDO updateObj = SocialUserConvert.INSTANCE.convert(updateReqVO);
socialUserMapper.updateById(updateObj);
}
@Override
public void deleteSocialUser(Long id) {
// 校验存在
validateSocialUserExists(id);
// 删除
socialUserMapper.deleteById(id);
}
private void validateSocialUserExists(Long id) {
if (socialUserMapper.selectById(id) == null) {
throw exception(SOCIAL_USER_NOT_EXISTS);
}
}
@Override
public SocialUserDO getSocialUser(Long id) {
return socialUserMapper.selectById(id);
}
@Override
public List<SocialUserDO> getSocialUserList(Collection<Long> ids) {
if (CollUtil.isEmpty(ids)) {
return ListUtil.empty();
}
return socialUserMapper.selectBatchIds(ids);
}
@Override
public PageResult<SocialUserDO> getSocialUserPage(SocialUserPageReqVO pageReqVO) {
return socialUserMapper.selectPage(pageReqVO);

View File

@ -13,7 +13,6 @@ import org.springframework.context.annotation.Import;
import javax.annotation.Resource;
import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildBetweenTime;
import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId;
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals;
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException;
@ -22,6 +21,7 @@ import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.SOCIAL_CLIENT_NOT_EXISTS;
import static org.junit.jupiter.api.Assertions.*;
// TODO 芋艿单测后续补充下
/**
* {@link SocialClientServiceImpl} 的单元测试类
*
@ -133,9 +133,7 @@ public class SocialClientServiceImplTest extends BaseDbUnitTest {
reqVO.setSocialType(null);
reqVO.setUserType(null);
reqVO.setClientId(null);
reqVO.setClientSecret(null);
reqVO.setStatus(null);
reqVO.setCreateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));
// 调用
PageResult<SocialClientDO> pageResult = socialClientService.getSocialClientPage(reqVO);