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

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