-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathUserService.java
More file actions
96 lines (80 loc) · 3.51 KB
/
UserService.java
File metadata and controls
96 lines (80 loc) · 3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package com.loopers.application.service;
import com.loopers.application.PasswordUpdateUseCase;
import com.loopers.application.RegisterUseCase;
import com.loopers.application.UserQueryUseCase;
import com.loopers.domain.model.*;
import com.loopers.domain.repository.UserRepository;
import com.loopers.domain.service.PasswordEncoder;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDate;
import java.time.LocalDateTime;
@Service
@Transactional(readOnly = true)
public class UserService implements RegisterUseCase, PasswordUpdateUseCase, UserQueryUseCase {
private final UserRepository userRepository;
private final PasswordEncoder passwordEncoder;
public UserService(UserRepository userRepository, PasswordEncoder passwordEncoder) {
this.userRepository = userRepository;
this.passwordEncoder = passwordEncoder;
}
@Override
@Transactional
public void register(String loginId, String name, String rawPassword, LocalDate birthday, String email) {
UserId userId = UserId.of(loginId);
UserName userName = UserName.of(name);
Birthday birth = Birthday.of(birthday);
Email userEmail = Email.of(email);
Password password = Password.of(rawPassword, birthday);
String encodedPassword = passwordEncoder.encrypt(password.getValue());
try {
User user = User.register(
userId, userName, encodedPassword, birth,
userEmail, WrongPasswordCount.init(), LocalDateTime.now()
);
userRepository.save(user);
} catch (DataIntegrityViolationException ex) {
throw new IllegalArgumentException("이미 사용중인 ID 입니다.", ex);
}
}
@Override
@Transactional
public void updatePassword(UserId userId, String currentRawPassword, String newRawPassword) {
User user = findUser(userId);
LocalDate birthday = user.getBirth().getValue();
Password newPassword = Password.of(newRawPassword, birthday);
if (!passwordEncoder.matches(currentRawPassword, user.getEncodedPassword())) {
throw new IllegalArgumentException("현재 비밀번호가 일치하지 않습니다.");
}
if (passwordEncoder.matches(newPassword.getValue(), user.getEncodedPassword())) {
throw new IllegalArgumentException("현재 비밀번호는 사용할 수 없습니다.");
}
String encodedNewPassword = passwordEncoder.encrypt(newPassword.getValue());
User updatedUser = user.changePassword(encodedNewPassword);
userRepository.save(updatedUser);
}
@Override
public UserInfoResponse getUserInfo(UserId userId) {
User user = findUser(userId);
return new UserInfoResponse(
user.getUserId().getValue(),
maskName(user.getUserName().getValue()),
user.getBirth().getValue(),
user.getEmail().getValue()
);
}
private User findUser(UserId userId) {
return userRepository.findById(userId)
.orElseThrow(() -> new IllegalArgumentException("사용자를 찾을 수 없습니다."));
}
private String maskName(String name) {
if (name == null || name.isEmpty()) {
return name;
}
if (name.length() == 1) {
return "*";
}
return name.substring(0, name.length() - 1) + "*";
}
}