- Entity: User, Role, RoleUrlPattern, UserStatus enum - Repository: UserRepository, RoleRepository (fetch join 쿼리) - Auth: GoogleTokenVerifier, JwtTokenProvider, JwtAuthenticationFilter - API: POST /api/auth/google, GET /api/auth/me, POST /api/auth/logout - DTO: AuthResponse, UserResponse, RoleResponse, GoogleLoginRequest - SecurityConfig: JWT 필터 등록, CORS 설정, 공개 엔드포인트 정의 - 초기 데이터: roles + role_url_patterns 시드 (data.sql) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
104 lines
2.3 KiB
Java
104 lines
2.3 KiB
Java
package com.gcsc.guide.entity;
|
|
|
|
import jakarta.persistence.*;
|
|
import lombok.Getter;
|
|
import lombok.NoArgsConstructor;
|
|
|
|
import java.time.LocalDateTime;
|
|
import java.util.HashSet;
|
|
import java.util.Set;
|
|
|
|
@Entity
|
|
@Table(name = "users")
|
|
@Getter
|
|
@NoArgsConstructor
|
|
public class User {
|
|
|
|
@Id
|
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
|
private Long id;
|
|
|
|
@Column(nullable = false, unique = true)
|
|
private String email;
|
|
|
|
@Column(nullable = false, length = 100)
|
|
private String name;
|
|
|
|
@Column(name = "avatar_url", length = 500)
|
|
private String avatarUrl;
|
|
|
|
@Enumerated(EnumType.STRING)
|
|
@Column(nullable = false, length = 20)
|
|
private UserStatus status = UserStatus.PENDING;
|
|
|
|
@Column(name = "is_admin", nullable = false)
|
|
private boolean isAdmin = false;
|
|
|
|
@Column(name = "created_at", nullable = false, updatable = false)
|
|
private LocalDateTime createdAt;
|
|
|
|
@Column(name = "updated_at", nullable = false)
|
|
private LocalDateTime updatedAt;
|
|
|
|
@Column(name = "last_login_at")
|
|
private LocalDateTime lastLoginAt;
|
|
|
|
@ManyToMany(fetch = FetchType.LAZY)
|
|
@JoinTable(
|
|
name = "user_roles",
|
|
joinColumns = @JoinColumn(name = "user_id"),
|
|
inverseJoinColumns = @JoinColumn(name = "role_id")
|
|
)
|
|
private Set<Role> roles = new HashSet<>();
|
|
|
|
public User(String email, String name, String avatarUrl) {
|
|
this.email = email;
|
|
this.name = name;
|
|
this.avatarUrl = avatarUrl;
|
|
}
|
|
|
|
public void activate() {
|
|
this.status = UserStatus.ACTIVE;
|
|
}
|
|
|
|
public void reject() {
|
|
this.status = UserStatus.REJECTED;
|
|
}
|
|
|
|
public void disable() {
|
|
this.status = UserStatus.DISABLED;
|
|
}
|
|
|
|
public void grantAdmin() {
|
|
this.isAdmin = true;
|
|
}
|
|
|
|
public void revokeAdmin() {
|
|
this.isAdmin = false;
|
|
}
|
|
|
|
public void updateLastLogin() {
|
|
this.lastLoginAt = LocalDateTime.now();
|
|
}
|
|
|
|
public void updateProfile(String name, String avatarUrl) {
|
|
this.name = name;
|
|
this.avatarUrl = avatarUrl;
|
|
}
|
|
|
|
public void updateRoles(Set<Role> roles) {
|
|
this.roles = roles;
|
|
}
|
|
|
|
@PrePersist
|
|
protected void onCreate() {
|
|
this.createdAt = LocalDateTime.now();
|
|
this.updatedAt = LocalDateTime.now();
|
|
}
|
|
|
|
@PreUpdate
|
|
protected void onUpdate() {
|
|
this.updatedAt = LocalDateTime.now();
|
|
}
|
|
}
|