Showing
1 changed file
with
117 additions
and
104 deletions
... | ... | @@ -10,10 +10,14 @@ import org.springframework.web.bind.annotation.*; |
10 | 10 | import org.thingsboard.server.common.data.StringUtils; |
11 | 11 | import org.thingsboard.server.common.data.User; |
12 | 12 | import org.thingsboard.server.common.data.exception.ThingsboardException; |
13 | +import org.thingsboard.server.common.data.id.EntityId; | |
14 | +import org.thingsboard.server.common.data.id.TenantId; | |
13 | 15 | import org.thingsboard.server.common.data.id.UserId; |
16 | +import org.thingsboard.server.common.data.security.Authority; | |
14 | 17 | import org.thingsboard.server.common.data.security.UserCredentials; |
15 | 18 | import org.thingsboard.server.common.data.yunteng.common.DeleteGroup; |
16 | 19 | import org.thingsboard.server.common.data.yunteng.core.message.ErrorMessage; |
20 | +import org.thingsboard.server.common.data.yunteng.core.utils.AccountProperties; | |
17 | 21 | import org.thingsboard.server.common.data.yunteng.dto.AuthorizeDTO; |
18 | 22 | import org.thingsboard.server.common.data.yunteng.dto.UserDTO; |
19 | 23 | import org.thingsboard.server.common.data.yunteng.dto.UserDetailsDTO; |
... | ... | @@ -46,113 +50,122 @@ import static org.thingsboard.server.common.data.yunteng.constant.QueryConstant. |
46 | 50 | @RequiredArgsConstructor |
47 | 51 | public class TkThirdPlatformController extends BaseController { |
48 | 52 | |
49 | - private final JwtTokenFactory tokenFactory; | |
50 | - private final RefreshTokenRepository refreshTokenRepository; | |
51 | - private final TkThirdPlatformService thirdService; | |
52 | - private final TkUserService tkUserService; | |
53 | - | |
54 | - @GetMapping(params = {PAGE_SIZE, PAGE}) | |
55 | - @ApiOperation("分页") | |
56 | - public TkPageData<TkThirdUserDTO> pageAlarmProfile( | |
57 | - @RequestParam(PAGE_SIZE) int pageSize, | |
58 | - @RequestParam(PAGE) int page, | |
59 | - @RequestParam(value = "platformName", required = false) ThirdPlatformEnum platformName, | |
60 | - @RequestParam(value = "name", required = false) String name, | |
61 | - @RequestParam(value = ORDER_FILED, required = false) String orderBy, | |
62 | - @RequestParam(value = ORDER_TYPE, required = false) OrderTypeEnum orderType) | |
63 | - throws ThingsboardException { | |
64 | - | |
65 | - | |
66 | - IPage<TkThirdUserEntity> pageInfrom = thirdService.getPage(page, pageSize, orderBy, orderType); | |
67 | - return thirdService.pageDatas(pageInfrom, platformName, name); | |
68 | - } | |
69 | - | |
70 | - @PostMapping("bind") | |
71 | - @ApiOperation("绑定") | |
72 | - public TkLoginResponse saveOrUpdateAlarmProfile( | |
73 | - @Validated @RequestBody TkThirdUserDTO dto) throws ThingsboardException { | |
74 | - UserDTO userDto = thirdService.bindUser(dto); | |
75 | - return buildJwtToken(userDto, dto.getThirdUserId()); | |
76 | - } | |
77 | - | |
78 | - @DeleteMapping | |
79 | - @ApiOperation("解绑(第三方小程序)") | |
80 | - public boolean deleteAlarmProfile(@Validated(DeleteGroup.class) @RequestBody TkThirdUserDTO dto) | |
81 | - throws ThingsboardException { | |
82 | - return thirdService.unbindUser(getCurrentUser().getCurrentTenantId(), dto.getAppUserId(), dto.getThirdUserId()); | |
83 | - } | |
84 | - | |
85 | - @DeleteMapping("/unbind") | |
86 | - @ApiOperation("解绑(平台账号)") | |
87 | - public boolean deleteAlarmProfile() | |
88 | - throws ThingsboardException { | |
89 | - return thirdService.unbindUser(getCurrentUser().getCurrentUserId()); | |
53 | + private final JwtTokenFactory tokenFactory; | |
54 | + private final RefreshTokenRepository refreshTokenRepository; | |
55 | + private final TkThirdPlatformService thirdService; | |
56 | + private final TkUserService tkUserService; | |
57 | + private final AccountProperties accountProperties; | |
58 | + @GetMapping(params = {PAGE_SIZE, PAGE}) | |
59 | + @ApiOperation("分页") | |
60 | + public TkPageData<TkThirdUserDTO> pageAlarmProfile( | |
61 | + @RequestParam(PAGE_SIZE) int pageSize, | |
62 | + @RequestParam(PAGE) int page, | |
63 | + @RequestParam(value = "platformName", required = false) ThirdPlatformEnum platformName, | |
64 | + @RequestParam(value = "name", required = false) String name, | |
65 | + @RequestParam(value = ORDER_FILED, required = false) String orderBy, | |
66 | + @RequestParam(value = ORDER_TYPE, required = false) OrderTypeEnum orderType) { | |
67 | + | |
68 | + IPage<TkThirdUserEntity> pageInfrom = thirdService.getPage(page, pageSize, orderBy, orderType); | |
69 | + return thirdService.pageDatas(pageInfrom, platformName, name); | |
70 | + } | |
71 | + | |
72 | + @PostMapping("bind") | |
73 | + @ApiOperation("绑定") | |
74 | + public TkLoginResponse saveOrUpdateAlarmProfile(@Validated @RequestBody TkThirdUserDTO dto) { | |
75 | + UserDTO userDto = thirdService.bindUser(dto); | |
76 | + return buildJwtToken(userDto, dto.getThirdUserId()); | |
77 | + } | |
78 | + | |
79 | + @DeleteMapping | |
80 | + @ApiOperation("解绑(第三方小程序)") | |
81 | + public boolean deleteAlarmProfile(@Validated(DeleteGroup.class) @RequestBody TkThirdUserDTO dto) | |
82 | + throws ThingsboardException { | |
83 | + return thirdService.unbindUser( | |
84 | + getCurrentUser().getCurrentTenantId(), dto.getAppUserId(), dto.getThirdUserId()); | |
85 | + } | |
86 | + | |
87 | + @DeleteMapping("/unbind") | |
88 | + @ApiOperation("解绑(平台账号)") | |
89 | + public boolean deleteAlarmProfile() throws ThingsboardException { | |
90 | + return thirdService.unbindUser(getCurrentUser().getCurrentUserId()); | |
91 | + } | |
92 | + | |
93 | + @GetMapping("login/{loginCode}") | |
94 | + @ApiOperation("第三方登录") | |
95 | + public TkLoginResponse codeLogin(@PathVariable("loginCode") String loginCode) { | |
96 | + String thirdUserId = thirdService.thirdLogin(loginCode); | |
97 | + UserDTO userDto = thirdService.login(thirdUserId); | |
98 | + if (userDto == null) { | |
99 | + return new TkLoginResponse().setThirdUserId(thirdUserId); | |
90 | 100 | } |
91 | - | |
92 | - @GetMapping("login/{loginCode}") | |
93 | - @ApiOperation("第三方登录") | |
94 | - public TkLoginResponse codeLogin(@PathVariable("loginCode") String loginCode) | |
95 | - throws ThingsboardException { | |
96 | - String thirdUserId = thirdService.thirdLogin(loginCode); | |
97 | - UserDTO userDto = thirdService.login(thirdUserId); | |
98 | - if (userDto == null) { | |
99 | - return new TkLoginResponse().setThirdUserId(thirdUserId); | |
100 | - } | |
101 | - return buildJwtToken(userDto, thirdUserId); | |
101 | + return buildJwtToken(userDto, thirdUserId); | |
102 | + } | |
103 | + | |
104 | + @GetMapping("login/id/{userId}") | |
105 | + @ApiOperation("组态用户认证") | |
106 | + public TkLoginResponse unionidLogin(@PathVariable("userId") String userId) { | |
107 | + UserDTO userDto = tkUserService.findUserInfoById(userId); | |
108 | + if (userDto == null) { | |
109 | + return new TkLoginResponse().setThirdUserId(userId); | |
102 | 110 | } |
103 | - | |
104 | - @GetMapping("login/id/{userId}") | |
105 | - @ApiOperation("组态用户认证") | |
106 | - public TkLoginResponse unionidLogin(@PathVariable("userId") String userId) | |
107 | - throws ThingsboardException { | |
108 | - UserDTO userDto = tkUserService.findUserInfoById(userId); | |
109 | - if (userDto == null) { | |
110 | - return new TkLoginResponse().setThirdUserId(userId); | |
111 | - } | |
112 | - return buildJwtToken(userDto, userId); | |
111 | + return buildJwtToken(userDto, userId); | |
112 | + } | |
113 | + | |
114 | + private String authrizeKey = "zhiyun"; | |
115 | + private String authrizeSecret = "zhiyun"; | |
116 | + | |
117 | + @PostMapping("authorize") | |
118 | + @ApiOperation("授权客户获取用户访问令牌") | |
119 | + public TkLoginResponse authorizeLogin(@Validated @RequestBody AuthorizeDTO dto) { | |
120 | + if (!authrizeKey.equals(dto.getAuthrizeKey()) | |
121 | + || !authrizeSecret.equals(dto.getAuthrizeSecret())) { | |
122 | + throw new DataValidationException(ErrorMessage.NO_PERMISSION.getMessage()); | |
113 | 123 | } |
114 | - | |
115 | - private String authrizeKey="zhiyun"; | |
116 | - private String authrizeSecret="zhiyun"; | |
117 | - @PostMapping("authorize") | |
118 | - @ApiOperation("授权客户获取用户访问令牌") | |
119 | - public TkLoginResponse authorizeLogin(@Validated @RequestBody AuthorizeDTO dto) | |
120 | - throws ThingsboardException { | |
121 | - if(!authrizeKey.equals(dto.getAuthrizeKey()) || !authrizeSecret.equals(dto.getAuthrizeSecret())){ | |
122 | - throw new DataValidationException( ErrorMessage.NO_PERMISSION.getMessage()); | |
123 | - } | |
124 | - UserDTO userDto = tkUserService.accountExist(dto.getUserName()); | |
125 | - if (userDto == null) { | |
126 | - return new TkLoginResponse().setThirdUserId(dto.getAuthrizeKey()); | |
127 | - } | |
128 | - return buildJwtToken(userDto, dto.getAuthrizeKey()); | |
124 | + UserDTO userDto = tkUserService.accountExist(dto.getUserName()); | |
125 | + if (userDto == null) { | |
126 | + return new TkLoginResponse().setThirdUserId(dto.getAuthrizeKey()); | |
129 | 127 | } |
130 | - | |
131 | - @NotNull | |
132 | - private TkLoginResponse buildJwtToken(UserDTO userDto, String thirdUserId) { | |
133 | - String accessToken = ""; | |
134 | - String refreshToken = ""; | |
135 | - if (userDto != null && StringUtils.isNotEmpty(userDto.getTbUser())) { | |
136 | - UserId userId = new UserId(UUID.fromString(userDto.getTbUser())); | |
137 | - User user = userService.findUserById(null, userId); | |
138 | - List<UserDetailsDTO> userDetailsDTOS = tkUserService.findUserDetailsByUsername(userDto.getUsername(),userDto.getTenantId()); | |
139 | - user.setUserDetailsDTO(userDetailsDTOS.get(0)); | |
140 | - UserCredentials credentials = userService.findUserCredentialsByUserId(user.getTenantId(), userId); | |
141 | - String email = user.getEmail(); | |
142 | - int emailIndex = email.indexOf("@"); | |
143 | - UserPrincipal principal = new UserPrincipal(UserPrincipal.Type.USER_NAME, emailIndex > 0 ? email.substring(0, emailIndex) : email); | |
144 | - SecurityUser securityUser = new SecurityUser(user, credentials.isEnabled(), principal); | |
145 | - securityUser.setCurrentUserId(userDto.getId()); | |
146 | - securityUser.setPlatformUserName(userDto.getUsername()); | |
147 | - accessToken = tokenFactory.createAccessJwtToken(securityUser).getToken(); | |
148 | - refreshToken = refreshTokenRepository.requestRefreshToken(securityUser).getToken(); | |
149 | - | |
150 | - } | |
151 | - TkLoginResponse result = new TkLoginResponse(); | |
152 | - result.setToken(accessToken); | |
153 | - result.setRefreshToken(refreshToken); | |
154 | - return result.setThirdUserId(thirdUserId); | |
128 | + return buildJwtToken(userDto, dto.getAuthrizeKey()); | |
129 | + } | |
130 | + | |
131 | + @NotNull | |
132 | + private TkLoginResponse buildJwtToken(UserDTO userDto, String thirdUserId) { | |
133 | + String accessToken = ""; | |
134 | + String refreshToken = ""; | |
135 | + if (userDto != null) { | |
136 | + List<UserDetailsDTO> userDetailsDTOS = | |
137 | + tkUserService.findUserDetailsByUsername(userDto.getUsername(), userDto.getTenantId()); | |
138 | + UserCredentials credentials = new UserCredentials(); | |
139 | + User user = new User(); | |
140 | + UserPrincipal principal; | |
141 | + if (StringUtils.isNotEmpty(userDto.getTbUser())) { | |
142 | + UserId userId = new UserId(UUID.fromString(userDto.getTbUser())); | |
143 | + user = userService.findUserById(null, userId); | |
144 | + credentials = userService.findUserCredentialsByUserId(user.getTenantId(), userId); | |
145 | + String email = user.getEmail(); | |
146 | + int emailIndex = email.indexOf("@"); | |
147 | + principal = | |
148 | + new UserPrincipal( | |
149 | + UserPrincipal.Type.USER_NAME, | |
150 | + emailIndex > 0 ? email.substring(0, emailIndex) : email); | |
151 | + } else { | |
152 | + String userName = userDto.getUsername(); | |
153 | + user.setAuthority(Authority.PLATFORM_USER); | |
154 | + user.setTenantId(TenantId.fromUUID(EntityId.NULL_UUID)); | |
155 | + user.setId(new UserId(UUID.fromString(userDto.getId()))); | |
156 | + user.setEmail(userName + "@" + accountProperties.getEmailSuffix()); | |
157 | + principal = new UserPrincipal(UserPrincipal.Type.USER_NAME, userName); | |
158 | + } | |
159 | + user.setUserDetailsDTO(userDetailsDTOS.get(0)); | |
160 | + SecurityUser securityUser = new SecurityUser(user, credentials.isEnabled(), principal); | |
161 | + securityUser.setCurrentUserId(userDto.getId()); | |
162 | + securityUser.setPlatformUserName(userDto.getUsername()); | |
163 | + accessToken = tokenFactory.createAccessJwtToken(securityUser).getToken(); | |
164 | + refreshToken = refreshTokenRepository.requestRefreshToken(securityUser).getToken(); | |
155 | 165 | } |
156 | - | |
157 | - | |
166 | + TkLoginResponse result = new TkLoginResponse(); | |
167 | + result.setToken(accessToken); | |
168 | + result.setRefreshToken(refreshToken); | |
169 | + return result.setThirdUserId(thirdUserId); | |
170 | + } | |
158 | 171 | } | ... | ... |