mirror of
https://git.imnavajas.es/jjimenez/erp-imprimelibros.git
synced 2026-01-13 08:58:48 +00:00
falta borrar y busqueda por columnas
This commit is contained in:
@ -1,35 +1,67 @@
|
||||
package com.imprimelibros.erp.users;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import java.util.Collection;
|
||||
import jakarta.validation.constraints.Email;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import org.hibernate.annotations.Formula;
|
||||
|
||||
@Entity
|
||||
@Table(name = "users")
|
||||
@Table(name = "users", uniqueConstraints = {
|
||||
@UniqueConstraint(name = "uk_users_username", columnNames = "username")
|
||||
})
|
||||
public class User {
|
||||
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
@Column(name = "id")
|
||||
private Long id;
|
||||
|
||||
@Column(name = "fullname")
|
||||
@NotBlank(message = "{validation.required}")
|
||||
private String fullName;
|
||||
|
||||
@Column(name = "username")
|
||||
@Column(name = "username", nullable = false, length = 190)
|
||||
@Email(message = "{validation.email}")
|
||||
@NotBlank(message = "{validation.required}")
|
||||
private String userName;
|
||||
|
||||
|
||||
@Column(name = "password")
|
||||
@NotBlank(message = "{validation.required}")
|
||||
private String password;
|
||||
|
||||
|
||||
@Column(name = "enabled")
|
||||
private boolean enabled;
|
||||
|
||||
@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
|
||||
@JoinTable(name = "users_roles",
|
||||
joinColumns = @JoinColumn(name = "user_id"),
|
||||
inverseJoinColumns = @JoinColumn(name = "role_id"))
|
||||
private Collection<Role> roles;
|
||||
@JoinTable(name = "users_roles", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "role_id"))
|
||||
private Set<Role> roles = new java.util.HashSet<>();
|
||||
|
||||
// SUPERADMIN=3, ADMIN=2, USER=1 (ajusta a tus nombres reales)
|
||||
@Formula("""
|
||||
(
|
||||
select coalesce(max(
|
||||
case r.name
|
||||
when 'SUPERADMIN' then 3
|
||||
when 'ADMIN' then 2
|
||||
else 1
|
||||
end
|
||||
), 0)
|
||||
from users_roles ur
|
||||
join roles r on r.id = ur.role_id
|
||||
where ur.user_id = id
|
||||
)
|
||||
""")
|
||||
private Integer roleRank;
|
||||
|
||||
@Formula("""
|
||||
(select group_concat(lower(r.name) order by r.name separator ', ')
|
||||
from users_roles ur join roles r on r.id = ur.role_id
|
||||
where ur.user_id = id)
|
||||
""")
|
||||
private String rolesConcat;
|
||||
|
||||
/* Constructors */
|
||||
public User() {
|
||||
@ -43,7 +75,7 @@ public class User {
|
||||
}
|
||||
|
||||
public User(String fullName, String userName, String password, boolean enabled,
|
||||
Collection<Role> roles) {
|
||||
Set<Role> roles) {
|
||||
this.fullName = fullName;
|
||||
this.userName = userName;
|
||||
this.password = password;
|
||||
@ -93,14 +125,22 @@ public class User {
|
||||
this.enabled = enabled;
|
||||
}
|
||||
|
||||
public Collection<Role> getRoles() {
|
||||
public Set<Role> getRoles() {
|
||||
return roles;
|
||||
}
|
||||
|
||||
public void setRoles(Collection<Role> roles) {
|
||||
public void setRoles(Set<Role> roles) {
|
||||
this.roles = roles;
|
||||
}
|
||||
|
||||
public Integer getRoleRank() {
|
||||
return roleRank;
|
||||
}
|
||||
|
||||
public String getRolesConcat() {
|
||||
return rolesConcat;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "User{" +
|
||||
@ -112,5 +152,5 @@ public class User {
|
||||
", roles=" + roles +
|
||||
'}';
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user