From fd034005b8284b5447f6f64148c8d1953150e356 Mon Sep 17 00:00:00 2001 From: Mirna Gama Date: Thu, 11 Jan 2024 14:47:51 -0300 Subject: [PATCH] [R9] User entity that implements UserDetails --- .../domain/entities/auth/User.java | 149 ++++++++++++++++++ 1 file changed, 149 insertions(+) create mode 100644 src/main/java/com/mirna/hospitalmanagementapi/domain/entities/auth/User.java diff --git a/src/main/java/com/mirna/hospitalmanagementapi/domain/entities/auth/User.java b/src/main/java/com/mirna/hospitalmanagementapi/domain/entities/auth/User.java new file mode 100644 index 0000000..0a517aa --- /dev/null +++ b/src/main/java/com/mirna/hospitalmanagementapi/domain/entities/auth/User.java @@ -0,0 +1,149 @@ +package com.mirna.hospitalmanagementapi.domain.entities.auth; + +import java.util.Collection; +import java.util.List; + +import org.springframework.security.core.GrantedAuthority; +import org.springframework.security.core.authority.SimpleGrantedAuthority; +import org.springframework.security.core.userdetails.UserDetails; + +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.Table; +import jakarta.validation.constraints.NotBlank; + +/** + * @see UserDetails + * @author Mirna Gama + * @version 1.0 + */ +@Table(name = "users") +@Entity(name = "User") +public class User implements UserDetails { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @NotBlank(message = "login cannot be blank") + @Column(name = "login") + private String login; + + @NotBlank(message = "password cannot be blank") + @Column(name = "password") + private String password; + + /** + * Returns the user id. + * + * @return A Long representing the user id. + */ + public Long getId() { + return id; + } + + /** + * Sets the user id. + * + * @param id The user's unique identifier. + */ + public void setId(Long id) { + this.id = id; + } + + /** + * Returns the login + * + * @return A string representing the user's system login. + */ + public String getLogin() { + return login; + } + + /** + * Sets the login + * + * @param login Must not be blank. + */ + public void setLogin(String login) { + this.login = login; + } + + /** + * Returns the password + * + * @return A string representing the user's system password. + */ + @Override + public String getPassword() { + return password; + } + + /** + * Sets the password + * + * @param password Must not be blank. + */ + public void setPassword(String password) { + this.password = password; + } + + @Override + public Collection getAuthorities() { + return List.of(new SimpleGrantedAuthority("ROLE_USER")); + } + + /** + * Returns the username. + * + * @return A string containing the user's system login + */ + @Override + public String getUsername() { + return login; + } + + /** + * Checks if the account is non expired + * + * @return true + */ + @Override + public boolean isAccountNonExpired() { + return true; + } + + /** + * Checks if the account is non locked + * + * @return true + */ + @Override + public boolean isAccountNonLocked() { + return true; + } + + /** + * Checks if the account credentials are non expired + * + * @return true + */ + @Override + public boolean isCredentialsNonExpired() { + return true; + } + + /** + * Checks if the entity is enabled + * + * @return true + */ + @Override + public boolean isEnabled() { + return true; + } + +}