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 @@ -26,11 +26,6 @@ public ResponseEntity<SuccessResponse<?>> getAllUsers() {
return SuccessResponse.ok(userService.getAllUsers());
}

@PostMapping
public ResponseEntity<SuccessResponse<?>> createUser(@Valid @RequestBody UserCreateRequestDTO request) {
return SuccessResponse.ok(userService.createUser(request));
}

@PutMapping("/{id}") // TODO: 관리자용 updateUser와 분리 필요
public ResponseEntity<SuccessResponse<?>> updateUser(@PathVariable Long id, @Valid @RequestBody UserUpdateRequestDTO request) {
return SuccessResponse.ok(userService.updateUser(id, request));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package DGU_AI_LAB.admin_be.domain.users.dto.request;

import DGU_AI_LAB.admin_be.domain.users.entity.User;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotBlank;
Expand All @@ -17,5 +18,30 @@ public record UserRegisterRequestDTO(

@Schema(description = "사용자 이름", example = "이소은")
@NotBlank
String name
) {}
String name,

@Schema(description = "학과", example = "컴퓨터공학과")
@NotBlank
String department,

@Schema(description = "학번", example = "202312345")
@NotBlank
String studentId,

@Schema(description = "전화번호", example = "010-1234-5678")
@NotBlank
String phone
) {
/** 비밀번호는 서비스에서 암호화한 값을 넘겨서 처리 */
public User toEntity(String encodedPassword) {
return User.builder()
.email(email)
.password(encodedPassword)
.name(name)
.department(department)
.studentId(studentId)
.phone(phone)
// role, isActive는 엔티티의 @Builder.Default 로 기본값 사용
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ public record UserSummaryDTO(
Long userId,
String name,
String email,
Long ubuntuUid,
Long ubuntuGid,
String role,
Boolean isActive,
LocalDateTime createdAt
Expand All @@ -21,8 +19,6 @@ public static UserSummaryDTO fromEntity(User user) {
.userId(user.getUserId())
.name(user.getName())
.email(user.getEmail())
.ubuntuUid(user.getUbuntuUid())
.ubuntuGid(user.getUbuntuGroup() != null ? user.getUbuntuGroup().getUbuntuGid() : null)
.role(user.getRole().name())
.isActive(user.getIsActive())
.createdAt(user.getCreatedAt())
Expand Down
33 changes: 5 additions & 28 deletions src/main/java/DGU_AI_LAB/admin_be/domain/users/entity/User.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package DGU_AI_LAB.admin_be.domain.users.entity;

import DGU_AI_LAB.admin_be.domain.groups.entity.Group;
import DGU_AI_LAB.admin_be.global.common.BaseTimeEntity;
import jakarta.persistence.*;
import lombok.*;
Expand All @@ -15,17 +14,15 @@ public class User extends BaseTimeEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "user_id")
private Long userId;

@Column(name = "email", nullable = false, length = 100)
@Column(name = "email", nullable = false, length = 100, unique = true)
private String email;

@Column(name = "password", nullable = false, length = 255)
private String password;

@Column(name = "ubuntu_uid")
private Long ubuntuUid;

@Column(name = "name", nullable = false, length = 100)
private String name;

Expand All @@ -47,28 +44,8 @@ public class User extends BaseTimeEntity {
@Builder.Default
private Boolean isActive = true;

public void updateUserInfo(String password, Boolean isActive) {
this.password = password;
this.isActive = isActive;
}

public void updateUbuntuUid(Long ubuntuUid) {
this.ubuntuUid = ubuntuUid;
public void updateUserInfo(String encodedPassword, Boolean isActive) {
if (encodedPassword != null) this.password = encodedPassword;
if (isActive != null) this.isActive = isActive;
}

public void updateUbuntuGroup(Group group) {
this.ubuntuGroup = group;
}

public void updateUnixInfo(Long ubuntuUid, Group ubuntuGroup) {
this.ubuntuUid = ubuntuUid;
this.ubuntuGroup = ubuntuGroup;
}

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "ubuntu_gid")
private Group ubuntuGroup;



}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,4 @@
@Repository
public interface UserRepository extends JpaRepository<User,Long> {
Optional<User> findByEmail(String email);
@Query("SELECT MAX(u.ubuntuUid) FROM User u")
Optional<Long> findMaxUbuntuUid();

boolean existsByUbuntuUid(Long ubuntuUid);

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package DGU_AI_LAB.admin_be.domain.users.service;

import DGU_AI_LAB.admin_be.domain.groups.entity.Group;
import DGU_AI_LAB.admin_be.domain.groups.repository.GroupRepository;
import DGU_AI_LAB.admin_be.domain.users.dto.request.UserLoginRequestDTO;
import DGU_AI_LAB.admin_be.domain.users.dto.request.UserRegisterRequestDTO;
import DGU_AI_LAB.admin_be.domain.users.dto.response.UserTokenResponseDTO;
Expand All @@ -15,6 +17,7 @@
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.concurrent.TimeUnit;

Expand All @@ -24,36 +27,37 @@
public class UserLoginService {

private final UserRepository userRepository;
private final GroupRepository groupRepository;
private final PasswordEncoder passwordEncoder;
private final JwtProvider jwtProvider;
private final RedisTemplate<String, String> redisTemplate;

private final long REFRESH_TOKEN_EXPIRE_TIME = 60 * 60 * 24 * 7;

/** 회원가입 */
@Transactional
public void register(UserRegisterRequestDTO request) {
String redisKey = "VERIFIED:" + request.email();

if (!Boolean.TRUE.equals(redisTemplate.hasKey(redisKey))) {
throw new UnauthorizedException(ErrorCode.EMAIL_NOT_VERIFIED);
}

if (userRepository.findByEmail(request.email()).isPresent()) {
throw new BusinessException(ErrorCode.USER_ALREADY_EXISTS);
}

User user = User.builder()
.email(request.email())
.password(passwordEncoder.encode(request.password()))
.name(request.name())
.role(Role.USER)
.isActive(true)
.build();
String encoded = passwordEncoder.encode(request.password());
User user = request.toEntity(encoded);

userRepository.save(user);

redisTemplate.delete(redisKey);
log.info("회원가입 완료 VERIFIED:{} 키 삭제", request.email());
log.info("회원가입 완료, VERIFIED:{} 키 삭제", request.email());
}



/** 로그인 */
public UserTokenResponseDTO login(UserLoginRequestDTO request) {
User user = userRepository.findByEmail(request.email())
.orElseThrow(() -> new UnauthorizedException(ErrorCode.INVALID_LOGIN_INFO));
Expand All @@ -71,5 +75,6 @@ public UserTokenResponseDTO login(UserLoginRequestDTO request) {

return UserTokenResponseDTO.of(accessToken, refreshToken);
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -29,32 +29,6 @@ public class UserService {

private static final long UID_BASE = 10000; // TODO: 이부분 시스템에 맞추어서 수정하기

/**
* 유저 생성
*/
public UserResponseDTO createUser(UserCreateRequestDTO request) {
log.info("[createUser] name={}", request.name());

Long uid = getNextAvailableUid();
Long gid = uid; // 기본적으로 UID와 동일한 GID 사용

Group group = groupRepository.findById(gid)
.orElseThrow(() -> new EntityNotFoundException(ErrorCode.ENTITY_NOT_FOUND));

User user = request.toEntity();
user.updateUbuntuUid(uid);
user.updateUbuntuGroup(group);

User saved = userRepository.save(user);
log.info("[createUser] user created with userId={}", saved.getUserId());

return UserResponseDTO.fromEntity(saved);
}

private Long getNextAvailableUid() {
return userRepository.findMaxUbuntuUid().orElse(UID_BASE - 1) + 1;
}

/**
* 단일 유저 조회
*/
Expand Down