-
Notifications
You must be signed in to change notification settings - Fork 4
/
ManufacturerRole.sol
54 lines (43 loc) · 1.79 KB
/
ManufacturerRole.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
pragma solidity ^0.4.24;
// Import the library 'Roles'
import "./Roles.sol";
// Define a contract 'ManufacturerRole' to manage this role - add, remove, check
contract ManufacturerRole {
using Roles for Roles.Role;
// Define 2 events, one for Adding, and other for Removing
event ManufacturerAdded(address indexed account);
event ManufacturerRemoved(address indexed account);
// Define a struct 'manufacturers' by inheriting from 'Roles' library, struct Role
Roles.Role private manufacturers;
// In the constructor make the address that deploys this contract the 1st manufacturer
constructor() public {
_addManufacturer(msg.sender);
}
// Define a modifier that checks to see if msg.sender has the appropriate role
modifier onlyManufacturer() {
require(isManufacturer(msg.sender), "sender is not a manufacturer");
_;
}
// Define a function 'isManufacturer' to check this role
function isManufacturer(address account) public view returns (bool) {
return manufacturers.has(account);
}
// Define a function 'addManufacturer' that adds this role
function addManufacturer(address account) public onlyManufacturer {
_addManufacturer(account);
}
// Define a function 'renounceManufacturer' to renounce this role
function renounceManufacturer() public {
_removeManufacturer(msg.sender);
}
// Define an internal function '_addManufacturer' to add this role, called by 'addManufacturer'
function _addManufacturer(address account) internal {
manufacturers.add(account);
emit ManufacturerAdded(account);
}
// Define an internal function '_removeManufacturer' to remove this role, called by 'removeManufacturer'
function _removeManufacturer(address account) internal {
manufacturers.remove(account);
emit ManufacturerRemoved(account);
}
}