Class PermissionManager

java.lang.Object
com.privatechain.access.rbac.PermissionManager

public class PermissionManager extends Object
Manages role-based access control (RBAC) for a private blockchain network.

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 a ReadWriteLock. 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 Details

    • PermissionManager

      public PermissionManager(BlockchainStorage storage)
      Constructs a PermissionManager that persists role assignments to the provided BlockchainStorage.
      Parameters:
      storage - non-null storage backend for durability
      Throws:
      NullPointerException - if storage is null
  • Method Details

    • assignRole

      public void assignRole(String nodeId, NodeRole role)
      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 BlockchainStorage synchronously.

      Parameters:
      nodeId - the unique node identifier (non-null, non-blank)
      role - the role to assign (non-null)
      Throws:
      NullPointerException - if nodeId or role is null
      IllegalArgumentException - if nodeId is blank
    • revokeRole

      public void revokeRole(String nodeId)
      Revokes any role assignment for a given node.

      After revocation, hasRole(String, NodeRole) and getRole(String) will return false and Optional.empty(), respectively.

      Parameters:
      nodeId - the unique node identifier (non-null, non-blank)
      Throws:
      NullPointerException - if nodeId is null
      IllegalArgumentException - if nodeId is blank
    • hasRole

      public boolean hasRole(String nodeId, NodeRole role)
      Checks whether a given node has a specific role.

      Returns true only 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:
      true if the node has the role; false otherwise
      Throws:
      NullPointerException - if nodeId or role is null
      IllegalArgumentException - if nodeId is blank
    • getRole

      public Optional<NodeRole> getRole(String nodeId)
      Retrieves the role assigned to a given node, if any.

      Returns an empty Optional if the node has no assigned role or is not known to the manager.

      Parameters:
      nodeId - the node identifier (non-null, non-blank)
      Returns:
      an Optional containing the assigned role, or empty if none
      Throws:
      NullPointerException - if nodeId is null
      IllegalArgumentException - if nodeId is blank
    • getAllRoleAssignments

      public Map<String,NodeRole> getAllRoleAssignments()
      Returns all currently assigned role assignments.

      The returned Map is 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

      public Set<String> getNodesWithRole(NodeRole role)
      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