Skip to content

Commit

Permalink
YEL-151 [feat] 어드민 로그인 간소화
Browse files Browse the repository at this point in the history
YEL-151 [feat] 어드민 로그인 간소화
  • Loading branch information
euije authored Sep 2, 2023
2 parents e46ab60 + d4d7fd7 commit b7972d9
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

import static com.yello.server.global.common.SuccessCode.DELETE_COOLDOWN_ADMIN_SUCCESS;
import static com.yello.server.global.common.SuccessCode.DELETE_USER_ADMIN_SUCCESS;
import static com.yello.server.global.common.SuccessCode.LOGIN_USER_ADMIN_SUCCESS;
import static com.yello.server.global.common.SuccessCode.READ_COOLDOWN_ADMIN_SUCCESS;
import static com.yello.server.global.common.SuccessCode.READ_USER_ADMIN_SUCCESS;

import com.yello.server.domain.admin.dto.request.AdminLoginRequest;
import com.yello.server.domain.admin.dto.response.AdminCooldownResponse;
import com.yello.server.domain.admin.dto.response.AdminLoginResponse;
import com.yello.server.domain.admin.dto.response.AdminUserResponse;
import com.yello.server.domain.admin.service.AdminService;
import com.yello.server.domain.user.entity.User;
Expand All @@ -17,6 +20,8 @@
import lombok.val;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
Expand All @@ -28,6 +33,12 @@ public class AdminController {

private final AdminService adminService;

@PostMapping("/login")
public BaseResponse<AdminLoginResponse> postAdminLogin(@RequestBody AdminLoginRequest request) {
val data = adminService.login(request);
return BaseResponse.success(LOGIN_USER_ADMIN_SUCCESS, data);
}

@GetMapping("/user")
public BaseResponse<AdminUserResponse> getUserAdmin(@AccessTokenUser User user, @RequestParam Integer page,
@Nullable @RequestParam String yelloId) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.yello.server.domain.admin.dto.request;

import lombok.Builder;

@Builder
public record AdminLoginRequest(
String password
) {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.yello.server.domain.admin.dto.response;

import lombok.Builder;

@Builder
public record AdminLoginResponse(
String accessToken
) {

public static AdminLoginResponse of(String accessToken) {
return AdminLoginResponse.builder()
.accessToken(accessToken)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
package com.yello.server.domain.admin.service;

import static com.yello.server.global.common.ErrorCode.USER_ADMIN_NOT_FOUND_EXCEPTION;

import com.yello.server.domain.admin.dto.request.AdminLoginRequest;
import com.yello.server.domain.admin.dto.response.AdminCooldownContentVO;
import com.yello.server.domain.admin.dto.response.AdminCooldownResponse;
import com.yello.server.domain.admin.dto.response.AdminLoginResponse;
import com.yello.server.domain.admin.dto.response.AdminUserContentVO;
import com.yello.server.domain.admin.dto.response.AdminUserResponse;
import com.yello.server.domain.admin.exception.UserAdminNotFoundException;
import com.yello.server.domain.admin.repository.UserAdminRepository;
import com.yello.server.domain.authorization.service.TokenProvider;
import com.yello.server.domain.cooldown.entity.Cooldown;
import com.yello.server.domain.cooldown.repository.CooldownRepository;
import com.yello.server.domain.user.entity.User;
import com.yello.server.domain.user.repository.UserRepository;
import com.yello.server.domain.user.service.UserManager;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
Expand All @@ -21,9 +29,28 @@
public class AdminService {

private final UserRepository userRepository;
private final UserManager userManager;
private final TokenProvider tokenProvider;
private final CooldownRepository cooldownRepository;
private final UserAdminRepository userAdminRepository;

public AdminLoginResponse login(AdminLoginRequest request) {
AtomicReference<String> accessToken = new AtomicReference<>();

userManager.getOfficialUsers()
.forEach((user) -> {
if (user.getYelloId().equals(request.password())) {
accessToken.set(tokenProvider.createAccessToken(user.getId(), user.getUuid()));
}
});

if (accessToken.get() == null || accessToken.get().isEmpty()) {
throw new UserAdminNotFoundException(USER_ADMIN_NOT_FOUND_EXCEPTION);
}

return AdminLoginResponse.of(accessToken.get());
}

public AdminUserResponse findUser(Long adminId, Pageable page) {
// exception
final User admin = userRepository.getById(adminId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ protected void doFilterInternal(
if (requestPath.equals("/")
|| requestPath.startsWith("/docs")
|| requestPath.startsWith("/actuator") || requestPath.startsWith("/prometheus")
|| requestPath.startsWith("/api/v1/admin/login")
|| (requestPath.startsWith("/api/v1/auth")
&& !requestPath.startsWith("/api/v1/auth/token/issue"))) {
filterChain.doFilter(request, response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
if (requestPath.equals("/")
|| requestPath.startsWith("/docs")
|| requestPath.startsWith("/actuator") || requestPath.startsWith("/prometheus")
|| requestPath.startsWith("/api/v1/admin/login")
|| requestPath.startsWith("/api/v1/auth")) {
filterChain.doFilter(request, response);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public UserSubscribeNeededResponse getUserSubscribe(User user, LocalDateTime tim
final Optional<Purchase> mostRecentPurchase =
purchaseRepository.findTopByUserAndProductTypeOrderByCreatedAtDesc(
user, ProductType.YELLO_PLUS);
final Boolean isSubscribeNeeded = user.getSubscribe()==Subscribe.CANCELED
final Boolean isSubscribeNeeded = user.getSubscribe() == Subscribe.CANCELED
&& mostRecentPurchase.isPresent()
&& Duration.between(mostRecentPurchase.get().getCreatedAt(), time).getSeconds()
< 1 * 24 * 60 * 60;
Expand All @@ -94,7 +94,7 @@ public void verifyAppleSubscriptionTransaction(Long userId,

purchaseManager.handleAppleTransactionError(verifyReceiptResponse, request.transactionId());

if (user.getSubscribe()==Subscribe.ACTIVE) {
if (user.getSubscribe() == Subscribe.ACTIVE) {
throw new SubscriptionConflictException(SUBSCRIBE_ACTIVE_EXCEPTION);
}

Expand Down Expand Up @@ -141,7 +141,7 @@ public GoogleSubscriptionGetResponse verifyGoogleSubscriptionTransaction(Long us
User user = userRepository.getById(userId);

// exception
if (user.getSubscribe()!=Subscribe.NORMAL) {
if (user.getSubscribe() != Subscribe.NORMAL) {
throw new PurchaseConflictException(GOOGLE_SUBSCRIPTIONS_FORBIDDEN_EXCEPTION);
}

Expand Down Expand Up @@ -187,7 +187,7 @@ public GoogleSubscriptionGetResponse verifyGoogleSubscriptionTransaction(Long us
GOOGLE_SUBSCRIPTION_TRANSACTION_EXPIRED_EXCEPTION);
}
case ConstantUtil.GOOGLE_PURCHASE_SUBSCRIPTION_CANCELED -> {
if (user.getSubscribe()==Subscribe.CANCELED) {
if (user.getSubscribe() == Subscribe.CANCELED) {
throw new GoogleBadRequestException(
GOOGLE_SUBSCRIPTION_DUPLICATED_CANCEL_EXCEPTION);
} else {
Expand Down Expand Up @@ -239,8 +239,9 @@ public GoogleTicketGetResponse verifyGoogleTicketTransaction(Long userId,
if (!inAppResponse.getStatusCode().is2xxSuccessful()) {
throw new GoogleTokenServerErrorException(GOOGLE_TOKEN_SERVER_EXCEPTION);
}
System.out.println("inAppResponse = " + inAppResponse);

if (inAppResponse.getBody().purchaseState()==0) {
if (inAppResponse.getBody().purchaseState() == 0) {
purchaseRepository.findByTransactionId(inAppResponse.getBody().orderId())
.ifPresent(action -> {
throw new PurchaseConflictException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import com.yello.server.domain.user.entity.Gender;
import com.yello.server.domain.user.entity.User;
import java.util.List;

public interface UserManager {

User getOfficialUser(Gender gender);

List<User> getOfficialUsers();
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import com.yello.server.domain.user.entity.Subscribe;
import com.yello.server.domain.user.entity.User;
import com.yello.server.domain.user.repository.UserRepository;
import java.util.ArrayList;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

Expand All @@ -28,6 +30,15 @@ public User getOfficialUser(Gender gender) {
);
}

@Override
public List<User> getOfficialUsers() {
List<User> users = new ArrayList<>();
users.add(getOfficialUser(Gender.FEMALE));
users.add(getOfficialUser(Gender.MALE));

return users;
}

private User makeOfficialUser(String name, String yelloId, Gender gender) {
return User.builder()
.recommendCount(0L)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ public enum SuccessCode {
RE_ISSUE_TOKEN_AUTH_SUCCESS(CREATED, "토큰 재발급에 성공했습니다."),
PURCHASE_SUBSCRIPTION_VERIFY_SUCCESS(CREATED, "애플 결제 검증 및 반영에 성공하였습니다."),
GOOGLE_PURCHASE_SUBSCRIPTION_VERIFY_SUCCESS(CREATED, "구글 구독 결제 검증 및 반영에 성공하였습니다."),
GOOGLE_PURCHASE_INAPP_VERIFY_SUCCESS(CREATED, "구글 인앱 결제 검증 및 반영에 성공하였습니다.");
GOOGLE_PURCHASE_INAPP_VERIFY_SUCCESS(CREATED, "구글 인앱 결제 검증 및 반영에 성공하였습니다."),
LOGIN_USER_ADMIN_SUCCESS(CREATED, "어드민 로그인에 성공하였습니다.");


private final HttpStatus httpStatus;
private final String message;
Expand Down
11 changes: 11 additions & 0 deletions src/test/java/com/yello/server/domain/user/FakeUserManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import com.yello.server.domain.user.entity.User;
import com.yello.server.domain.user.repository.UserRepository;
import com.yello.server.domain.user.service.UserManager;
import java.util.ArrayList;
import java.util.List;

public class FakeUserManager implements UserManager {

Expand All @@ -29,6 +31,15 @@ public User getOfficialUser(Gender gender) {
);
}

@Override
public List<User> getOfficialUsers() {
List<User> users = new ArrayList<>();
users.add(getOfficialUser(Gender.FEMALE));
users.add(getOfficialUser(Gender.MALE));

return users;
}

private User makeOfficialUser(String name, String yelloId, Gender gender) {
return User.builder()
.recommendCount(0L)
Expand Down

0 comments on commit b7972d9

Please sign in to comment.