Reviewer 역할 검증 보고서 smoke (runtime-role-matrix-live-20260714101723-v7-reviewer-001)
This commit is contained in:
parent
2984c64361
commit
55351f4c1d
1 changed files with 122 additions and 0 deletions
122
src/main/java/com/klaroworks/runtime/role/RoleValidator.java
Normal file
122
src/main/java/com/klaroworks/runtime/role/RoleValidator.java
Normal file
|
|
@ -0,0 +1,122 @@
|
|||
package com.klaroworks.runtime.role;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* Role validator for validating role contexts and permissions.
|
||||
* Used by RoleValidatorTest for validation testing.
|
||||
*/
|
||||
public class RoleValidator {
|
||||
|
||||
private final Map<String, List<String>> validPermissions;
|
||||
private final Map<String, Integer> roleLevels;
|
||||
|
||||
public RoleValidator() {
|
||||
this.validPermissions = new HashMap<>();
|
||||
this.roleLevels = new HashMap<>();
|
||||
initializeDefaults();
|
||||
}
|
||||
|
||||
private void initializeDefaults() {
|
||||
validPermissions.put("ADMIN", List.of("*"));
|
||||
validPermissions.put("MANAGER", List.of("read", "write", "approve", "delete"));
|
||||
validPermissions.put("USER", List.of("read", "write"));
|
||||
validPermissions.put("GUEST", List.of("read"));
|
||||
|
||||
roleLevels.put("ADMIN", 100);
|
||||
roleLevels.put("MANAGER", 50);
|
||||
roleLevels.put("USER", 10);
|
||||
roleLevels.put("GUEST", 1);
|
||||
}
|
||||
|
||||
public ValidationResult validateContext(RoleContext context) {
|
||||
if (context == null) {
|
||||
return ValidationResult.failure("Context cannot be null");
|
||||
}
|
||||
|
||||
if (context.getUserId() == null || context.getUserId().isBlank()) {
|
||||
return ValidationResult.failure("User ID cannot be null or blank");
|
||||
}
|
||||
|
||||
if (context.getRole() == null || context.getRole().isBlank()) {
|
||||
return ValidationResult.failure("Role cannot be null or blank");
|
||||
}
|
||||
|
||||
if (!roleLevels.containsKey(context.getRole())) {
|
||||
return ValidationResult.failure("Unknown role: " + context.getRole());
|
||||
}
|
||||
|
||||
return ValidationResult.success();
|
||||
}
|
||||
|
||||
public ValidationResult validatePermission(String role, String permission) {
|
||||
if (role == null || role.isBlank()) {
|
||||
return ValidationResult.failure("Role cannot be null or blank");
|
||||
}
|
||||
|
||||
if (permission == null || permission.isBlank()) {
|
||||
return ValidationResult.failure("Permission cannot be null or blank");
|
||||
}
|
||||
|
||||
List<String> allowed = validPermissions.get(role);
|
||||
if (allowed == null) {
|
||||
return ValidationResult.failure("Unknown role: " + role);
|
||||
}
|
||||
|
||||
if (!allowed.contains(permission) && !allowed.contains("*")) {
|
||||
return ValidationResult.failure("Permission '" + permission + "' not allowed for role '" + role + "'");
|
||||
}
|
||||
|
||||
return ValidationResult.success();
|
||||
}
|
||||
|
||||
public boolean isValidRole(String role) {
|
||||
return roleLevels.containsKey(role);
|
||||
}
|
||||
|
||||
public boolean isValidPermission(String role, String permission) {
|
||||
return validatePermission(role, permission).isValid();
|
||||
}
|
||||
|
||||
public int getRoleLevel(String role) {
|
||||
return roleLevels.getOrDefault(role, 0);
|
||||
}
|
||||
|
||||
public boolean hasHigherPrivilege(String role1, String role2) {
|
||||
Integer level1 = roleLevels.get(role1);
|
||||
Integer level2 = roleLevels.get(role2);
|
||||
if (level1 == null || level2 == null) {
|
||||
return false;
|
||||
}
|
||||
return level1 > level2;
|
||||
}
|
||||
|
||||
public List<String> getAllowedPermissions(String role) {
|
||||
return List.copyOf(validPermissions.getOrDefault(role, List.of()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Validation result record.
|
||||
*/
|
||||
public record ValidationResult(boolean valid, String errorMessage) {
|
||||
public static ValidationResult success() {
|
||||
return new ValidationResult(true, null);
|
||||
}
|
||||
|
||||
public static ValidationResult failure(String message) {
|
||||
return new ValidationResult(false, message);
|
||||
}
|
||||
|
||||
public boolean isValid() {
|
||||
return valid;
|
||||
}
|
||||
|
||||
public Optional<String> getErrorMessage() {
|
||||
return Optional.ofNullable(errorMessage);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue