Skip to content

Commit

Permalink
feat: added OZ Access control example (#539) (#609)
Browse files Browse the repository at this point in the history
* 539 OZ Access control example

Signed-off-by: ebadiere <[email protected]>

* Applied feedback

Signed-off-by: ebadiere <[email protected]>

* Fixed tag.

Signed-off-by: ebadiere <[email protected]>

---------

Signed-off-by: ebadiere <[email protected]>
  • Loading branch information
ebadiere authored Dec 12, 2023
1 parent f8c6ef6 commit 5804b9d
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
30 changes: 30 additions & 0 deletions contracts/oz/access-control/AccessControlContract.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/access/AccessControl.sol";

contract AccessControlContract is AccessControl {
// Define roles
bytes32 public constant ADMIN_ROLE = keccak256("ADMIN_ROLE");
bytes32 public constant MANAGER_ROLE = keccak256("MANAGER_ROLE");

constructor() {
// Assign the deployer (msg.sender) the admin role
_grantRole(ADMIN_ROLE, msg.sender);
}

// Function that can only be called by users with the admin role
function adminFunction() public view onlyRole(ADMIN_ROLE) returns (string memory) {
return "This function can only be called by administrators";
}

// Function that can only be called by users with the manager role
function managerFunction() public view onlyRole(MANAGER_ROLE) returns (string memory) {
return "This function can only be called by managers";
}

// Function to grant the manager role to an address
function grantManagerRole(address account) public onlyRole(ADMIN_ROLE) {
_grantRole(MANAGER_ROLE, account);
}
}
68 changes: 68 additions & 0 deletions test/OZ/access-control/accessControl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*-
*
* Hedera Smart Contracts
*
* Copyright (C) 2023 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

const { expect } = require("chai");
const { ethers } = require("hardhat");
const Constants = require('../../constants')

describe("@OZAccessControlContract", function () {
let admin;
let manager;
let user;
let accessContract;

const ADMIN_ROLE = ethers.utils.keccak256(ethers.utils.toUtf8Bytes("ADMIN_ROLE"));
const MANAGER_ROLE = ethers.utils.keccak256(ethers.utils.toUtf8Bytes("MANAGER_ROLE"));


// Deploy the contract and set up roles before each test
beforeEach(async function () {
[admin, manager, user] = await ethers.getSigners();

const AccessControlContract = await ethers.getContractFactory("AccessControlContract");
accessContract = await AccessControlContract.deploy(Constants.GAS_LIMIT_1_000_000);
await accessContract.deployed();

// Grant the MANAGER_ROLE to the manager address
await accessContract.connect(admin).grantManagerRole(manager.address);
});

it("admin should call adminFunction successfully", async function () {
const result = await accessContract.connect(admin).adminFunction();
expect(result).to.equal("This function can only be called by administrators");
});

it("manager should call managerFunction successfully", async function () {
const result = await accessContract.connect(manager).managerFunction();
expect(result).to.equal("This function can only be called by managers");
});

it("user should not be able to call adminFunction", async function () {
await expect(accessContract.connect(user).adminFunction())
.to.be.revertedWithCustomError(accessContract, "AccessControlUnauthorizedAccount")
.withArgs(user.address, ADMIN_ROLE);
});

it("user should not be able to call managerFunction", async function () {
await expect(accessContract.connect(user).managerFunction())
.to.be.revertedWithCustomError(accessContract, "AccessControlUnauthorizedAccount")
.withArgs(user.address, MANAGER_ROLE);
});
});

0 comments on commit 5804b9d

Please sign in to comment.