Commit 0aa90ac755fa24ff633f0ab2c234690df070c495
1 parent
966ab5fc
refactor: 第三方平台用户绑定和解绑接口逻辑优化
1、绑定的时候验证平台用户是否已经绑定 2、解绑的时候只能操作同一租户下的账号
Showing
4 changed files
with
29 additions
and
22 deletions
... | ... | @@ -65,16 +65,15 @@ public class YtThirdPlatformController extends BaseController { |
65 | 65 | @ApiOperation("绑定") |
66 | 66 | public JwtTokenPair saveOrUpdateAlarmProfile( |
67 | 67 | @Validated @RequestBody YtThirdUserDTO dto) throws ThingsboardException { |
68 | - UserDTO userDto = thirdService.saveOrUpdate(dto); | |
68 | + UserDTO userDto = thirdService.bindUser(dto); | |
69 | 69 | return buildJwtToken(userDto); |
70 | 70 | } |
71 | 71 | |
72 | 72 | @DeleteMapping |
73 | - @ApiOperation("删除") | |
74 | - public boolean deleteAlarmProfile(@Validated(DeleteGroup.class) @RequestBody DeleteDTO deleteDTO) | |
73 | + @ApiOperation("解绑") | |
74 | + public boolean deleteAlarmProfile(@Validated(DeleteGroup.class) @RequestBody YtThirdUserDTO dto) | |
75 | 75 | throws ThingsboardException { |
76 | - deleteDTO.setTenantId(getCurrentUser().getCurrentTenantId()); | |
77 | - return thirdService.deleteDataByIds(deleteDTO); | |
76 | + return thirdService.unbindUser(getCurrentUser().getCurrentTenantId(), dto.getAppUserId(),dto.getThirdUserId()); | |
78 | 77 | } |
79 | 78 | |
80 | 79 | @GetMapping("login/{thirdId}") | ... | ... |
... | ... | @@ -59,7 +59,8 @@ public enum ErrorMessage { |
59 | 59 | DEVICE_LOSED(400039,"设备相关参数丢失"), |
60 | 60 | SCENE_REACT_NOT_EXTIED(400040,"场景联动不存在"), |
61 | 61 | DEVICE_USED_SCENE_REACT(400041,"场景联动【%s】正在使用该设备"), |
62 | - SCENE_REACT_USED_ALARM_PROFILE(400041,"场景联动正在使用该告警配置"), | |
62 | + SCENE_REACT_USED_ALARM_PROFILE(400042,"场景联动正在使用该告警配置"), | |
63 | + APP_USER_BINDED(400043,"账号【%s】已绑定"), | |
63 | 64 | HAVE_NO_PERMISSION(500002,"没有修改权限"); |
64 | 65 | private final int code; |
65 | 66 | private String message; | ... | ... |
... | ... | @@ -60,7 +60,7 @@ public class YtThirdPlatformServiceImpl extends AbstractBaseService<YtThirdPlatf |
60 | 60 | |
61 | 61 | @Override |
62 | 62 | @Transactional(rollbackFor = Exception.class) |
63 | - public UserDTO saveOrUpdate(YtThirdUserDTO dto) { | |
63 | + public UserDTO bindUser(YtThirdUserDTO dto) { | |
64 | 64 | User user = null; |
65 | 65 | switch (dto.getLoginMethod()) { |
66 | 66 | case PHONE: |
... | ... | @@ -73,21 +73,21 @@ public class YtThirdPlatformServiceImpl extends AbstractBaseService<YtThirdPlatf |
73 | 73 | if (user == null) { |
74 | 74 | throw new YtDataValidationException(ErrorMessage.USER_NOT_EXISTS.getMessage()); |
75 | 75 | } |
76 | + | |
77 | + Wrapper filter = new QueryWrapper<YtThirdUserEntity>().lambda() | |
78 | + .eq(YtThirdUserEntity::getAppUserId, user.getId()); | |
79 | + YtThirdUserEntity oldBind = baseMapper.selectOne(filter); | |
80 | + if(oldBind != null){ | |
81 | + throw new YtDataValidationException(String.format(ErrorMessage.APP_USER_BINDED.getMessage(),user.getUsername())); | |
82 | + } | |
83 | + | |
76 | 84 | if (StringUtils.isEmpty(user.getAvatar()) && StringUtils.isNotEmpty(dto.getAvatarUrl())) { |
77 | 85 | user.setAvatar(dto.getAvatarUrl()); |
78 | 86 | userMapper.updateById(user); |
79 | 87 | } |
80 | 88 | dto.setAppUserId(user.getId()); |
89 | + baseMapper.insert(dto.getEntity(YtThirdUserEntity.class)); | |
81 | 90 | |
82 | - Wrapper filter = new QueryWrapper<YtThirdUserEntity>().lambda() | |
83 | - .eq(YtThirdUserEntity::getThirdUserId, dto.getThirdUserId()); | |
84 | - YtThirdUserEntity oldVideo = baseMapper.selectOne(filter); | |
85 | - if (null == oldVideo) { | |
86 | - baseMapper.insert(dto.getEntity(YtThirdUserEntity.class)); | |
87 | - } else { | |
88 | - dto.setId(oldVideo.getId()); | |
89 | - baseMapper.updateById(dto.getEntity(YtThirdUserEntity.class)); | |
90 | - } | |
91 | 91 | return user.getDTO(UserDTO.class); |
92 | 92 | } |
93 | 93 | |
... | ... | @@ -159,9 +159,15 @@ public class YtThirdPlatformServiceImpl extends AbstractBaseService<YtThirdPlatf |
159 | 159 | |
160 | 160 | @Override |
161 | 161 | @Transactional(rollbackFor = Exception.class) |
162 | - public boolean deleteDataByIds(DeleteDTO deleteDTO) { | |
162 | + public boolean unbindUser(String tenantId,String appUserId,String thirdUserId) { | |
163 | + | |
164 | + User user = userMapper.selectById(appUserId); | |
165 | + if(!tenantId.equals(user.getTenantId())){ | |
166 | + throw new YtDataValidationException(ErrorMessage.NO_PERMISSION.getMessage()); | |
167 | + } | |
163 | 168 | Wrapper filter = new QueryWrapper<YtThirdUserEntity>().lambda() |
164 | - .in(YtThirdUserEntity::getId, deleteDTO.getIds()); | |
169 | + .eq(YtThirdUserEntity::getAppUserId,appUserId) | |
170 | + .eq(YtThirdUserEntity::getThirdUserId,thirdUserId); | |
165 | 171 | return baseMapper.delete(filter) > 0; |
166 | 172 | } |
167 | 173 | ... | ... |
... | ... | @@ -7,7 +7,6 @@ import org.thingsboard.server.common.data.yunteng.dto.UserDTO; |
7 | 7 | import org.thingsboard.server.common.data.yunteng.dto.YtThirdUserDTO; |
8 | 8 | import org.thingsboard.server.common.data.yunteng.enums.ThirdPlatformEnum; |
9 | 9 | import org.thingsboard.server.common.data.yunteng.utils.tools.YtPageData; |
10 | -import org.thingsboard.server.dao.yunteng.entities.User; | |
11 | 10 | import org.thingsboard.server.dao.yunteng.entities.YtThirdUserEntity; |
12 | 11 | |
13 | 12 | /** |
... | ... | @@ -32,13 +31,15 @@ public interface YtThirdPlatformService extends BaseService<YtThirdUserEntity> { |
32 | 31 | * @param dto |
33 | 32 | * @return |
34 | 33 | */ |
35 | - UserDTO saveOrUpdate(YtThirdUserDTO dto); | |
34 | + UserDTO bindUser(YtThirdUserDTO dto); | |
36 | 35 | |
37 | 36 | /** |
38 | - * @param deleteDTO | |
37 | + * 第三方平台用户与系统用户解绑 | |
38 | + * @param appUserId | |
39 | + * @param thirdUserId | |
39 | 40 | * @return |
40 | 41 | */ |
41 | - boolean deleteDataByIds(DeleteDTO deleteDTO); | |
42 | + boolean unbindUser(String tenantId,String appUserId,String thirdUserId); | |
42 | 43 | |
43 | 44 | /** |
44 | 45 | * 第三方登录 | ... | ... |