Skip to content

Instantly share code, notes, and snippets.

@jmrocco

jmrocco/blog.md Secret

Created January 19, 2021 04:24
Show Gist options
  • Save jmrocco/15c3672259f3fc36d94193fe8d10caec to your computer and use it in GitHub Desktop.
Save jmrocco/15c3672259f3fc36d94193fe8d10caec to your computer and use it in GitHub Desktop.

Access Control

What is It?

The first category of contracts is access control. Access control allows a developer to regulate who can use certain features of the contract. Examples are: minting tokens, voting on proposals, ownership, etc. This feature is useful for creating a restrictive contract.

How to Use

OpenZeppelin provides two contracts: Ownable.sol and Roles.sol for access control. Both methods are useful in different scenarios depending on how restrictive you want the contract to be.

Ownership

Ownership is the most basic form of access control. It's the best method to use when you have one administrative user. To incorporate ownership, add an import statement at the beginning of your contract.

https://gist.github.com/9755d8ec1d7c0504e26503bb1f660414

Importing the Ownable.sol contract allows you to use functions such as transferOwnership(address newOwner) to transfer ownership to different users and renounceOwnership() to renounce ownership of the contract all together. Keep in mind that once a contract is renounced it cannot be claimed again.

The default owner of the contract is the msg.sender of the contract. You can change the owner in the Ownable.sol file.

Ownable contracts have an is Ownable statement. To specify which functions you only want the administrator to have access to, add onlyOwner.

https://gist.github.com/cce9b97179b8cb46f8f60d08200933e8

Ownable.sol is a basic implementation of access control that is optimal for a smaller group of users as well as one administrator.

Roles

Access control is also utilized through the contracts/access/Roles.sol contract. This contract allows you to assign roles to different users as well as control who can use certain functions. This is the best method when you have a multiple of users with varying levels of authority.

Add import "openzeppelin-solidity/contracts/access/Roles.sol"; to the top of your contract. Create your different roles Role private "your_Role". A require statement in your function states which users have access it.

https://gist.github.com/5b3bb78ec9f272ab88fb1648aaeaa670

OpenZeppelin Roles

Within the access folder, there are premade roles for you to use: Capper, Minter,Pauser,Signer,Whitelist Admin,and Whitelisted. To use any of these premade roles, import them the same way as with the other contracts. They contain functions to assign the role to your users, renounce the role, and restrict access for functions. All six contracts are identical to each other, except for their names. Thus if you want, you can create your own role contract using one of them as a template. To use, you import it into your function. The benefit of doing this would be that it provides a more detailed role. It also makes your code shorter.

More Details

For more examples of using Ownable.sol and Roles.sol check out the following links to open source code:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment