Warning! Contract bytecode has been changed and doesn't match the verified one. Therefore, interaction with this smart contract may be risky.
- Contract name:
- AccessManager
- Optimization enabled
- false
- Compiler version
- v0.8.26+commit.8a97fa7a
- EVM Version
- paris
- Verified at
- 2024-09-24T13:20:14.185474Z
contracts/Classes/AccessManager.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
import "./Access.sol"; // Assuming AuthLib includes all the functionality as discussed
/**
* @title AccessManager
* @dev A contract for managing role-based access control. Allows accounts to be granted or revoked roles, and checks for required ranks.
* This contract uses the AuthLib library for role management.
*/
contract AccessManager {
using AuthLib for AuthLib.RoleData;
// State variable for storing role data
AuthLib.RoleData private roleData;
// Event declarations for logging changes in role assignments
event RoleGranted(address indexed account, AuthLib.Rank role);
event RoleRevoked(address indexed account);
modifier onlyGladiator() {
require(checkRole(msg.sender, AuthLib.Rank.GLADIATOR), "Access Restricted");
_;
}
modifier onlySenator() {
require(checkRole(msg.sender, AuthLib.Rank.SENATOR), "Access Restricted");
_;
}
modifier onlyConsul() {
require(checkRole(msg.sender, AuthLib.Rank.CONSUL),"Access Restricted");
_;
}
modifier onlyLegatus() {
require(checkRole(msg.sender, AuthLib.Rank.LEGATUS),"Access Restricted");
_;
}
modifier onlyPreatormaximus() {
require(checkRole(msg.sender, AuthLib.Rank.PREATORMAXIMUS),"Access Restricted");
_;
}
uint256 private locked = 1;
modifier nonReentrant() virtual {
require(locked == 1, "REENTRANCY");
locked = 2;
_;
locked = 1;
}
/**
* @dev Constructor that grants the contract deployer the highest role (PREATORMAXIMUS).
*/
constructor() {
// Grant the deployer the highest role initially
roleData.grantRole(msg.sender, AuthLib.Rank.PREATORMAXIMUS);
}
/**
* @notice Grants a specific role to an account.
* @dev Only accounts with the CONSUL or higher role can grant roles.
* @param account The address of the account to grant the role to.
* @param rank The rank (role) to assign to the account.
*/
function grantRole(address account, AuthLib.Rank rank) public {
// Check if the caller has sufficient privileges (CONSUL or higher)
require(
roleData.getHighestRankForAccount(msg.sender) >= AuthLib.Rank.CONSUL,
"Insufficient privileges to grant roles."
);
// Grant the role to the specified account
roleData.grantRole(account, rank);
// Emit an event for logging
emit RoleGranted(account, rank);
}
/**
* @notice Revokes a role from a specific account.
* @dev Only accounts with the CONSUL or higher role can revoke roles.
* @param account The address of the account to revoke the role from.
*/
function revokeRole(address account) public {
// Check if the caller has sufficient privileges (CONSUL or higher)
require(
roleData.getHighestRankForAccount(msg.sender) >= AuthLib.Rank.CONSUL,
"Insufficient privileges to revoke roles."
);
// Revoke the role from the specified account
roleData.revokeRole(account);
// Emit an event for logging
emit RoleRevoked(account);
}
/**
* @notice Checks if an account has a specific role or higher.
* @param account The address of the account to check.
* @param rank The required rank to check against.
* @return True if the account holds the required rank or higher, false otherwise.
*/
function checkRole(address account, AuthLib.Rank rank) public view returns (bool) {
// Check if the account has the specified role or higher
return roleData.getHighestRankForAccount(account) >= rank;
}
/**
* @notice Returns the highest rank (role) assigned to an account.
* @param account The address of the account to query.
* @return The rank held by the account.
*/
function getAccountRank(address account) public view returns (AuthLib.Rank) {
// Retrieve the highest rank assigned to the account
return roleData.getHighestRankForAccount(account);
}
}
contracts/Classes/Access.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
/**
* @title AuthLib
* @dev A library for managing role-based access control (RBAC) using ranks. Allows accounts to be granted or revoked roles, and checks for required ranks.
*/
library AuthLib {
// Custom error for unauthorized account access
error AccessControlUnauthorizedAccount(address account, bytes32 neededRole);
// Enum representing different ranks in the system
enum Rank {
PRINCEPS,
GLADIATOR,
LEGATUS,
SENATOR,
CONSUL,
PREATORMAXIMUS
}
// Registry structure to store keys and their corresponding ranks
struct Registry {
uint256[] keys; // Array to hold registered keys
mapping(uint256 => uint256) indexOf; // Maps key to its index in the keys array
mapping(uint256 => bool) inserted; // Tracks whether a key is inserted
mapping(uint256 => Rank) keyRoles; // Maps key to its assigned rank
}
// RoleData structure for managing roles of accounts
struct RoleData {
Registry registry; // Holds the registry for keys and roles
mapping(address => Rank) ranks; // Maps accounts to their assigned ranks
}
// Events
event RoleGranted(address indexed account, Rank role);
event RoleRevoked(address indexed account, Rank role);
/**
* @dev Registers a key with a specified rank in the registry.
* @param _registry The registry where the key will be stored.
* @param key The key to register (typically the address cast to uint256).
* @param rank The rank to assign to the key.
*/
function Register(Registry storage _registry, uint256 key, Rank rank) public {
if (!_registry.inserted[key]) {
_registry.inserted[key] = true;
_registry.indexOf[key] = _registry.keys.length;
_registry.keys.push(key);
_registry.keyRoles[key] = rank; // Store the rank with the key
} else {
// Update the rank if already registered
_registry.keyRoles[key] = rank;
}
}
/**
* @dev Removes a key from the registry.
* @param _registry The registry from which the key will be removed.
* @param key The key to remove.
*/
function Remove(Registry storage _registry, uint256 key) public {
if (!_registry.inserted[key]) return;
delete _registry.inserted[key];
uint256 index = _registry.indexOf[key];
uint256 lastKey = _registry.keys[_registry.keys.length - 1];
_registry.keys[index] = lastKey;
_registry.indexOf[lastKey] = index;
delete _registry.indexOf[key];
_registry.keys.pop();
delete _registry.keyRoles[key]; // Remove the associated role
}
/**
* @dev Grants a role (rank) to an account. This updates the registry and emits a RoleGranted event.
* @param roleData The RoleData struct that holds the registry and ranks.
* @param account The account to grant the role to.
* @param rank The rank to assign to the account.
*/
function grantRole(RoleData storage roleData, address account, Rank rank) public {
uint256 key = uint256(uint160(account));
roleData.ranks[account] = rank;
Register(roleData.registry, key, rank);
emit RoleGranted(account, rank); // Emit event for granting role
}
/**
* @dev Revokes a role (rank) from an account. This updates the registry and emits a RoleRevoked event.
* @param roleData The RoleData struct that holds the registry and ranks.
* @param account The account from which the role will be revoked.
*/
function revokeRole(RoleData storage roleData, address account) public {
uint256 key = uint256(uint160(account));
Rank role = roleData.ranks[account]; // Capture the role before removal for the event
delete roleData.ranks[account];
Remove(roleData.registry, key);
emit RoleRevoked(account, role); // Emit event for revoking role
}
/**
* @dev Checks whether an account holds the required role (rank) or higher.
* @param roleData The RoleData struct that holds the registry and ranks.
* @param requiredRank The minimum required rank for the account.
* @param account The account to check for the required rank.
*/
function checkRole(RoleData storage roleData, Rank requiredRank, address account) public view {
require(roleData.ranks[account] >= requiredRank, "AccessControlUnauthorizedAccount");
}
/**
* @dev Retrieves the highest rank assigned to an account.
* @param roleData The RoleData struct that holds the registry and ranks.
* @param account The account whose rank is being queried.
* @return The highest rank held by the account.
*/
function getHighestRankForAccount(RoleData storage roleData, address account) public view returns (Rank) {
return roleData.ranks[account];
}
}
Compiler Settings
{"outputSelection":{"*":{"*":["abi","evm.bytecode","evm.deployedBytecode","evm.methodIdentifiers","metadata","storageLayout"],"":["ast"]}},"optimizer":{"runs":200,"enabled":false},"libraries":{"contracts/Classes/Access.sol":{"AuthLib":"0xc4d8811f729b26847b4619da49698be6f94bb912"}},"evmVersion":"paris"}
Contract ABI
[{"type":"constructor","stateMutability":"nonpayable","inputs":[]},{"type":"event","name":"RoleGranted","inputs":[{"type":"address","name":"account","internalType":"address","indexed":true},{"type":"uint8","name":"role","internalType":"enum AuthLib.Rank","indexed":false}],"anonymous":false},{"type":"event","name":"RoleRevoked","inputs":[{"type":"address","name":"account","internalType":"address","indexed":true}],"anonymous":false},{"type":"function","stateMutability":"view","outputs":[{"type":"bool","name":"","internalType":"bool"}],"name":"checkRole","inputs":[{"type":"address","name":"account","internalType":"address"},{"type":"uint8","name":"rank","internalType":"enum AuthLib.Rank"}]},{"type":"function","stateMutability":"view","outputs":[{"type":"uint8","name":"","internalType":"enum AuthLib.Rank"}],"name":"getAccountRank","inputs":[{"type":"address","name":"account","internalType":"address"}]},{"type":"function","stateMutability":"nonpayable","outputs":[],"name":"grantRole","inputs":[{"type":"address","name":"account","internalType":"address"},{"type":"uint8","name":"rank","internalType":"enum AuthLib.Rank"}]},{"type":"function","stateMutability":"nonpayable","outputs":[],"name":"revokeRole","inputs":[{"type":"address","name":"account","internalType":"address"}]}]
Contract Creation Code
0x6080604052600160055534801561001557600080fd5b50600073c4d8811f729b26847b4619da49698be6f94bb91263337b2d8290913360056040518463ffffffff1660e01b815260040161005593929190610149565b60006040518083038186803b15801561006d57600080fd5b505af4158015610081573d6000803e3d6000fd5b50505050610180565b8082525050565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b60006100bc82610091565b9050919050565b6100cc816100b1565b82525050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602160045260246000fd5b60068110610112576101116100d2565b5b50565b600081905061012382610101565b919050565b600061013382610115565b9050919050565b61014381610128565b82525050565b600060608201905061015e600083018661008a565b61016b60208301856100c3565b610178604083018461013a565b949350505050565b6109458061018f6000396000f3fe608060405234801561001057600080fd5b506004361061004c5760003560e01c806323a3ad72146100515780633e8402361461008157806380e52e3f1461009d578063a89027f8146100b9575b600080fd5b61006b600480360381019061006691906105de565b6100e9565b6040516100789190610639565b60405180910390f35b61009b600480360381019061009691906105de565b610197565b005b6100b760048036038101906100b29190610654565b61033b565b005b6100d360048036038101906100ce9190610654565b6104d1565b6040516100e091906106f8565b60405180910390f35b60008160058111156100fe576100fd610681565b5b600073c4d8811f729b26847b4619da49698be6f94bb9126301dba04c9091866040518363ffffffff1660e01b815260040161013a929190610729565b602060405180830381865af4158015610157573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061017b9190610767565b600581111561018d5761018c610681565b5b1015905092915050565b600460058111156101ab576101aa610681565b5b600073c4d8811f729b26847b4619da49698be6f94bb9126301dba04c9091336040518363ffffffff1660e01b81526004016101e7929190610729565b602060405180830381865af4158015610204573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906102289190610767565b600581111561023a57610239610681565b5b101561027b576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161027290610817565b60405180910390fd5b600073c4d8811f729b26847b4619da49698be6f94bb91263337b2d82909184846040518463ffffffff1660e01b81526004016102b993929190610846565b60006040518083038186803b1580156102d157600080fd5b505af41580156102e5573d6000803e3d6000fd5b505050508173ffffffffffffffffffffffffffffffffffffffff167faa259565575c834bc07e74dca784b4071676133ac78513b431afb6ee7edae1218260405161032f91906106f8565b60405180910390a25050565b6004600581111561034f5761034e610681565b5b600073c4d8811f729b26847b4619da49698be6f94bb9126301dba04c9091336040518363ffffffff1660e01b815260040161038b929190610729565b602060405180830381865af41580156103a8573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906103cc9190610767565b60058111156103de576103dd610681565b5b101561041f576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401610416906108ef565b60405180910390fd5b600073c4d8811f729b26847b4619da49698be6f94bb912631fe3a8a39091836040518363ffffffff1660e01b815260040161045b929190610729565b60006040518083038186803b15801561047357600080fd5b505af4158015610487573d6000803e3d6000fd5b505050508073ffffffffffffffffffffffffffffffffffffffff167f6107a4a5447a4208ba14e3ec240bccf0a93828124ccf501af04601031070b18360405160405180910390a250565b60008073c4d8811f729b26847b4619da49698be6f94bb9126301dba04c9091846040518363ffffffff1660e01b815260040161050e929190610729565b602060405180830381865af415801561052b573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061054f9190610767565b9050919050565b600080fd5b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b60006105868261055b565b9050919050565b6105968161057b565b81146105a157600080fd5b50565b6000813590506105b38161058d565b92915050565b600681106105c657600080fd5b50565b6000813590506105d8816105b9565b92915050565b600080604083850312156105f5576105f4610556565b5b6000610603858286016105a4565b9250506020610614858286016105c9565b9150509250929050565b60008115159050919050565b6106338161061e565b82525050565b600060208201905061064e600083018461062a565b92915050565b60006020828403121561066a57610669610556565b5b6000610678848285016105a4565b91505092915050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602160045260246000fd5b600681106106c1576106c0610681565b5b50565b60008190506106d2826106b0565b919050565b60006106e2826106c4565b9050919050565b6106f2816106d7565b82525050565b600060208201905061070d60008301846106e9565b92915050565b8082525050565b6107238161057b565b82525050565b600060408201905061073e6000830185610713565b61074b602083018461071a565b9392505050565b600081519050610761816105b9565b92915050565b60006020828403121561077d5761077c610556565b5b600061078b84828501610752565b91505092915050565b600082825260208201905092915050565b7f496e73756666696369656e742070726976696c6567657320746f206772616e7460008201527f20726f6c65732e00000000000000000000000000000000000000000000000000602082015250565b6000610801602783610794565b915061080c826107a5565b604082019050919050565b60006020820190508181036000830152610830816107f4565b9050919050565b610840816106d7565b82525050565b600060608201905061085b6000830186610713565b610868602083018561071a565b6108756040830184610837565b949350505050565b7f496e73756666696369656e742070726976696c6567657320746f207265766f6b60008201527f6520726f6c65732e000000000000000000000000000000000000000000000000602082015250565b60006108d9602883610794565b91506108e48261087d565b604082019050919050565b60006020820190508181036000830152610908816108cc565b905091905056fea2646970667358221220fbc0336c2cd6176f3432dd07e1fcf25e49f77480fe41ad38df558de9796eb11164736f6c634300081a0033
Deployed ByteCode
0x608060405234801561001057600080fd5b506004361061004c5760003560e01c806323a3ad72146100515780633e8402361461008157806380e52e3f1461009d578063a89027f8146100b9575b600080fd5b61006b600480360381019061006691906105de565b6100e9565b6040516100789190610639565b60405180910390f35b61009b600480360381019061009691906105de565b610197565b005b6100b760048036038101906100b29190610654565b61033b565b005b6100d360048036038101906100ce9190610654565b6104d1565b6040516100e091906106f8565b60405180910390f35b60008160058111156100fe576100fd610681565b5b600073c4d8811f729b26847b4619da49698be6f94bb9126301dba04c9091866040518363ffffffff1660e01b815260040161013a929190610729565b602060405180830381865af4158015610157573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061017b9190610767565b600581111561018d5761018c610681565b5b1015905092915050565b600460058111156101ab576101aa610681565b5b600073c4d8811f729b26847b4619da49698be6f94bb9126301dba04c9091336040518363ffffffff1660e01b81526004016101e7929190610729565b602060405180830381865af4158015610204573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906102289190610767565b600581111561023a57610239610681565b5b101561027b576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161027290610817565b60405180910390fd5b600073c4d8811f729b26847b4619da49698be6f94bb91263337b2d82909184846040518463ffffffff1660e01b81526004016102b993929190610846565b60006040518083038186803b1580156102d157600080fd5b505af41580156102e5573d6000803e3d6000fd5b505050508173ffffffffffffffffffffffffffffffffffffffff167faa259565575c834bc07e74dca784b4071676133ac78513b431afb6ee7edae1218260405161032f91906106f8565b60405180910390a25050565b6004600581111561034f5761034e610681565b5b600073c4d8811f729b26847b4619da49698be6f94bb9126301dba04c9091336040518363ffffffff1660e01b815260040161038b929190610729565b602060405180830381865af41580156103a8573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906103cc9190610767565b60058111156103de576103dd610681565b5b101561041f576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401610416906108ef565b60405180910390fd5b600073c4d8811f729b26847b4619da49698be6f94bb912631fe3a8a39091836040518363ffffffff1660e01b815260040161045b929190610729565b60006040518083038186803b15801561047357600080fd5b505af4158015610487573d6000803e3d6000fd5b505050508073ffffffffffffffffffffffffffffffffffffffff167f6107a4a5447a4208ba14e3ec240bccf0a93828124ccf501af04601031070b18360405160405180910390a250565b60008073c4d8811f729b26847b4619da49698be6f94bb9126301dba04c9091846040518363ffffffff1660e01b815260040161050e929190610729565b602060405180830381865af415801561052b573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061054f9190610767565b9050919050565b600080fd5b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b60006105868261055b565b9050919050565b6105968161057b565b81146105a157600080fd5b50565b6000813590506105b38161058d565b92915050565b600681106105c657600080fd5b50565b6000813590506105d8816105b9565b92915050565b600080604083850312156105f5576105f4610556565b5b6000610603858286016105a4565b9250506020610614858286016105c9565b9150509250929050565b60008115159050919050565b6106338161061e565b82525050565b600060208201905061064e600083018461062a565b92915050565b60006020828403121561066a57610669610556565b5b6000610678848285016105a4565b91505092915050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602160045260246000fd5b600681106106c1576106c0610681565b5b50565b60008190506106d2826106b0565b919050565b60006106e2826106c4565b9050919050565b6106f2816106d7565b82525050565b600060208201905061070d60008301846106e9565b92915050565b8082525050565b6107238161057b565b82525050565b600060408201905061073e6000830185610713565b61074b602083018461071a565b9392505050565b600081519050610761816105b9565b92915050565b60006020828403121561077d5761077c610556565b5b600061078b84828501610752565b91505092915050565b600082825260208201905092915050565b7f496e73756666696369656e742070726976696c6567657320746f206772616e7460008201527f20726f6c65732e00000000000000000000000000000000000000000000000000602082015250565b6000610801602783610794565b915061080c826107a5565b604082019050919050565b60006020820190508181036000830152610830816107f4565b9050919050565b610840816106d7565b82525050565b600060608201905061085b6000830186610713565b610868602083018561071a565b6108756040830184610837565b949350505050565b7f496e73756666696369656e742070726976696c6567657320746f207265766f6b60008201527f6520726f6c65732e000000000000000000000000000000000000000000000000602082015250565b60006108d9602883610794565b91506108e48261087d565b604082019050919050565b60006020820190508181036000830152610908816108cc565b905091905056fea2646970667358221220fbc0336c2cd6176f3432dd07e1fcf25e49f77480fe41ad38df558de9796eb11164736f6c634300081a0033