Skip to content

Commit 3533708

Browse files
authored
Merge pull request #29 from maxnorm/docs/auto-generated-26
[DOCS] Auto-generated Docs Pages
2 parents 732d26d + ae9fdc8 commit 3533708

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+2082
-1031
lines changed

website/docs/library/access/AccessControl/AccessControlFacet.mdx

Lines changed: 67 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
sidebar_position: 99
2+
sidebar_position: 2
33
title: "AccessControlFacet"
44
description: "Manages role-based access control within a diamond."
55
gitSource: "https://github.com/Perfect-Abstractions/Compose/tree/main/src/access/AccessControl/AccessControlFacet.sol"
@@ -25,15 +25,14 @@ Manages role-based access control within a diamond.
2525
</DocSubtitle>
2626

2727
<Callout type="info" title="Key Features">
28-
- Permission management via roles and accounts.
29-
- Role hierarchy support with administrative roles.
30-
- Batch operations for granting and revoking roles.
31-
- Reverts with specific errors for unauthorized actions.
28+
- Hierarchical role administration: roles can have their own admin roles.
29+
- Batch operations for granting and revoking multiple roles efficiently.
30+
- Explicit error messages for unauthorized access attempts.
3231
</Callout>
3332

3433
## Overview
3534

36-
The AccessControlFacet provides a robust role-based access control (RBAC) system for Compose diamonds. It allows granular permission management, enabling developers to define roles and assign them to accounts. This facet is crucial for orchestrating secure interactions by enforcing role requirements on sensitive functions.
35+
The AccessControlFacet provides a robust role-based access control (RBAC) system for Compose diamonds. It enables granular permission management by defining roles, assigning them to accounts, and enforcing role requirements on function calls. This facet is crucial for securing administrative functions and controlling access to sensitive operations.
3736

3837
---
3938

@@ -478,58 +477,92 @@ error AccessControlUnauthorizedSender(address _sender, address _account);
478477
<ExpandableCode language="solidity" maxLines={20} title="Example Implementation">
479478
{`pragma solidity ^0.8.30;
480479
481-
import {DiamondLoupeFacet} from "@compose/diamond-loupe/src/DiamondLoupeFacet.sol";
482-
import {AccessControlFacet} from "@compose/access-control/src/AccessControlFacet.sol";
483-
484-
interface IDiamond {
485-
function diamondCut(...) external returns (bytes32);
486-
function facetFunction(bytes4 _selector) external view returns (address _facetAddress);
487-
}
480+
import {DiamondABI} from "@compose/diamond-abi/DiamondABI.sol";
481+
import {DiamondInit ABI} from "@compose/diamond-init/DiamondInitABI.sol";
482+
import {DiamondCutFacet} from "@compose/diamond-cut/DiamondCutFacet.sol";
483+
import {AccessControlFacet} from "@compose/access-control/AccessControlFacet.sol";
488484
485+
// Example of granting a role
489486
contract Deployer {
490-
// Assume diamond instance is deployed and initialized
491-
IDiamond internal diamond;
487+
address immutable diamondAddress;
492488
493489
constructor(address _diamondAddress) {
494-
diamond = IDiamond(_diamondAddress);
490+
diamondAddress = _diamondAddress;
495491
}
496492
497-
function grantAdminRole(address _admin, address _member) public {
498-
// Assuming 'grantRole' is the selector for AccessControlFacet.grantRole
499-
bytes4 grantRoleSelector = AccessControlFacet.grantRole.selector;
500-
(bool success, ) = address(diamond).call(abi.encodeWithSelector(grantRoleSelector, keccak256("ADMIN_ROLE"), _admin));
501-
require(success, "Failed to grant ADMIN_ROLE");
493+
function grantAdminRole() external {
494+
bytes4 selector = AccessControlFacet.grantRole.selector;
495+
// Assuming DEFAULT_ADMIN_ROLE is 0x00...00
496+
bytes memory data = abi.encodeWithSelector(selector, bytes32(0), msg.sender);
502497
503-
(success, ) = address(diamond).call(abi.encodeWithSelector(grantRoleSelector, keccak256("MEMBER_ROLE"), _member));
504-
require(success, "Failed to grant MEMBER_ROLE");
498+
// In a real deployment, this would be part of a DiamondCut
499+
// For demonstration, directly calling the facet via the diamond proxy
500+
diamondAddress.call(data);
505501
}
506502
507-
function checkMemberRole(address _account) public view returns (bool) {
508-
bytes4 hasRoleSelector = AccessControlFacet.hasRole.selector;
509-
(bool success, bytes memory result) = address(diamond).staticcall(abi.encodeWithSelector(hasRoleSelector, keccak256("MEMBER_ROLE"), _account));
510-
require(success, "Failed to check MEMBER_ROLE");
503+
function checkRole(address _account, bytes32 _role) public view returns (bool) {
504+
bytes4 selector = AccessControlFacet.hasRole.selector;
505+
bytes memory data = abi.encodeWithSelector(selector, _role, _account);
506+
(bool success, bytes memory result) = diamondAddress.staticcall(data);
507+
require(success, "checkRole failed");
511508
return abi.decode(result, (bool));
512509
}
513-
}
514-
`}
510+
}`}
515511
</ExpandableCode>
516512

517513
## Best Practices
518514

519515
<Callout type="tip" title="Best Practice">
520-
- Initialize roles and grant initial permissions during diamond deployment.
521-
- Use `grantRoleBatch` and `revokeRoleBatch` for efficient multi-account role management.
522-
- Design roles with clear administrative hierarchies using `setRoleAdmin`.
516+
- Initialize roles and their admins during diamond deployment using `DiamondInit`.
517+
- Grant roles to specific addresses or multisigs, avoiding broad grants to `address(0)`.
518+
- Use `requireRole` judiciously within other facets to protect sensitive functions.
523519
</Callout>
524520

525521
## Security Considerations
526522

527523
<Callout type="warning" title="Security">
528-
Ensure that the administrative roles are protected to prevent unauthorized role modifications. Sensitive functions should use `requireRole` to enforce access control. Be mindful of gas costs when performing batch operations on very large sets of accounts.
524+
Ensure that the caller of `setRoleAdmin`, `grantRole`, `revokeRole`, `grantRoleBatch`, and `revokeRoleBatch` is authorized by the role's admin. Be cautious when setting role admins to prevent privilege escalation. Reentrancy is not a direct concern for this facet's core logic, but ensure calling facets properly validate inputs before calling `requireRole`.
529525
</Callout>
530526

527+
<div style={{marginTop: "3rem", paddingTop: "2rem", borderTop: "1px solid var(--ifm-color-emphasis-200)"}}>
528+
<RelatedDocs
529+
items={[
530+
{
531+
title: "AccessControlMod",
532+
href: "/docs/library/access/AccessControl",
533+
description: "Module used by AccessControlFacet",
534+
icon: "📦"
535+
},
536+
{
537+
title: "OwnerTwoStepsFacet",
538+
href: "/docs/library/access/OwnerTwoSteps",
539+
description: "Related facet in access",
540+
icon: "💎"
541+
},
542+
{
543+
title: "AccessControlPausableFacet",
544+
href: "/docs/library/access/AccessControlPausable",
545+
description: "Related facet in access",
546+
icon: "💎"
547+
},
548+
{
549+
title: "AccessControlTemporalFacet",
550+
href: "/docs/library/access/AccessControlTemporal",
551+
description: "Related facet in access",
552+
icon: "💎"
553+
},
554+
{
555+
title: "OwnerFacet",
556+
href: "/docs/library/access/Owner",
557+
description: "Related facet in access",
558+
icon: "💎"
559+
}
560+
]}
561+
/>
562+
</div>
563+
531564
<div style={{marginTop: "3rem", paddingTop: "2rem", borderTop: "1px solid var(--ifm-color-emphasis-200)"}}>
532565
<WasThisHelpful pageId="AccessControlFacet" />
533566
</div>
534567

535-
<LastUpdated date="2025-12-28T04:28:16.357Z" />
568+
<LastUpdated date="2025-12-28T04:47:31.466Z" />

website/docs/library/access/AccessControl/AccessControlMod.mdx

Lines changed: 53 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
sidebar_position: 99
2+
sidebar_position: 1
33
title: "AccessControlMod"
44
description: "Manage roles and permissions within a diamond."
55
gitSource: "https://github.com/Perfect-Abstractions/Compose/tree/main/src/access/AccessControl/AccessControlMod.sol"
@@ -26,8 +26,9 @@ Manage roles and permissions within a diamond.
2626

2727
<Callout type="info" title="Key Features">
2828
- Role-based access control for granular permission management.
29-
- Standardized interface for granting, revoking, and checking roles.
30-
- Support for defining and assigning admin roles to manage other roles.
29+
- Functions to grant, revoke, and check for role ownership (`grantRole`, `revokeRole`, `hasRole`).
30+
- Support for setting and changing the administrative role for any given role (`setRoleAdmin`).
31+
- Built-in reversion with `AccessControlUnauthorizedAccount` for unauthorized access attempts.
3132
</Callout>
3233

3334
<Callout type="info" title="Module Usage">
@@ -36,7 +37,7 @@ This module provides internal functions for use in your custom facets. Import it
3637

3738
## Overview
3839

39-
The AccessControl module provides a robust system for managing roles and permissions within a Compose diamond. It allows for granular control over who can perform specific actions by assigning roles to accounts. This enhances security and enables composability by defining clear access boundaries for different functionalities.
40+
The AccessControl module provides a robust system for managing roles and permissions within a Compose diamond. It allows for granular control over which accounts can perform specific actions by assigning them roles. This is crucial for maintaining security and ensuring that only authorized entities can interact with sensitive functions.
4041

4142
---
4243

@@ -401,45 +402,75 @@ error AccessControlUnauthorizedAccount(address _account, bytes32 _role);
401402
<ExpandableCode language="solidity" maxLines={20} title="Example Implementation">
402403
{`pragma solidity ^0.8.30;
403404
404-
import {IAccessControl} from "@compose/diamond-contracts/contracts/modules/access/IAccessControl.sol";
405+
import {IAccessControlMod} from "@compose-protocol/diamond-contracts/contracts/modules/access/AccessControlMod.sol";
405406
406407
contract MyFacet {
407-
IAccessControl public accessControl;
408+
IAccessControlMod internal accessControl;
408409
409-
// Assume accessControl is initialized elsewhere
410+
function initialize(address accessControlAddress) external {
411+
accessControl = IAccessControlMod(accessControlAddress);
412+
}
410413
411-
function performAdminAction() external {
412-
address caller = _msgSender();
413-
bytes32 adminRole = accessControl.getRoleAdmin(AccessControl.DEFAULT_ADMIN_ROLE());
414-
accessControl.requireRole(caller, adminRole);
415-
// ... perform admin action ...
414+
function grantAdminRoleToUser(address user) external {
415+
bytes32 adminRole = accessControl.getStorage().DEFAULT_ADMIN_ROLE;
416+
accessControl.grantRole(adminRole, user);
416417
}
417418
418-
function grantNewRole(address _account, bytes32 _role) external {
419-
address caller = _msgSender();
420-
bytes32 adminRole = accessControl.getRoleAdmin(AccessControl.DEFAULT_ADMIN_ROLE());
421-
accessControl.requireRole(caller, adminRole);
422-
accessControl.grantRole(_role, _account);
419+
function performAdminAction() external {
420+
bytes32 adminRole = accessControl.getStorage().DEFAULT_ADMIN_ROLE;
421+
accessControl.requireRole(adminRole);
422+
// Perform admin action
423423
}
424424
}`}
425425
</ExpandableCode>
426426

427427
## Best Practices
428428

429429
<Callout type="tip" title="Best Practice">
430-
- Use `requireRole` to enforce access control checks directly within facet functions, reverting with `AccessControlUnauthorizedAccount` if the caller lacks the necessary role.
431-
- Understand that roles are managed by their designated admin role; ensure the admin role itself is properly secured.
432-
- When revoking roles, be mindful of the implications for ongoing operations that may rely on that role.
430+
- Use `requireRole` to enforce access control checks directly within facet functions, reverting with `AccessControlUnauthorizedAccount` on failure.
431+
- Define custom roles and manage their admin roles using `setRoleAdmin` to maintain a clear hierarchy and control over role assignments.
432+
- Ensure the AccessControl module is initialized with appropriate default admin roles during diamond deployment.
433433
</Callout>
434434

435435
## Integration Notes
436436

437437
<Callout type="success" title="Shared Storage">
438-
The AccessControl module stores its state within the diamond's storage. Facets interacting with AccessControl should use the `IAccessControl` interface. Functions like `grantRole`, `revokeRole`, and `setRoleAdmin` modify the access control state, which is immediately visible to all facets upon upgrade. The module relies on the diamond's underlying address to function.
438+
The AccessControl module stores its state within the diamond's storage. Facets interact with this module via its interface. The `getStorage()` function provides direct access to the module's internal storage struct, which contains mappings for roles, role admins, and account role assignments. Any changes made to role assignments or admin roles through the AccessControl module's functions are immediately reflected and accessible to all facets interacting with the diamond.
439439
</Callout>
440440

441+
<div style={{marginTop: "3rem", paddingTop: "2rem", borderTop: "1px solid var(--ifm-color-emphasis-200)"}}>
442+
<RelatedDocs
443+
items={[
444+
{
445+
title: "OwnerTwoStepsMod",
446+
href: "/docs/library/access/OwnerTwoSteps",
447+
description: "Related module in access",
448+
icon: "📦"
449+
},
450+
{
451+
title: "AccessControlPausableMod",
452+
href: "/docs/library/access/AccessControlPausable",
453+
description: "Related module in access",
454+
icon: "📦"
455+
},
456+
{
457+
title: "AccessControlTemporalMod",
458+
href: "/docs/library/access/AccessControlTemporal",
459+
description: "Related module in access",
460+
icon: "📦"
461+
},
462+
{
463+
title: "OwnerMod",
464+
href: "/docs/library/access/Owner",
465+
description: "Related module in access",
466+
icon: "📦"
467+
}
468+
]}
469+
/>
470+
</div>
471+
441472
<div style={{marginTop: "3rem", paddingTop: "2rem", borderTop: "1px solid var(--ifm-color-emphasis-200)"}}>
442473
<WasThisHelpful pageId="AccessControlMod" />
443474
</div>
444475

445-
<LastUpdated date="2025-12-28T04:28:12.206Z" />
476+
<LastUpdated date="2025-12-28T04:47:27.236Z" />

0 commit comments

Comments
 (0)