package org.psesquared.server.model; import jakarta.persistence.CascadeType; import jakarta.persistence.Column; import jakarta.persistence.Entity; import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; import jakarta.persistence.Id; import jakarta.persistence.OneToMany; import jakarta.persistence.Table; import java.util.Collection; import java.util.List; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.authority.SimpleGrantedAuthority; import org.springframework.security.core.userdetails.UserDetails; /** * A user that synchronizes their podcasts via this server. */ @Data @Builder @NoArgsConstructor @AllArgsConstructor @Entity @Table(name = "users") public class User implements UserDetails { /** * The primary key for the table. */ @Id @GeneratedValue(strategy=GenerationType.SEQUENCE) @Column(name = "id", updatable = false) private Long id; /** * The username of the user. */ @Column(name = "username", unique = true, nullable = false, updatable = false) private String username; /** * The email address of the user. */ @Column(name = "email", unique = true, nullable = false) private String email; /** * The password of the user. */ @Column(name = "password", nullable = false) private String password; /** * The verification status of the user. */ @Column(name = "enabled", nullable = false) private boolean enabled; /** * Timestamp of when this user account was created. */ @Column(name = "created_at", nullable = false, updatable = false) private long createdAt; /** * The role of the user. */ @Column(name = "role", nullable = false) private Role role; /** * The subscription actions the user made. */ @OneToMany(mappedBy = "user", cascade = CascadeType.REMOVE) private List subscriptionActions; /** * The episode actions the user made. */ @OneToMany(mappedBy = "user", cascade = CascadeType.REMOVE) private List episodeActions; /** * Returns a collection with one {@link SimpleGrantedAuthority} * with {@link #role}. * * @return The collection of granted authorities */ @Override public Collection getAuthorities() { return List.of(new SimpleGrantedAuthority(role.toString())); } /** * Checks if this user account has not expired. * * @return {@code true} if the user account has not expired, *
* {@code false} otherwise */ @Override public boolean isAccountNonExpired() { return enabled; } /** * Checks if this user account is not locked. * * @return {@code true} if the user account is not locked, *
* {@code false} otherwise */ @Override public boolean isAccountNonLocked() { return enabled; } /** * Checks if this user account's credentials have not expired. * * @return {@code true} if the credentials have not expired, *
* {@code false} otherwise */ @Override public boolean isCredentialsNonExpired() { return enabled; } }