Initial commit
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
package com.mzh.library.entity;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class AuthenticatedUser implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private final long id;
|
||||
private final String username;
|
||||
private final String displayName;
|
||||
private final Role role;
|
||||
private final Set<Permission> permissions;
|
||||
|
||||
public AuthenticatedUser(long id, String username, String displayName, Role role, Set<Permission> permissions) {
|
||||
this.id = id;
|
||||
this.username = username;
|
||||
this.displayName = displayName;
|
||||
this.role = role;
|
||||
this.permissions = Collections.unmodifiableSet(new LinkedHashSet<>(permissions));
|
||||
}
|
||||
|
||||
public static AuthenticatedUser from(User user, Set<Permission> permissions) {
|
||||
return new AuthenticatedUser(
|
||||
user.getId(),
|
||||
user.getUsername(),
|
||||
user.getDisplayName(),
|
||||
user.getRole(),
|
||||
permissions
|
||||
);
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public String getDisplayName() {
|
||||
return displayName;
|
||||
}
|
||||
|
||||
public Role getRole() {
|
||||
return role;
|
||||
}
|
||||
|
||||
public Set<Permission> getPermissions() {
|
||||
return permissions;
|
||||
}
|
||||
|
||||
public Set<String> getPermissionCodes() {
|
||||
return permissions.stream()
|
||||
.map(Permission::getCode)
|
||||
.collect(Collectors.toCollection(LinkedHashSet::new));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.mzh.library.entity;
|
||||
|
||||
public enum Permission {
|
||||
MANAGE_USERS("manage_users"),
|
||||
MANAGE_BOOKS("manage_books"),
|
||||
MANAGE_READERS("manage_readers"),
|
||||
MANAGE_BORROWING("manage_borrowing"),
|
||||
VIEW_REPORTS("view_reports"),
|
||||
VIEW_SYSTEM_LOGS("view_system_logs"),
|
||||
VIEW_CATALOG("view_catalog"),
|
||||
BORROW_BOOKS("borrow_books");
|
||||
|
||||
private final String code;
|
||||
|
||||
Permission(String code) {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public String getCode() {
|
||||
return code;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.mzh.library.entity;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
public enum Role {
|
||||
ADMINISTRATOR("administrator", "Administrator"),
|
||||
LIBRARIAN("librarian", "Librarian"),
|
||||
READER("reader", "Reader");
|
||||
|
||||
private final String code;
|
||||
private final String displayName;
|
||||
|
||||
Role(String code, String displayName) {
|
||||
this.code = code;
|
||||
this.displayName = displayName;
|
||||
}
|
||||
|
||||
public String getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public String getDisplayName() {
|
||||
return displayName;
|
||||
}
|
||||
|
||||
public static Role fromCode(String code) {
|
||||
if (code == null || code.trim().isEmpty()) {
|
||||
throw new IllegalArgumentException("Role code is required");
|
||||
}
|
||||
|
||||
String normalized = code.trim().toLowerCase(Locale.ROOT);
|
||||
for (Role role : values()) {
|
||||
if (role.code.equals(normalized)) {
|
||||
return role;
|
||||
}
|
||||
}
|
||||
|
||||
throw new IllegalArgumentException("Unsupported role code: " + code);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package com.mzh.library.entity;
|
||||
|
||||
public class User {
|
||||
private long id;
|
||||
private String username;
|
||||
private String passwordHash;
|
||||
private String displayName;
|
||||
private Role role;
|
||||
private boolean active;
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public String getPasswordHash() {
|
||||
return passwordHash;
|
||||
}
|
||||
|
||||
public void setPasswordHash(String passwordHash) {
|
||||
this.passwordHash = passwordHash;
|
||||
}
|
||||
|
||||
public String getDisplayName() {
|
||||
return displayName;
|
||||
}
|
||||
|
||||
public void setDisplayName(String displayName) {
|
||||
this.displayName = displayName;
|
||||
}
|
||||
|
||||
public Role getRole() {
|
||||
return role;
|
||||
}
|
||||
|
||||
public void setRole(Role role) {
|
||||
this.role = role;
|
||||
}
|
||||
|
||||
public boolean isActive() {
|
||||
return active;
|
||||
}
|
||||
|
||||
public void setActive(boolean active) {
|
||||
this.active = active;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user