Skip to content

Commit

Permalink
Merge pull request #51 from Team-MindWay/50-signup-teacher-authority-…
Browse files Browse the repository at this point in the history
…putting

🔀 :: 회원가입시에 학생과 선생님 구별하여 권한 부여하기
  • Loading branch information
Umjiseung authored Apr 8, 2024
2 parents 18a7674 + 6bb5da8 commit 9aeb43e
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import com.mindway.server.v2.global.exception.ErrorCode;
import com.mindway.server.v2.global.exception.MindWayException;

public class MemberNotFoundException extends MindWayException {
public class UserNotFoundException extends MindWayException {

public MemberNotFoundException() {
public UserNotFoundException() {
super(ErrorCode.MEMBER_NOT_FOUND);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.mindway.server.v2.domain.auth.RefreshToken;
import com.mindway.server.v2.domain.auth.exception.ExpiredRefreshTokenException;
import com.mindway.server.v2.domain.auth.exception.MemberNotFoundException;
import com.mindway.server.v2.domain.auth.exception.UserNotFoundException;
import com.mindway.server.v2.domain.auth.presentation.dto.response.TokenResponse;
import com.mindway.server.v2.domain.auth.repository.RefreshRepository;
import com.mindway.server.v2.domain.auth.service.ReissueTokenService;
Expand All @@ -27,7 +27,7 @@ public TokenResponse execute(String refreshToken) {
.orElseThrow(ExpiredRefreshTokenException::new);

User user = userRepository.findById(refreshEntity.getMemberId())
.orElseThrow(MemberNotFoundException::new);
.orElseThrow(UserNotFoundException::new);

refreshRepository.deleteById(refreshEntity.getRefreshToken());

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.mindway.server.v2.domain.auth.service.impl;

import com.mindway.server.v2.domain.auth.RefreshToken;
import com.mindway.server.v2.domain.auth.exception.UserNotFoundException;
import com.mindway.server.v2.domain.auth.presentation.dto.request.SignInRequest;
import com.mindway.server.v2.domain.auth.presentation.dto.response.TokenResponse;
import com.mindway.server.v2.domain.auth.repository.RefreshRepository;
Expand All @@ -20,6 +21,7 @@
import org.springframework.beans.factory.annotation.Value;

import java.io.IOException;
import java.util.Objects;
import java.util.UUID;

@RequiredArgsConstructor
Expand Down Expand Up @@ -52,6 +54,9 @@ public TokenResponse execute(SignInRequest signInRequest) throws GAuthException{
User user = userRepository.findByEmail(userInfo.getEmail())
.orElseGet(() -> saveUser(userInfo));

if (user == null)
throw new UserNotFoundException();

TokenResponse tokenResponse = jwtProvider.generateTokenDto(user.getId());

saveRefreshToken(tokenResponse, user);
Expand All @@ -68,12 +73,20 @@ public TokenResponse execute(SignInRequest signInRequest) throws GAuthException{
}

private User saveUser(GAuthUserInfo gAuthUserInfo) {
if (Objects.equals(gAuthUserInfo.getRole(), "ROLE_STUDENT")) {
return saveStudent(gAuthUserInfo);
} else if (Objects.equals(gAuthUserInfo.getRole(), "ROLE_TEACHER")) {
return saveTeacher(gAuthUserInfo);
}
return null;
}

private User saveStudent(GAuthUserInfo gAuthUserInfo) {
User user = User.builder()
.id(UUID.randomUUID())
.email(gAuthUserInfo.getEmail())
.name(gAuthUserInfo.getName())
.studentNum(new StudentNum(gAuthUserInfo.getGrade(), gAuthUserInfo.getClassNum(), gAuthUserInfo.getNum()))
.gauth_role(gAuthUserInfo.getRole())
.authority(Authority.ROLE_STUDENT)
.build();

Expand All @@ -82,6 +95,20 @@ private User saveUser(GAuthUserInfo gAuthUserInfo) {
return user;
}

private User saveTeacher(GAuthUserInfo gAuthUserInfo) {
User teacher = User.builder()
.id(UUID.randomUUID())
.email(gAuthUserInfo.getEmail())
.name(gAuthUserInfo.getName())
.studentNum(new StudentNum(gAuthUserInfo.getGrade(), gAuthUserInfo.getClassNum(), gAuthUserInfo.getNum()))
.authority(Authority.ROLE_TEACHER)
.build();

userRepository.save(teacher);

return teacher;
}

private void saveRefreshToken(TokenResponse tokenResponse, User user) {
RefreshToken refreshToken = RefreshToken.builder()
.refreshToken(tokenResponse.getRefreshToken())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ public class User {
@Embedded
private StudentNum studentNum;

private String gauth_role;

@Enumerated(EnumType.STRING)
private Authority authority;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.mindway.server.v2.domain.user.util;

import com.mindway.server.v2.domain.auth.exception.MemberNotFoundException;
import com.mindway.server.v2.domain.auth.exception.UserNotFoundException;
import com.mindway.server.v2.domain.user.entity.User;
import com.mindway.server.v2.domain.user.repository.UserRepository;
import lombok.RequiredArgsConstructor;
Expand All @@ -16,6 +16,6 @@ public class UserUtil {
public User getCurrentUser() {
String email = SecurityContextHolder.getContext().getAuthentication().getName();
return userRepository.findByEmail(email)
.orElseThrow(MemberNotFoundException::new);
.orElseThrow(UserNotFoundException::new);
}
}

0 comments on commit 9aeb43e

Please sign in to comment.