исправила модель юзера, добавила класс-перечисление ролей юзера

This commit is contained in:
Елена Бакальская 2024-05-11 18:43:10 +04:00
parent f62b651f74
commit 2173e08c60
2 changed files with 25 additions and 10 deletions

View File

@ -18,17 +18,16 @@ public class UserEntity extends BaseEntity {
@Column(nullable = false, length = 5) @Column(nullable = false, length = 5)
private String password; private String password;
@Column(nullable = false) private UserRole role;
private boolean isAdmin;
public UserEntity() { public UserEntity() {
} }
public UserEntity(Integer id, String username, String password, boolean isAdmin) { public UserEntity(Integer id, String username, String password) {
this.username = username; this.username = username;
this.password = password; this.password = password;
this.isAdmin = isAdmin; this.role = UserRole.USER;
} }
public String getUsername() { public String getUsername() {
@ -47,17 +46,17 @@ public class UserEntity extends BaseEntity {
this.password = password; this.password = password;
} }
public boolean getIsAdmin() { public UserRole getRole() {
return isAdmin; return role;
} }
public void setIsAdmin(boolean isAdmin) { public void setRole(UserRole role) {
this.isAdmin = isAdmin; this.role = role;
} }
@Override @Override
public int hashCode() { public int hashCode() {
return Objects.hash(id, username, password, isAdmin); return Objects.hash(id, username, password, role);
} }
@Override @Override
@ -69,7 +68,7 @@ public class UserEntity extends BaseEntity {
final UserEntity other = (UserEntity) obj; final UserEntity other = (UserEntity) obj;
return Objects.equals(other.getId(), id) && return Objects.equals(other.getId(), id) &&
Objects.equals(other.getUsername(), username) && Objects.equals(other.getUsername(), username) &&
Objects.equals(other.getIsAdmin(), isAdmin) && Objects.equals(other.getRole(), role) &&
Objects.equals(other.getPassword(), password); Objects.equals(other.getPassword(), password);
} }
} }

View File

@ -0,0 +1,16 @@
package com.example.backend.users.model;
import org.springframework.security.core.GrantedAuthority;
public enum UserRole implements GrantedAuthority {
ADMIN,
USER;
private static final String PREFIX = "ROLE_";
@Override
public String getAuthority() {
return PREFIX + this.name();
}
}