Skip to content

Commit ead5512

Browse files
authored
[feat] admin 회원가입 기능 추가
1 parent 4c4c258 commit ead5512

8 files changed

Lines changed: 88 additions & 18 deletions

File tree

backend/src/main/java/com/back/api/auth/dto/request/SignupRequest.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
import java.time.LocalDate;
44

5+
import com.back.domain.user.entity.UserRole;
6+
57
import io.swagger.v3.oas.annotations.media.Schema;
68
import jakarta.validation.constraints.Email;
79
import jakarta.validation.constraints.NotBlank;
10+
import jakarta.validation.constraints.NotNull;
811
import jakarta.validation.constraints.Pattern;
912
import jakarta.validation.constraints.Size;
1013

@@ -35,6 +38,13 @@ public record SignupRequest(
3538
@Size(min = 3, max = 10, message = "닉네임은 3~10 글자여야 합니다.")
3639
String nickname,
3740

41+
@NotNull(message = "사용자 권한 설정은 필수입니다.")
42+
@Schema(
43+
description = "사용자 권한",
44+
example = "NORMAL | ADMIN"
45+
)
46+
UserRole role,
47+
3848
@Schema(
3949
description = "생년월일 중 연도",
4050
example = "2002"
@@ -57,7 +67,13 @@ public record SignupRequest(
5767
)
5868
@NotBlank(message = "생년월일은 필수입니다.")
5969
@Pattern(regexp = "\\d{1,2}", message = "일은 숫자여야합니다.")
60-
String day
70+
String day,
71+
72+
@Schema(
73+
description = "사업자 등록 번호",
74+
example = "000-00-00000"
75+
)
76+
String registrationNumber
6177
) {
6278
public LocalDate toBirthDate() {
6379
return LocalDate.of(

backend/src/main/java/com/back/api/auth/service/AuthService.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@
1515
import com.back.api.auth.dto.response.TokenResponse;
1616
import com.back.api.auth.dto.response.UserResponse;
1717
import com.back.api.auth.store.AuthStore;
18+
import com.back.api.store.service.StoreService;
1819
import com.back.domain.auth.entity.ActiveSession;
1920
import com.back.domain.auth.entity.RefreshToken;
2021
import com.back.domain.auth.repository.ActiveSessionRepository;
2122
import com.back.domain.auth.repository.RefreshTokenRepository;
23+
import com.back.domain.store.entity.Store;
2224
import com.back.domain.user.entity.User;
2325
import com.back.domain.user.entity.UserActiveStatus;
24-
import com.back.domain.user.entity.UserRole;
2526
import com.back.domain.user.repository.UserRepository;
2627
import com.back.global.error.code.AuthErrorCode;
2728
import com.back.global.error.exception.ErrorException;
@@ -44,6 +45,7 @@ public class AuthService {
4445
private final ActiveSessionCache activeSessionCache;
4546
private final ActiveSessionRepository activeSessionRepository;
4647
private final AuthStore authStore;
48+
private final StoreService storeService;
4749

4850
@Transactional
4951
public AuthResponse signup(SignupRequest request) {
@@ -58,14 +60,25 @@ public AuthResponse signup(SignupRequest request) {
5860
String encoded = passwordEncoder.encode(request.password());
5961
LocalDate birthDate = request.toBirthDate();
6062

63+
Store store = null;
64+
65+
if (
66+
request.registrationNumber() != null
67+
&& !request.registrationNumber().isBlank()
68+
&& !request.registrationNumber().isEmpty()
69+
) {
70+
store = storeService.getStoreByRegistrationNumber(request.registrationNumber());
71+
}
72+
6173
User user = User.builder()
6274
.email(request.email())
6375
.password(encoded)
6476
.fullName(request.fullName())
6577
.nickname(request.nickname())
66-
.role(UserRole.NORMAL)
78+
.role(request.role())
6779
.activeStatus(UserActiveStatus.ACTIVE)
6880
.birthDate(birthDate)
81+
.store(store)
6982
.build();
7083

7184
User savedUser = userRepository.save(user);

backend/src/main/java/com/back/api/store/service/StoreService.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,10 @@ public Store getStoreById(long storeId) {
2222
() -> new ErrorException(StoreErrorCode.NOT_FOUND)
2323
);
2424
}
25+
26+
public Store getStoreByRegistrationNumber(String number) {
27+
return storeRepository.findByRegistrationNumber(number).orElseThrow(
28+
() -> new ErrorException(StoreErrorCode.NOT_FOUND)
29+
);
30+
}
2531
}

backend/src/main/java/com/back/api/user/controller/UserController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
@RequestMapping("/api/v1/users")
2222
@RequiredArgsConstructor
2323
@Validated
24-
@PreAuthorize("hasRole('NORMAL')")
24+
@PreAuthorize("hasAnyRole('NORMAL', 'ADMIN')")
2525
public class UserController implements UserApi {
2626

2727
private final UserService userService;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package com.back.domain.store.repository;
22

3+
import java.util.Optional;
4+
35
import org.springframework.data.jpa.repository.JpaRepository;
46

57
import com.back.domain.store.entity.Store;
68

79
public interface StoreRepository extends JpaRepository<Store, Long> {
10+
Optional<Store> findByRegistrationNumber(String number);
811
}

backend/src/main/java/com/back/global/config/SecurityConfig.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ SecurityFilterChain filterChain(HttpSecurity http, RequestIdFilter requestIdFilt
8383
.requestMatchers("/.well-known/**").permitAll()
8484
.requestMatchers("/api/v1/auth/signup").permitAll()
8585
.requestMatchers("/api/v1/auth/login").permitAll()
86-
.requestMatchers("/api/v1/admin/auth/**").permitAll()
8786
.requestMatchers("/api/v1/events/**").permitAll()
8887
.requestMatchers("/ws/**").permitAll() // WebSocket 핸드셰이크 허용
8988
.requestMatchers("/api/v1/admin/**").hasRole("ADMIN")

backend/src/test/java/com/back/api/auth/controller/AuthControllerTest.java

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,18 @@ class SignupTest {
8787
@DisplayName("Success Sign up")
8888
void signup_success() throws Exception {
8989

90-
String requestJson = mapper.writeValueAsString(Map.of(
91-
"email", user.getEmail(),
92-
"password", testUser.rawPassword(),
93-
"fullName", user.getFullName(),
94-
"nickname", user.getNickname(),
95-
"year", "2002",
96-
"month", "2",
97-
"day", "11"
98-
));
90+
Map<String, Object> body = new java.util.HashMap<>();
91+
body.put("email", user.getEmail());
92+
body.put("password", testUser.rawPassword());
93+
body.put("fullName", user.getFullName());
94+
body.put("nickname", user.getNickname());
95+
body.put("role", UserRole.NORMAL.name());
96+
body.put("year", "2002");
97+
body.put("month", "2");
98+
body.put("day", "11");
99+
body.put("registrationNumber", null);
100+
101+
String requestJson = mapper.writeValueAsString(body);
99102

100103
ResultActions actions = mvc
101104
.perform(
@@ -123,7 +126,8 @@ void signup_failed_by_missing_params() throws Exception {
123126

124127
String requestJson = mapper.writeValueAsString(Map.of(
125128
"email", user.getEmail(),
126-
"password", user.getPassword(),
129+
"password", testUser.rawPassword(),
130+
"role", UserRole.NORMAL.name(),
127131
"year", "2002",
128132
"month", "2",
129133
"day", "11"
@@ -152,6 +156,7 @@ void signup_failed_by_existing_email() throws Exception {
152156
"email", existedUser.user().getEmail(),
153157
"password", existedUser.rawPassword(),
154158
"fullName", user.getFullName(),
159+
"role", UserRole.NORMAL.name(),
155160
"nickname", "A" + existedUser.user().getNickname(),
156161
"year", "2002",
157162
"month", "2",
@@ -185,6 +190,7 @@ void signup_failed_by_existing_nickname() throws Exception {
185190
"password", existedUser.rawPassword(),
186191
"fullName", user.getFullName(),
187192
"nickname", existedUser.user().getNickname(),
193+
"role", UserRole.NORMAL.name(),
188194
"year", "2002",
189195
"month", "2",
190196
"day", "11"

backend/src/test/rest/auth.http

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,29 @@
1-
### 회원가입
1+
### 상점 관리자 회원가입
2+
POST {{BASE_URL}}/api/v1/auth/signup
3+
Content-Type: application/json
4+
5+
{
6+
"email": "admin1234@test.com",
7+
"password": "qwer1234",
8+
"fullName": "Admin User",
9+
"nickname": "Admin1234",
10+
"role": "ADMIN",
11+
"year": "2002",
12+
"month": "2",
13+
"day": "11",
14+
"registrationNumber": "123-45-67890"
15+
}
16+
17+
### 상점 관리자 로그인
18+
POST {{BASE_URL}}/api/v1/auth/login
19+
Content-Type: application/json
20+
21+
{
22+
"email": "admin1234@test.com",
23+
"password": "qwer1234"
24+
}
25+
26+
### 일반 사용자 회원가입
227
POST {{BASE_URL}}/api/v1/auth/signup
328
Content-Type: application/json
429

@@ -7,12 +32,14 @@ Content-Type: application/json
732
"password": "qwer1234",
833
"fullName": "Test User",
934
"nickname": "테스트유저",
35+
"role": "NORMAL",
1036
"year": "2002",
1137
"month": "2",
12-
"day": "11"
38+
"day": "11",
39+
"registrationNumber": ""
1340
}
1441

15-
### 로그인
42+
### 일반 사용자 로그인
1643
POST {{BASE_URL}}/api/v1/auth/login
1744
Content-Type: application/json
1845

0 commit comments

Comments
 (0)