التحكم في الوصول
Ownable والأدوار
التحكم في الوصول درس مجاني في Web3 & DApp Development Fundamentals على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Web3 & DApp Development Fundamentals، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Web3 & DApp Development Fundamentals 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
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 withonlyOwnerand 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.
الأسئلة الشائعة
هل درس «التحكم في الوصول» مجاني؟
نعم — نص درس «التحكم في الوصول» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Web3 & DApp Development Fundamentals، انتقل إلى CoddyKit PRO. تتضمن دورة Web3 & DApp Development Fundamentals 4 دروس في المجموع.
ماذا ستتعلم في «التحكم في الوصول»؟
Ownable والأدوار تتمرن على Web3 & DApp Development Fundamentals مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Web3 & DApp Development Fundamentals؟
لا تُشترط خبرة سابقة. Web3 & DApp Development Fundamentals على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.
كم من الوقت يستغرق درس «التحكم في الوصول»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Web3 & DApp Development Fundamentals هذا؟
نعم. كل درس في Web3 & DApp Development Fundamentals يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- لماذا OpenZeppelin
- التحكم في الوصول
- امتدادات الرموز
- العقود القابلة للترقية