Skip to content

Commit

Permalink
Adding the access control (#5) pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
mojtaba-eshghie committed Aug 28, 2023
1 parent b394a2d commit f04abb3
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ A collection of DCR graphs model of popular design patterns in Solidity smart co
| [Rate Limiting](/design-pattern-models/rate-limiting.md) | [View](https://dcrgraphs.net/tool/main/Graph?id=00307896-5158-418e-bd05-20b90c57f05c) | [Run](https://sim.dcrgraphs.net?code=00307896-5158-418e-bd05-20b90c57f05c) |
| [Speed Bump (timed temporal-constrained)](/design-pattern-models/speed-bump.md) | [view](https://dcrgraphs.net/tool/main/Graph?id=adcc8b39-de19-45c8-acb6-70f2bcbbc9e6) | [Run](https://sim.dcrgraphs.net?code=adcc8b39-de19-45c8-acb6-70f2bcbbc9e6) |
| [Safe Self-Destruction](/design-pattern-models/safe-self-destruction.md) | [View]() | [Run](https://sim.dcrgraphs.net?code=983d043a-45f2-46c2-b863-a4bae3b42af1) |
| [Access Control](/design-pattern-models/access-control.md) | Data 3C |
| [Access Control](/design-pattern-models/access-control.md) | [View](https://dcrgraphs.net/tool/main/Graph?id=b5d35894-e109-4e38-8b5d-c781ebe7b3cc) | [Run](https://sim.dcrgraphs.net?code=b5d35894-e109-4e38-8b5d-c781ebe7b3cc) |
| [Commit and Reveal](/design-pattern-models/commit-and-reveal.md) | Data 3C |
| [Circuit Breaker / Emergency Stop](/design-pattern-models/circuit-breaker.md) | Data 3C |
| [Escapable](/design-pattern-models/escapable.md) | Data 3C |
Expand Down
49 changes: 49 additions & 0 deletions design-pattern-models/access-control.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Access Control Pattern Model in DCR Graph

## Access Control Pattern Description

Access control restricts access to desired functions to only a subset of accounts calling them. A common instance of this pattern is to initialize a variable owner to the contract deployer and only allow this account successfully call certain functions. Here, we can nicely exploit that access control is built into DCR graphs as a first-class citizen, in the form of roles
assigned to activities. Each activity in a DCR model can be limited to one or more specific roles. In simpler scenarios, roles are assigned statically to accounts when a contract is deployed on the blockchain. In general, however, access rights can be assigned dynamically. DCR graphs support this using activity effects from an external database source. This feature allows changing the roles of activities as a result of an activity being executed.

## Example Usage

The activities and relations DCR model of this pattern is reflecting the following Solidity contract:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract RoleBasedAccess {
address public admin;
address public player;

constructor(address _player) {
admin = msg.sender; // The deployer of the contract is the admin
player = _player; // The player's address is set during contract deployment
}

modifier onlyAdmin() {
require(msg.sender == admin, "Only admin can call this function");
_;
}

modifier onlyPlayer() {
require(msg.sender == player, "Only player can call this function");
_;
}

function start() public onlyAdmin returns(string memory) {
return "Admin function executed!";
}

function play() public onlyPlayer returns(string memory) {
return "Player function executed!";
}
}

## DCR Model

![Access Control](/svg/access-control.svg)

[Download Access Control source](/src/access-control.xml)

[Link to the public DCR graph in DCRGraphs.net](https://dcrgraphs.net/tool/main/Graph?id=b5d35894-e109-4e38-8b5d-c781ebe7b3cc)

0 comments on commit f04abb3

Please sign in to comment.