Class PermissionManager
This class maintains the mapping of node addresses to their assigned roles
(NodeRole.NODE_ADMIN, NodeRole.NODE_MINER, NodeRole.NODE_OBSERVER).
All role assignments are persisted in the configured BlockchainStorage
so they survive node restarts.
Thread safety
All operations on role assignments are serialized via aReadWriteLock.
Multiple threads can read roles concurrently; writes are exclusive. This is appropriate
for scenarios where permission checks (reads) happen far more frequently than
permission updates (writes).
Usage
PermissionManager pm = new PermissionManager(storage);
pm.assignRole("node-123", NodeRole.NODE_MINER);
if (pm.hasRole("node-123", NodeRole.NODE_MINER)) {
acceptBlockSubmission(block);
}
- Since:
- 1.0.0
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionPermissionManager(BlockchainStorage storage) Constructs aPermissionManagerthat persists role assignments to the providedBlockchainStorage. -
Method Summary
Modifier and TypeMethodDescriptionvoidassignRole(String nodeId, NodeRole role) Assigns a role to a node identified by its node ID.Returns all currently assigned role assignments.getNodesWithRole(NodeRole role) Returns all node IDs that have been assigned a specific role.Retrieves the role assigned to a given node, if any.booleanChecks whether a given node has a specific role.voidrevokeRole(String nodeId) Revokes any role assignment for a given node.
-
Constructor Details
-
PermissionManager
Constructs aPermissionManagerthat persists role assignments to the providedBlockchainStorage.- Parameters:
storage- non-null storage backend for durability- Throws:
NullPointerException- if storage is null
-
-
Method Details
-
assignRole
Assigns a role to a node identified by its node ID.The assignment is immediately visible to all threads and is persisted to the configured
BlockchainStoragesynchronously.- Parameters:
nodeId- the unique node identifier (non-null, non-blank)role- the role to assign (non-null)- Throws:
NullPointerException- if nodeId or role is nullIllegalArgumentException- if nodeId is blank
-
revokeRole
Revokes any role assignment for a given node.After revocation,
hasRole(String, NodeRole)andgetRole(String)will returnfalseandOptional.empty(), respectively.- Parameters:
nodeId- the unique node identifier (non-null, non-blank)- Throws:
NullPointerException- if nodeId is nullIllegalArgumentException- if nodeId is blank
-
hasRole
Checks whether a given node has a specific role.Returns
trueonly if the node is assigned exactly that role. This method is read-locked and thread-safe for concurrent access.- Parameters:
nodeId- the node identifier (non-null, non-blank)role- the role to check (non-null)- Returns:
trueif the node has the role;falseotherwise- Throws:
NullPointerException- if nodeId or role is nullIllegalArgumentException- if nodeId is blank
-
getRole
Retrieves the role assigned to a given node, if any.Returns an empty
Optionalif the node has no assigned role or is not known to the manager.- Parameters:
nodeId- the node identifier (non-null, non-blank)- Returns:
- an
Optionalcontaining the assigned role, or empty if none - Throws:
NullPointerException- if nodeId is nullIllegalArgumentException- if nodeId is blank
-
getAllRoleAssignments
Returns all currently assigned role assignments.The returned
Mapis an unmodifiable copy made under a read lock, so the caller is free to iterate over it without blocking the manager.- Returns:
- an unmodifiable map of node IDs to roles
-
getNodesWithRole
Returns all node IDs that have been assigned a specific role.- Parameters:
role- the role to search for (non-null)- Returns:
- unmodifiable set of node IDs with the role
- Throws:
NullPointerException- if role is null
-