diff --git a/src/main/java/com/klaroworks/runtime/role/RoleValidator.java b/src/main/java/com/klaroworks/runtime/role/RoleValidator.java new file mode 100644 index 0000000..a384720 --- /dev/null +++ b/src/main/java/com/klaroworks/runtime/role/RoleValidator.java @@ -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> validPermissions; + private final Map 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 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 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 getErrorMessage() { + return Optional.ofNullable(errorMessage); + } + } +}