package com.onelife.devmate.model;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.SuperBuilder;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import java.security.Principal;import java.time.LocalDateTime;
import java.util.Collection;import java.util.List;
import java.time.LocalDate;
import java.util.stream.Collectors;
@Entity
@SuperBuilder
@Getter
@Setter
@Table(name = "user" , schema = "public")
public class User implements UserDetails, Principal {
@Id
@GeneratedValue(strategy = GenerationType.*IDENTITY*)
private Long id;
private String username;
private String firstName;
private String lastName;
private LocalDate dateOfBirth;
@Column(unique = true)
private String email;
private String password;
private boolean accountLocked;
private boolean enabled;
@ManyToMany(fetch = FetchType.*EAGER*)
@JoinTable(
name = "user_roles",
joinColumns = @JoinColumn(name = "user_id"),
inverseJoinColumns = @JoinColumn(name = "role_id")
)
private List<Role> roles;
@Column(nullable = false,updatable = false)
private LocalDateTime createdDate;
@Column(insertable = false)
private LocalDateTime lastModifiedDate;
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return this.roles
.stream()
.map(r -> new SimpleGrantedAuthority(r.getName()))
.collect(Collectors.*toList*());
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id; }
public String getFirstName() {
return firstName; }
public void setFirstName(String firstName) {
this.firstName = firstName; }
public String getLastName() {
return lastName; }
public void setLastName(String lastName) {
this.lastName = lastName; }
public LocalDate getDateOfBirth() {
return dateOfBirth; }
public void setDateOfBirth(LocalDate dateOfBirth) {
this.dateOfBirth = dateOfBirth; }
public boolean isAccountLocked() {
return accountLocked; }
public void setAccountLocked(boolean accountLocked){
this.accountLocked = accountLocked; }
public boolean isEnabled() {
return enabled; }
public void setEnabled(boolean enabled) {
this.enabled = enabled; }
public List<Role> getRoles() {
return roles; }
public void setRoles(List<Role> roles) {
this.roles = roles; }
public LocalDateTime getCreatedDate() {
return createdDate; }
public void setCreatedDate(LocalDateTime createdDate) {
this.createdDate = createdDate; }
public LocalDateTime getLastModifiedDate() {
return lastModifiedDate; }
public void setLastModifiedDate(LocalDateTime lastModifiedDate) {
this.lastModifiedDate = lastModifiedDate; }
public String getEmail() {
return email; }
public void setEmail(String email) {
this.email = email; }
public String getPassword() {
return password; }
public void setPassword(String password) {
this.password = password; }
public String getUsername(){
return username; }
public void setUsername(String username){
this.username = username; }
@Override public String getName(){return username;}}