diff --git a/src/main/java/com/klaroworks/runtime/role/RoleContext.java b/src/main/java/com/klaroworks/runtime/role/RoleContext.java new file mode 100644 index 0000000..51c18ec --- /dev/null +++ b/src/main/java/com/klaroworks/runtime/role/RoleContext.java @@ -0,0 +1,59 @@ +package com.klaroworks.runtime.role; + +import java.util.Map; +import java.util.Objects; +import java.util.concurrent.ConcurrentHashMap; + +/** + * Context holder for role evaluation containing user identity and runtime attributes. + */ +public final class RoleContext { + + private final String userId; + private final Map attributes; + + public RoleContext(String userId) { + this(userId, new ConcurrentHashMap<>()); + } + + public RoleContext(String userId, Map attributes) { + this.userId = Objects.requireNonNull(userId, "userId must not be null"); + this.attributes = new ConcurrentHashMap<>(Objects.requireNonNull(attributes, "attributes must not be null")); + } + + public String getUserId() { + return userId; + } + + public Map getAttributes() { + return Map.copyOf(attributes); + } + + public Object getAttribute(String key) { + return attributes.get(key); + } + + public RoleContext withAttribute(String key, Object value) { + Map newAttrs = new ConcurrentHashMap<>(attributes); + newAttrs.put(key, value); + return new RoleContext(userId, newAttrs); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + RoleContext that = (RoleContext) o; + return Objects.equals(userId, that.userId) && Objects.equals(attributes, that.attributes); + } + + @Override + public int hashCode() { + return Objects.hash(userId, attributes); + } + + @Override + public String toString() { + return "RoleContext{userId='" + userId + "', attributes=" + attributes + "}"; + } +}