I am kinda new to blockchains and solidity. I am trying to create a smart contract that can keep track of inventory as well as be able to order new things if necessary. I would like it to have some kind of access control to make sure not everybody in my "firm" is able to order things. All I can find is the "onlyOwner" modifier which makes sense but is there a way to make it so that an owner and someone else can do it? Or just some specified person that is not necessarly the owner? Thank you so much in advance!
1 Answer
Have a look at the Open Zeppelin Access Control contracts.
You can then define roles for different groups within your company, one of which could be orderRole
or something similar.
You can then add the desired addresses to the mapping which defines who has role permissions.
struct RoleData {
mapping (address => bool) members;
bytes32 adminRole;
}
The contracts themselves are here.
-
Thank you! I have already look at this contract the only thing I am having difficulty figuring out is how to differentiate between different roles. So I imagine that if I wanted to add role called "developer" I would add the line "bytes32 developer;" to your code right? But how do you know whether it is the admin or developer that tries to do something? Commented Mar 12, 2021 at 10:17
-
I think you'd need to hash the role name, rather than use the name directly, like:
bytes32 public constant MY_ROLE = keccak256("MY_ROLE");
See this page -> docs.openzeppelin.com/contracts/4.x/api/access (this is a newer version of the page I previously linked to - I've edited my answer with the new link.) Commented Mar 12, 2021 at 11:15 -
Good question about distinguishing between the admin and the rest of the group. You could call
getRoleAdmin()
for the specific role, then check the admin role to see if the calling address is an admin. If it is, you could output a different event (or execute different code) to show that whatever function is being called is being called by the admin. Commented Mar 12, 2021 at 11:19