0Pricing
Web3 & DApp Development Fundamentals · Lektion

Zugriffskontrolle

Ownable und Rollen

Zugriffskontrolle ist eine kostenlose Web3 & DApp Development Fundamentals-Lektion auf CoddyKit. Dies ist Lektion 2 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Web3 & DApp Development Fundamentals-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Web3 & DApp Development Fundamentals-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

Why Access Control

Many contract functions should only be callable by certain accounts — minting tokens, pausing the system, withdrawing funds. Access control enforces who can do what.

OpenZeppelin offers two main patterns: Ownable and AccessControl.

The Ownable Pattern

Ownable gives a contract a single privileged owner. Import and inherit it:

import "@openzeppelin/contracts/access/Ownable.sol"; contract Vault is Ownable { constructor() Ownable(msg.sender) {} }

The deployer becomes the initial owner.

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

contract Vault is Ownable {
    constructor() Ownable(msg.sender) {}
}

The onlyOwner Modifier

Restrict a function to the owner with the onlyOwner modifier:

function withdraw() public onlyOwner { payable(owner()).transfer(address(this).balance); }

If anyone else calls it, the transaction reverts automatically.

function withdraw() public onlyOwner {
    payable(owner()).transfer(address(this).balance);
}

Transferring Ownership

Ownable lets you hand control to another address:

// Give ownership to a new account vault.transferOwnership(newOwner); // Or give it up forever vault.renounceOwnership();

Renouncing makes onlyOwner functions permanently uncallable — use with care.

// Give ownership to a new account
vault.transferOwnership(newOwner);

// Or give it up forever
vault.renounceOwnership();

Limits of a Single Owner

One owner is simple but limiting:

  • No way to grant different permissions to different people.
  • A single key is a single point of failure.

For richer setups, use role-based access control.

The AccessControl Pattern

AccessControl supports many named roles. Inherit it and define your roles:

import "@openzeppelin/contracts/access/AccessControl.sol"; contract Token is AccessControl { bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE"); }

Roles are identified by a hashed name.

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

contract Token is AccessControl {
    bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE");
}

Granting Roles

The deployer typically gets the admin role and then grants others:

constructor() { _grantRole(DEFAULT_ADMIN_ROLE, msg.sender); _grantRole(MINTER_ROLE, msg.sender); }

The DEFAULT_ADMIN_ROLE can grant and revoke all other roles.

constructor() {
    _grantRole(DEFAULT_ADMIN_ROLE, msg.sender);
    _grantRole(MINTER_ROLE, msg.sender);
}

The onlyRole Modifier

Restrict functions to holders of a role:

function mint(address to, uint256 amount) public onlyRole(MINTER_ROLE) { _mint(to, amount); }

Only accounts granted MINTER_ROLE can mint; everyone else reverts.

function mint(address to, uint256 amount)
    public onlyRole(MINTER_ROLE) {
    _mint(to, amount);
}

Managing Roles at Runtime

Admins can grant and revoke roles after deployment:

token.grantRole(MINTER_ROLE, alice); token.revokeRole(MINTER_ROLE, alice); // Check membership bool canMint = await token.hasRole(MINTER_ROLE, alice);

An account can even renounce its own role.

token.grantRole(MINTER_ROLE, alice);
token.revokeRole(MINTER_ROLE, alice);

// Check membership
bool canMint = await token.hasRole(MINTER_ROLE, alice);

Choosing a Pattern

Which to use?

  • Ownable — simple admin tasks, one trusted operator.
  • AccessControl — multiple roles, separation of duties, DAOs.

For production, consider giving the owner/admin role to a multisig rather than a single key.

Each Role Has an Admin

In AccessControl, every role has an admin role that controls who can grant or revoke it. By default that is DEFAULT_ADMIN_ROLE, but you can change it:

// Make MANAGER_ROLE the admin of MINTER_ROLE _setRoleAdmin(MINTER_ROLE, MANAGER_ROLE);

This lets you build hierarchies of permissions.

// Make MANAGER_ROLE the admin of MINTER_ROLE
_setRoleAdmin(MINTER_ROLE, MANAGER_ROLE);

Quick Check

Test your understanding of access control.

Recap

You learned OpenZeppelin's access control patterns.

  • Ownable gives one owner; restrict with onlyOwner and transfer or renounce ownership.
  • AccessControl supports many roles identified by hashed names.
  • Grant the admin role at deploy; protect functions with onlyRole.
  • Admins grant/revoke roles at runtime; accounts can renounce roles.
  • Use Ownable for simple cases, AccessControl (ideally behind a multisig) for complex ones.

Häufig gestellte Fragen

Ist die Lektion „Zugriffskontrolle“ kostenlos?

Ja — der vollständige Text von „Zugriffskontrolle“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Web3 & DApp Development Fundamentals-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Web3 & DApp Development Fundamentals-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Zugriffskontrolle“?

Ownable und Rollen Du übst Web3 & DApp Development Fundamentals mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um Web3 & DApp Development Fundamentals zu starten?

Keine Vorkenntnisse erforderlich. Web3 & DApp Development Fundamentals auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 2 von 4.

Wie lange dauert die Lektion „Zugriffskontrolle“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser Web3 & DApp Development Fundamentals-Lektion Code schreiben und ausführen?

Ja. Jede Web3 & DApp Development Fundamentals-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Warum OpenZeppelin?
  2. Zugriffskontrolle
  3. Token-Erweiterungen
  4. Upgradebare Contracts
← Zurück zu Web3 & DApp Development Fundamentals