Skip to content

Commit

Permalink
feat: register access control (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
pegahcarter authored Oct 21, 2024
1 parent 85414df commit 5ebccad
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
2 changes: 2 additions & 0 deletions contracts/tasks/TaskFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ contract TaskFactory is ITaskFactory, Initializable, PeriodUtils, AccessUtils {

/// @inheritdoc ITaskFactory
function registerDescriptions(Description[] calldata descriptions) external returns (bytes32[] memory) {
_revertIfNotAdmin();
uint256 length = descriptions.length;
bytes32[] memory newDescriptionIds = new bytes32[](length);
for (uint256 i = 0; i < length; i++) {
Expand All @@ -41,6 +42,7 @@ contract TaskFactory is ITaskFactory, Initializable, PeriodUtils, AccessUtils {

/// @inheritdoc ITaskFactory
function registerDescription(Description calldata description) external returns (bytes32) {
_revertIfNotAdmin();
return _registerDescription(description);
}

Expand Down
29 changes: 25 additions & 4 deletions contracts/tasks/TaskRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,38 @@ pragma solidity ^0.8.20;

import {EnumerableSet} from "@openzeppelin/contracts/utils/structs/EnumerableSet.sol";
import {Task, ITaskRegistry} from "./interfaces/ITaskRegistry.sol";
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";

contract TaskRegistry is ITaskRegistry {
contract TaskRegistry is ITaskRegistry, Ownable {
using EnumerableSet for EnumerableSet.Bytes32Set;

EnumerableSet.Bytes32Set private _taskIds;
mapping(bytes32 => Task) private _tasks;

// TODO: access control ?
address public approved;

constructor() Ownable(msg.sender) {}

// Access control to register tasks

function setApproved(address _approved) external onlyOwner {
approved = _approved;
}

error NotApproved();
modifier onlyApproved() {
if (!_isApproved(msg.sender)) revert NotApproved();
_;
}

/// @dev returns true if the approved address set to 0 or the msg.sender is the approved address
function _isApproved(address who) internal view returns (bool) {
address approved_ = approved; // gas
return (approved_ == address(0) || approved_ == msg.sender);
}

/// @inheritdoc ITaskRegistry
function registerTasks(Task[] calldata tasks) external returns (bytes32[] memory) {
function registerTasks(Task[] calldata tasks) external onlyApproved returns (bytes32[] memory) {
uint256 length = tasks.length;
bytes32[] memory newTaskIds = new bytes32[](length);
for (uint256 i = 0; i < length; i++) {
Expand All @@ -24,7 +45,7 @@ contract TaskRegistry is ITaskRegistry {
}

/// @inheritdoc ITaskRegistry
function registerTask(Task memory task) external returns (bytes32) {
function registerTask(Task memory task) external onlyApproved returns (bytes32) {
return _registerTask(task);
}

Expand Down

0 comments on commit 5ebccad

Please sign in to comment.