...
|
...
|
@@ -11,28 +11,34 @@ import lombok.RequiredArgsConstructor; |
11
|
11
|
import lombok.extern.slf4j.Slf4j;
|
12
|
12
|
import org.apache.commons.lang3.RandomStringUtils;
|
13
|
13
|
import org.apache.commons.lang3.StringUtils;
|
|
14
|
+import org.springframework.context.ApplicationEventPublisher;
|
14
|
15
|
import org.springframework.scheduling.annotation.Async;
|
15
|
16
|
import org.springframework.security.access.AccessDeniedException;
|
|
17
|
+import org.springframework.security.authentication.BadCredentialsException;
|
16
|
18
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
17
|
19
|
import org.springframework.stereotype.Service;
|
18
|
20
|
import org.springframework.transaction.annotation.Transactional;
|
|
21
|
+import org.thingsboard.server.common.data.edge.EdgeEventActionType;
|
19
|
22
|
import org.thingsboard.server.common.data.id.EntityId;
|
|
23
|
+import org.thingsboard.server.common.data.id.TenantId;
|
|
24
|
+import org.thingsboard.server.common.data.id.UserId;
|
20
|
25
|
import org.thingsboard.server.common.data.query.TsValue;
|
|
26
|
+import org.thingsboard.server.common.data.security.UserCredentials;
|
|
27
|
+import org.thingsboard.server.common.data.security.event.UserAuthDataChangedEvent;
|
21
|
28
|
import org.thingsboard.server.common.data.yunteng.constant.FastIotConstants;
|
22
|
29
|
import org.thingsboard.server.common.data.yunteng.constant.ModelConstants;
|
|
30
|
+import org.thingsboard.server.common.data.yunteng.core.cache.CacheUtils;
|
23
|
31
|
import org.thingsboard.server.common.data.yunteng.core.exception.YtDataValidationException;
|
24
|
32
|
import org.thingsboard.server.common.data.yunteng.core.exception.NoneTenantAssetException;
|
25
|
33
|
import org.thingsboard.server.common.data.yunteng.core.message.ErrorMessage;
|
26
|
34
|
import org.thingsboard.server.common.data.yunteng.dto.*;
|
27
|
|
-import org.thingsboard.server.common.data.yunteng.dto.request.AccountReqDTO;
|
28
|
|
-import org.thingsboard.server.common.data.yunteng.dto.request.RoleOrOrganizationReqDTO;
|
29
|
|
-import org.thingsboard.server.common.data.yunteng.dto.request.SendResetPasswordEmailMsg;
|
30
|
|
-import org.thingsboard.server.common.data.yunteng.dto.request.SmsReqDTO;
|
|
35
|
+import org.thingsboard.server.common.data.yunteng.dto.request.*;
|
31
|
36
|
import org.thingsboard.server.common.data.yunteng.enums.MessageTypeEnum;
|
32
|
37
|
import org.thingsboard.server.common.data.yunteng.enums.MsgTemplatePurposeEnum;
|
33
|
38
|
import org.thingsboard.server.common.data.yunteng.enums.UserStatusEnum;
|
34
|
39
|
import org.thingsboard.server.common.data.yunteng.utils.ReflectUtils;
|
35
|
40
|
import org.thingsboard.server.common.data.yunteng.utils.tools.YtPageData;
|
|
41
|
+import org.thingsboard.server.dao.user.UserService;
|
36
|
42
|
import org.thingsboard.server.dao.yunteng.entities.*;
|
37
|
43
|
import org.thingsboard.server.dao.yunteng.mapper.*;
|
38
|
44
|
import org.thingsboard.server.dao.yunteng.service.*;
|
...
|
...
|
@@ -43,8 +49,9 @@ import java.util.*; |
43
|
49
|
import java.util.concurrent.CompletableFuture;
|
44
|
50
|
import java.util.stream.Collectors;
|
45
|
51
|
|
46
|
|
-import static org.thingsboard.server.common.data.yunteng.constant.FastIotConstants.CHINA_MOBILE_PATTERN;
|
47
|
|
-import static org.thingsboard.server.common.data.yunteng.constant.FastIotConstants.EMAIL_PATTERN;
|
|
52
|
+import static org.thingsboard.server.common.data.yunteng.constant.FastIotConstants.*;
|
|
53
|
+import static org.thingsboard.server.common.data.yunteng.constant.FastIotConstants.CacheConfigKey.MOBILE_LOGIN_SMS_CODE;
|
|
54
|
+import static org.thingsboard.server.common.data.yunteng.constant.FastIotConstants.DEFAULT_DELIMITER;
|
48
|
55
|
import static org.thingsboard.server.common.data.yunteng.constant.ModelConstants.TablePropertyMapping.*;
|
49
|
56
|
|
50
|
57
|
@Service
|
...
|
...
|
@@ -68,6 +75,10 @@ public class YtUserServiceImpl extends AbstractBaseService<UserMapper, User> |
68
|
75
|
public static final String ACTIVATE_URL_PATTERN = "%s/api/noauth/activate?activateToken=%s";
|
69
|
76
|
private final PasswordEncoder passwordEncoder;
|
70
|
77
|
|
|
78
|
+ private CacheUtils cacheUtils;
|
|
79
|
+ private final UserService tbUserService;
|
|
80
|
+ private final ApplicationEventPublisher eventPublisher;
|
|
81
|
+
|
71
|
82
|
@Override
|
72
|
83
|
public List<UserDetailsDTO> findUserDetailsByUsername(String username) {
|
73
|
84
|
// 多个租户可能存在多个username相同的情况
|
...
|
...
|
@@ -427,6 +438,52 @@ public class YtUserServiceImpl extends AbstractBaseService<UserMapper, User> |
427
|
438
|
}
|
428
|
439
|
|
429
|
440
|
@Override
|
|
441
|
+ public void forgetPassword(String phoneNumber,AccountReqDTO forget) {
|
|
442
|
+ String key =
|
|
443
|
+ MsgTemplatePurposeEnum.FOR_FORGET_PASSWORD.name()
|
|
444
|
+ + DEFAULT_DELIMITER
|
|
445
|
+ + MessageTypeEnum.PHONE_MESSAGE.name()
|
|
446
|
+ + DEFAULT_DELIMITER
|
|
447
|
+ + phoneNumber;
|
|
448
|
+ boolean correct =
|
|
449
|
+ cacheUtils
|
|
450
|
+ .get(MOBILE_LOGIN_SMS_CODE, key)
|
|
451
|
+ .map(
|
|
452
|
+ o -> {
|
|
453
|
+ CodeTTL codeTTL = (CodeTTL) o;
|
|
454
|
+ if (System.currentTimeMillis() - codeTTL.getSendTs() < 5 * 60 * 1000) {
|
|
455
|
+ return Objects.equals(codeTTL.getCode(), forget.getUserId());
|
|
456
|
+ } else {
|
|
457
|
+ return false;
|
|
458
|
+ }
|
|
459
|
+ })
|
|
460
|
+ .orElse(false);
|
|
461
|
+ if (!correct) {
|
|
462
|
+ throw new BadCredentialsException("验证码不正确");
|
|
463
|
+ }
|
|
464
|
+ String pwd = forget.getPassword();
|
|
465
|
+ if (StringUtils.isEmpty(pwd)
|
|
466
|
+ || StringUtils.isEmpty(forget.getResetPassword())
|
|
467
|
+ || !pwd.equals(forget.getResetPassword())) {
|
|
468
|
+ throw new YtDataValidationException(ErrorMessage.INVALID_PARAMETER.getMessage());
|
|
469
|
+ }
|
|
470
|
+
|
|
471
|
+ User user = baseMapper.selectOne(new QueryWrapper<User>().lambda().eq(User::getPhoneNumber, phoneNumber));
|
|
472
|
+
|
|
473
|
+ UserId userId = new UserId(UUID.fromString(user.getTbUser()));
|
|
474
|
+ UserCredentials userCredentials =
|
|
475
|
+ tbUserService.findUserCredentialsByUserId(TenantId.SYS_TENANT_ID, userId);
|
|
476
|
+
|
|
477
|
+ String encodePwd = passwordEncoder.encode(pwd);
|
|
478
|
+ userCredentials.setPassword(encodePwd);
|
|
479
|
+ user.setPassword(encodePwd);
|
|
480
|
+ tbUserService.replaceUserCredentials(new TenantId(UUID.fromString(user.getTenantId())), userCredentials);
|
|
481
|
+ eventPublisher.publishEvent(new UserAuthDataChangedEvent(userId));
|
|
482
|
+
|
|
483
|
+ changePassword(user);
|
|
484
|
+ }
|
|
485
|
+
|
|
486
|
+ @Override
|
430
|
487
|
public List<UserDetailsDTO> getUserByPhoneNumber(String phoneNumber) {
|
431
|
488
|
return baseMapper.findUserDetailsByPhoneNumber(phoneNumber);
|
432
|
489
|
}
|
...
|
...
|
@@ -586,6 +643,11 @@ public class YtUserServiceImpl extends AbstractBaseService<UserMapper, User> |
586
|
643
|
@Override
|
587
|
644
|
public User validateChangePasswordAccount(AccountReqDTO accountReqDTO) {
|
588
|
645
|
User user = baseMapper.selectById(accountReqDTO.getUserId());
|
|
646
|
+ checkPassword(accountReqDTO, user);
|
|
647
|
+ return user;
|
|
648
|
+ }
|
|
649
|
+
|
|
650
|
+ private void checkPassword(AccountReqDTO accountReqDTO, User user) {
|
589
|
651
|
if (null == user
|
590
|
652
|
|| StringUtils.isEmpty(accountReqDTO.getPassword())
|
591
|
653
|
|| StringUtils.isEmpty(accountReqDTO.getResetPassword())) {
|
...
|
...
|
@@ -597,7 +659,6 @@ public class YtUserServiceImpl extends AbstractBaseService<UserMapper, User> |
597
|
659
|
throw new YtDataValidationException(ErrorMessage.USERNAME_PASSWORD_INCORRECT.getMessage());
|
598
|
660
|
}
|
599
|
661
|
user.setPassword(accountReqDTO.getResetPassword());
|
600
|
|
- return user;
|
601
|
662
|
}
|
602
|
663
|
|
603
|
664
|
/**
|
...
|
...
|
|