|
16 | 16 | import org.lowcoder.domain.group.service.GroupService;
|
17 | 17 | import org.lowcoder.domain.organization.model.OrgMember;
|
18 | 18 | import org.lowcoder.domain.organization.service.OrgMemberService;
|
| 19 | +import org.lowcoder.domain.organization.service.OrganizationService; |
19 | 20 | import org.lowcoder.domain.user.model.*;
|
20 | 21 | import org.lowcoder.domain.user.model.User.TransformedUserInfo;
|
21 | 22 | import org.lowcoder.domain.user.repository.UserRepository;
|
|
29 | 30 | import org.lowcoder.sdk.constants.WorkspaceMode;
|
30 | 31 | import org.lowcoder.sdk.exception.BizError;
|
31 | 32 | import org.lowcoder.sdk.exception.BizException;
|
| 33 | +import org.lowcoder.sdk.util.HashUtils; |
32 | 34 | import org.lowcoder.sdk.util.LocaleUtils;
|
33 | 35 | import org.springframework.beans.factory.annotation.Autowired;
|
34 | 36 | import org.springframework.dao.DuplicateKeyException;
|
|
40 | 42 |
|
41 | 43 | import javax.annotation.Nonnull;
|
42 | 44 | import java.security.SecureRandom;
|
| 45 | +import java.time.Instant; |
| 46 | +import java.time.temporal.ChronoUnit; |
43 | 47 | import java.util.*;
|
44 | 48 | import java.util.function.Function;
|
45 | 49 | import java.util.stream.Collectors;
|
@@ -69,12 +73,15 @@ public class UserServiceImpl implements UserService {
|
69 | 73 | @Autowired
|
70 | 74 | private OrgMemberService orgMemberService;
|
71 | 75 | @Autowired
|
| 76 | + private OrganizationService organizationService; |
| 77 | + @Autowired |
72 | 78 | private GroupService groupService;
|
73 | 79 | @Autowired
|
74 | 80 | private CommonConfig commonConfig;
|
75 | 81 | @Autowired
|
76 | 82 | private AuthenticationService authenticationService;
|
77 |
| - |
| 83 | + @Autowired |
| 84 | + private EmailCommunicationService emailCommunicationService; |
78 | 85 | private Conf<Integer> avatarMaxSizeInKb;
|
79 | 86 |
|
80 | 87 | @PostConstruct
|
@@ -262,6 +269,47 @@ public Mono<String> resetPassword(String userId) {
|
262 | 269 | });
|
263 | 270 | }
|
264 | 271 |
|
| 272 | + @Override |
| 273 | + public Mono<Boolean> lostPassword(String userEmail) { |
| 274 | + return findByName(userEmail) |
| 275 | + .zipWhen(user -> orgMemberService.getCurrentOrgMember(user.getId()) |
| 276 | + .flatMap(orgMember -> organizationService.getById(orgMember.getOrgId())) |
| 277 | + .map(organization -> organization.getCommonSettings().get("PASSWORD_RESET_EMAIL_TEMPLATE"))) |
| 278 | + .flatMap(tuple -> { |
| 279 | + User user = tuple.getT1(); |
| 280 | + String emailTemplate = (String)tuple.getT2(); |
| 281 | + |
| 282 | + String token = generateNewRandomPwd(); |
| 283 | + Instant tokenExpiry = Instant.now().plus(12, ChronoUnit.HOURS); |
| 284 | + if (!emailCommunicationService.sendPasswordResetEmail(userEmail, token, emailTemplate)) { |
| 285 | + return Mono.empty(); |
| 286 | + } |
| 287 | + user.setPasswordResetToken(HashUtils.hash(token.getBytes())); |
| 288 | + user.setPasswordResetTokenExpiry(tokenExpiry); |
| 289 | + return repository.save(user).then(Mono.empty()); |
| 290 | + }); |
| 291 | + } |
| 292 | + |
| 293 | + @Override |
| 294 | + public Mono<Boolean> resetLostPassword(String userEmail, String token, String newPassword) { |
| 295 | + return findByName(userEmail) |
| 296 | + .flatMap(user -> { |
| 297 | + if (Instant.now().until(user.getPasswordResetTokenExpiry(), ChronoUnit.MINUTES) <= 0) { |
| 298 | + return ofError(BizError.INVALID_PARAMETER, "TOKEN_EXPIRED"); |
| 299 | + } |
| 300 | + |
| 301 | + if (!StringUtils.equals(HashUtils.hash(token.getBytes()), user.getPasswordResetToken())) { |
| 302 | + return ofError(BizError.INVALID_PARAMETER, "INVALID_TOKEN"); |
| 303 | + } |
| 304 | + |
| 305 | + user.setPassword(encryptionService.encryptPassword(newPassword)); |
| 306 | + user.setPasswordResetToken(StringUtils.EMPTY); |
| 307 | + user.setPasswordResetTokenExpiry(Instant.now()); |
| 308 | + return repository.save(user) |
| 309 | + .thenReturn(true); |
| 310 | + }); |
| 311 | + } |
| 312 | + |
265 | 313 | @SuppressWarnings("SpellCheckingInspection")
|
266 | 314 | @Nonnull
|
267 | 315 | private static String generateNewRandomPwd() {
|
|
0 commit comments