Skip to content

Commit

Permalink
Merge pull request #103 from Team-KeepGoing/feature-#102
Browse files Browse the repository at this point in the history
feature :: add check teacher approval logic
  • Loading branch information
priverg authored Oct 21, 2024
2 parents 91aa45b + 102e14d commit e8106e6
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Entity
@Getter
@Setter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Table(name = "users")
public class User {
Expand Down Expand Up @@ -58,6 +60,12 @@ public class User {
@Column
private LocalDateTime statusTime;

/*
교사 계정 승인 여부
*/
@Column(nullable = false)
private boolean approved;

@OneToMany(mappedBy = "teacher", fetch = FetchType.LAZY)
private List<Notice> notices = new ArrayList<>();

Expand All @@ -68,13 +76,15 @@ public static User registerUser(
String email,
String password,
String name,
boolean teacher
boolean teacher,
boolean approved
) {
User user = new User();
user.email = email;
user.password = password;
user.name = name;
user.teacher = teacher;
user.approved = approved;

return user;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public record SignupRequest(
String email,
String password,
String name,
boolean isTeacher
boolean isTeacher,
boolean isApproved
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,15 @@ public class UserDetailsImpl implements UserDetails {
@Getter
private final boolean teacher;

public UserDetailsImpl(Long id, String email, String name, String password, boolean teacher) {
private final boolean approved;

public UserDetailsImpl(Long id, String email, String name, String password, boolean teacher, boolean approved) {
this.id = id;
this.email = email;
this.name = name;
this.password = password;
this.teacher = teacher;
this.approved = approved;
}

public static UserDetailsImpl build(User user) {
Expand All @@ -40,7 +43,8 @@ public static UserDetailsImpl build(User user) {
user.getEmail(),
user.getName(),
user.getPassword(),
user.isTeacher()
user.isTeacher(),
user.isApproved()
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,31 @@ public Optional<User> getUserByEmail(String email){
}

/* 인증 및 JWT 토큰 생성 */
@Override
public JwtResponse authenticateAndGenerateJWT(String email, String password) {
Authentication authentication = authenticationManager.authenticate(
new UsernamePasswordAuthenticationToken(email, password));
SecurityContextHolder.getContext().setAuthentication(authentication);

String jwt = jwtUtils.generateJwtToken(authentication);
UserDetailsImpl userDetails = (UserDetailsImpl) authentication.getPrincipal();

checkTeacherApproval(userDetails);

String jwt = jwtUtils.generateJwtToken(authentication);
return JwtResponse.setJwtResponse(jwt, userDetails.getId(), userDetails.getEmail(), userDetails.getName(), userDetails.isTeacher());
}

private void checkTeacherApproval(UserDetailsImpl userDetails) {
if (userDetails.isTeacher()) {
User user = userRepository.findById(userDetails.getId())
.orElseThrow(() -> new UserException(UserError.USER_NOT_FOUND));

if (!user.isApproved()) {
throw new BusinessException(UserError.TEACHER_ACCOUNT_NOT_APPROVED);
}
}
}

private String getNameByAuthentication(Authentication authentication) {
return authentication.getName();
}
Expand All @@ -131,7 +145,8 @@ private User createUser(SignupRequest signupRequest) {
signupRequest.email(),
encoder.encode(signupRequest.password()),
signupRequest.name(),
signupRequest.isTeacher()
signupRequest.isTeacher(),
signupRequest.isApproved()
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
@RequiredArgsConstructor
public enum UserError implements ErrorProperty {
USER_NOT_TEACHER(HttpStatus.BAD_REQUEST, "선생님이 아닙니다."),
USER_NOT_FOUND(HttpStatus.BAD_REQUEST, "해당 학생을 찾을 수 없습니다");
USER_NOT_FOUND(HttpStatus.BAD_REQUEST, "해당 학생을 찾을 수 없습니다"),
TEACHER_ACCOUNT_NOT_APPROVED(HttpStatus.UNAUTHORIZED, "교사 계정이 승인되지 않았습니다.");

private final HttpStatus status;
private final String message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
public class UserException extends BusinessException {
private static final UserException USER_NOT_TEACHER = new UserException(UserError.USER_NOT_TEACHER);
private static final UserException USER_NOT_FOUND = new UserException(UserError.USER_NOT_FOUND);
private static final UserException TEACHER_ACCOUNT_NOT_APPROVED = new UserException(UserError.TEACHER_ACCOUNT_NOT_APPROVED);

public UserException(ErrorProperty error) {
super(error);
Expand All @@ -15,4 +16,5 @@ public static UserException userNotTeacher() {
return USER_NOT_TEACHER;
}
public static UserException userNotFound(){ return USER_NOT_FOUND; }
public static UserException teacherAccountNotApproved() {return TEACHER_ACCOUNT_NOT_APPROVED; }
}

0 comments on commit e8106e6

Please sign in to comment.