Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;

import in.koreatech.koin.domain.user.dto.AuthResponse;
import in.koreatech.koin.domain.user.dto.EmailCheckExistsRequest;
import in.koreatech.koin.domain.user.dto.NicknameCheckExistsRequest;
import in.koreatech.koin.domain.user.dto.StudentResponse;
Expand Down Expand Up @@ -154,4 +155,19 @@ ResponseEntity<Void> checkDuplicationOfNickname(
@ModelAttribute("nickname")
@Valid NicknameCheckExistsRequest request
);

@ApiResponses(
value = {
@ApiResponse(responseCode = "200"),
@ApiResponse(responseCode = "400", content = @Content(schema = @Schema(hidden = true))),
@ApiResponse(responseCode = "401", content = @Content(schema = @Schema(hidden = true))),
@ApiResponse(responseCode = "403", content = @Content(schema = @Schema(hidden = true))),
@ApiResponse(responseCode = "404", content = @Content(schema = @Schema(hidden = true)))
}
)
@Operation(summary = "사용자 권한 조회")
@GetMapping("/user/auth")
ResponseEntity<AuthResponse> getAuth(
@Auth(permit = {STUDENT, OWNER, COOP}) Long userId
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import in.koreatech.koin.domain.user.dto.AuthResponse;
import in.koreatech.koin.domain.user.dto.EmailCheckExistsRequest;
import in.koreatech.koin.domain.user.dto.NicknameCheckExistsRequest;
import in.koreatech.koin.domain.user.dto.StudentResponse;
Expand Down Expand Up @@ -104,4 +105,12 @@ public ResponseEntity<Void> checkDuplicationOfNickname(
userService.checkUserNickname(request);
return ResponseEntity.ok().build();
}

@GetMapping("/user/auth")
public ResponseEntity<AuthResponse> getAuth(
@Auth(permit = {STUDENT, OWNER, COOP}) Long userId
) {
AuthResponse authResponse = userService.getAuth(userId);
return ResponseEntity.ok().body(authResponse);
}
}
18 changes: 18 additions & 0 deletions src/main/java/in/koreatech/koin/domain/user/dto/AuthResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package in.koreatech.koin.domain.user.dto;

import com.fasterxml.jackson.databind.PropertyNamingStrategies.SnakeCaseStrategy;
import com.fasterxml.jackson.databind.annotation.JsonNaming;

import in.koreatech.koin.domain.user.model.User;
import io.swagger.v3.oas.annotations.media.Schema;

@JsonNaming(SnakeCaseStrategy.class)
public record AuthResponse(
@Schema(description = "사용자 권한 타입", example = "STUDENT")
String userType
) {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C

개행 삭제해주세요!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

클래스 정의부와 몸체의 첫 내용 사이에는 항상 개행을 넣어주고 있었는데, 컨벤션 상 개행을 넣지 않아야 하는 건가요? 크게 중요한 부분은 아니지만 현 프로젝트 내에서는 혼용하고 있기 때문에 통일하면 좋을 것 같네요

ex)

@JsonNaming(value = SnakeCaseStrategy.class)
public record DeptResponse(String deptNum, String name) {

    public static DeptResponse from(String findNumber, Dept dept) {
        return new DeptResponse(findNumber, dept.getName());
    }
}

public static AuthResponse from(User user) {
return new AuthResponse(user.getUserType().getValue());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import in.koreatech.koin.domain.user.dto.AuthResponse;
import in.koreatech.koin.domain.user.dto.EmailCheckExistsRequest;
import in.koreatech.koin.domain.user.dto.NicknameCheckExistsRequest;
import in.koreatech.koin.domain.user.dto.UserLoginRequest;
Expand Down Expand Up @@ -92,4 +93,9 @@ public void checkUserNickname(NicknameCheckExistsRequest request) {
throw DuplicationEmailException.withDetail("nickname: " + request.nickname());
});
}

public AuthResponse getAuth(Long userId) {
User user = userRepository.getById(userId);
return AuthResponse.from(user);
}
}
45 changes: 45 additions & 0 deletions src/test/java/in/koreatech/koin/acceptance/UserApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -658,4 +658,49 @@ void checkDuplicationOfNicknameBadRequest() {
.statusCode(HttpStatus.BAD_REQUEST.value())
.extract();
}

@Test
@DisplayName("로그인된 사용자의 권한을 조회한다.")
void getAuth() {
Student student = Student.builder()
.studentNumber("2019136135")
.anonymousNickname("익명")
.department("컴퓨터공학부")
.userIdentity(UserIdentity.UNDERGRADUATE)
.isGraduated(false)
.user(
User.builder()
.password("1234")
.nickname("주노")
.name("최준호")
.phoneNumber("010-1234-5678")
.userType(STUDENT)
.gender(UserGender.MAN)
.email("test@koreatech.ac.kr")
.isAuthed(true)
.isDeleted(false)
.build()
)
.build();

studentRepository.save(student);
String token = jwtProvider.createToken(student.getUser());

ExtractableResponse<Response> response = RestAssured
.given()
.header("Authorization", "Bearer " + token)
.when()
.get("/user/auth")
.then()
.statusCode(HttpStatus.OK.value())
.extract();

User user = student.getUser();

assertSoftly(
softly -> {
softly.assertThat(response.body().jsonPath().getString("user_type")).isEqualTo(user.getUserType().getValue());
}
);
}
}