feat: 个人中心-安全设置,支持绑定、解绑三方账号

This commit is contained in:
2023-10-19 23:44:56 +08:00
parent 36d52d3e15
commit efe455736c
19 changed files with 520 additions and 76 deletions

View File

@@ -0,0 +1,48 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package top.charles7c.cnadmin.system.model.vo;
import java.io.Serializable;
import lombok.Data;
import io.swagger.v3.oas.annotations.media.Schema;
/**
* 第三方账号绑定信息
*
* @author Charles7c
* @since 2023/10/19 21:29
*/
@Data
@Schema(description = "第三方账号绑定信息")
public class UserSocialBindVO implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 来源
*/
@Schema(description = "来源", example = "GITEE")
private String source;
/**
* 描述
*/
@Schema(description = "描述", example = "码云")
private String description;
}

View File

@@ -16,8 +16,12 @@
package top.charles7c.cnadmin.system.service;
import java.util.List;
import top.charles7c.cnadmin.system.model.entity.UserSocialDO;
import me.zhyd.oauth.model.AuthUser;
/**
* 用户社会化关联业务接口
*
@@ -44,4 +48,33 @@ public interface UserSocialService {
* 用户社会化关联信息
*/
void saveOrUpdate(UserSocialDO userSocial);
/**
* 根据用户 ID 查询
*
* @param userId
* 用户 ID
* @return 用户社会化关联信息
*/
List<UserSocialDO> listByUserId(Long userId);
/**
* 绑定
*
* @param authUser
* 社交身份信息
* @param userId
* 用户 ID
*/
void bind(AuthUser authUser, Long userId);
/**
* 根据来源和用户 ID 删除
*
* @param source
* 来源
* @param userId
* 用户 ID
*/
void deleteBySourceAndUserId(String source, Long userId);
}

View File

@@ -16,15 +16,25 @@
package top.charles7c.cnadmin.system.service.impl;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.json.JSONUtil;
import top.charles7c.cnadmin.common.util.validate.CheckUtils;
import top.charles7c.cnadmin.system.mapper.UserSocialMapper;
import top.charles7c.cnadmin.system.model.entity.UserSocialDO;
import top.charles7c.cnadmin.system.service.UserSocialService;
import me.zhyd.oauth.model.AuthUser;
/**
* 用户社会化关联业务实现
*
@@ -54,4 +64,32 @@ public class UserSocialServiceImpl implements UserSocialService {
.update();
}
}
@Override
public List<UserSocialDO> listByUserId(Long userId) {
return baseMapper.lambdaQuery().eq(UserSocialDO::getUserId, userId).list();
}
@Override
public void bind(AuthUser authUser, Long userId) {
String source = authUser.getSource();
String openId = authUser.getUuid();
List<UserSocialDO> userSocialList = this.listByUserId(userId);
Set<String> boundSocialSet = userSocialList.stream().map(UserSocialDO::getSource).collect(Collectors.toSet());
CheckUtils.throwIf(boundSocialSet.contains(source), "您已经绑定过了 [{}] 平台,请先解绑");
UserSocialDO userSocial = this.getBySourceAndOpenId(source, openId);
CheckUtils.throwIfNotNull(userSocial, "[{}] 平台账号 [{}] 已被其他用户绑定", source, authUser.getUsername());
userSocial = new UserSocialDO();
userSocial.setUserId(userId);
userSocial.setSource(source);
userSocial.setOpenId(openId);
userSocial.setMetaJson(JSONUtil.toJsonStr(authUser));
userSocial.setLastLoginTime(LocalDateTime.now());
baseMapper.insert(userSocial);
}
@Override
public void deleteBySourceAndUserId(String source, Long userId) {
baseMapper.lambdaUpdate().eq(UserSocialDO::getSource, source).eq(UserSocialDO::getUserId, userId).remove();
}
}