Complete the code to define a simple owner check function in a smart contract.
function isOwner(address user) public view returns (bool) {
return user == [1];
}msg.sender instead of the stored owner variable.address(this) which is the contract address.The function checks if the given user address matches the stored owner address.
Complete the code to add a modifier that restricts function access to the owner only.
modifier onlyOwner() {
require([1] == owner, "Not owner");
_;
}tx.origin which can be unsafe for access control.owner to itself.The modifier checks that the caller (msg.sender) is the owner before running the function.
Fix the error in the access control check that uses a mapping for roles.
mapping(address => bool) public admins;
function restrictedAction() public {
require([1][msg.sender], "Not admin");
// action code
}The mapping is named admins, so the require statement must check admins[msg.sender].
Fill both blanks to create a function that grants admin role only by the owner.
function grantAdmin(address user) public [1] { admins[user] = true; emit AdminGranted(user); } modifier [2]() { require(msg.sender == owner, "Not owner"); _; }
public or external without access control.The function uses the onlyOwner modifier to restrict access to the owner only.
Fill all three blanks to implement a role-based access control check inside a function.
function performAction() public {
require(roles[msg.sender] [1] [2], "Access denied");
// action code
}
mapping(address => uint) public roles;
uint constant ADMIN_ROLE = [3];ADMIN_ROLE to 0 which usually means no role.The function checks if the caller's role is at least ADMIN_ROLE (which is 1) to allow access.