-
Notifications
You must be signed in to change notification settings - Fork 11
feat: Separate pause and unpause roles in Pausable plugin #156
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| # Migrating to Separate Pause/Unpause Roles | ||
|
|
||
| This guide explains how to migrate your code to use the new `pause_roles` and `unpause_roles` attributes instead of the consolidated `manager_roles` attribute in the Pausable plugin. | ||
|
|
||
| ## Changes Required | ||
|
|
||
| ### Before | ||
|
|
||
| Previously, you would define permissions for both pausing and unpausing using a single attribute: | ||
|
|
||
| ```rust | ||
| #[pausable(manager_roles(Role::PauseManager))] | ||
| struct Contract { | ||
| // Contract fields | ||
| } | ||
| ``` | ||
|
|
||
| This meant that any account with the `PauseManager` role could both pause and unpause features. | ||
|
|
||
| ### After | ||
|
|
||
| Now, you need to specify permissions for pausing and unpausing separately: | ||
|
|
||
| ```rust | ||
| #[pausable( | ||
| pause_roles(Role::PauseManager), | ||
| unpause_roles(Role::UnpauseManager) | ||
| )] | ||
| struct Contract { | ||
| // Contract fields | ||
| } | ||
| ``` | ||
|
|
||
| With this change, you can: | ||
| - Grant an account only the ability to pause features (emergency response) | ||
| - Grant a different account only the ability to unpause features (recovery process) | ||
| - Grant some accounts both abilities | ||
|
|
||
| ## Step-by-Step Migration | ||
|
|
||
| 1. **Update your Role enum** to include separate roles for pausing and unpausing, if desired: | ||
|
|
||
| ```rust | ||
| #[derive(AccessControlRole, Deserialize, Serialize, Copy, Clone)] | ||
| #[serde(crate = "near_sdk::serde")] | ||
| pub enum Role { | ||
| // Previous role that could both pause and unpause | ||
| // PauseManager, | ||
|
|
||
| // New separate roles | ||
| PauseManager, // Can only pause features | ||
| UnpauseManager, // Can only unpause features | ||
| // Other roles... | ||
| } | ||
| ``` | ||
|
|
||
| 2. **Update the pausable attribute** to use the new format: | ||
|
|
||
| ```rust | ||
| // Old format | ||
| // #[pausable(manager_roles(Role::PauseManager))] | ||
|
|
||
| // New format | ||
| #[pausable( | ||
| pause_roles(Role::PauseManager), | ||
| unpause_roles(Role::UnpauseManager) | ||
| )] | ||
| ``` | ||
|
|
||
| 3. **Update contract initialization** to grant the appropriate roles: | ||
|
|
||
| ```rust | ||
| #[init] | ||
| pub fn new(pause_manager: AccountId, unpause_manager: AccountId) -> Self { | ||
| let mut contract = Self { | ||
| // contract fields | ||
| }; | ||
|
|
||
| // Make the contract itself super admin | ||
| contract.acl_init_super_admin(env::current_account_id()); | ||
|
|
||
| // Grant pause role | ||
| contract.acl_grant_role(Role::PauseManager.into(), pause_manager); | ||
|
|
||
| // Grant unpause role (might be the same or different account) | ||
| contract.acl_grant_role(Role::UnpauseManager.into(), unpause_manager); | ||
|
|
||
| contract | ||
| } | ||
| ``` | ||
|
|
||
| 4. **Update tests** to test both pause and unpause permissions separately. | ||
|
|
||
| ## Example | ||
|
|
||
| Here's a complete example of a contract using the new separated roles: | ||
|
|
||
| ```rust | ||
| #[access_control(role_type(Role))] | ||
| #[near(contract_state)] | ||
| #[derive(Pausable, PanicOnDefault)] | ||
| #[pausable( | ||
| pause_roles(Role::PauseManager, Role::EmergencyPauser), | ||
| unpause_roles(Role::UnpauseManager, Role::ServiceRestorer) | ||
| )] | ||
| pub struct Counter { | ||
| counter: u64, | ||
| } | ||
| ``` | ||
|
|
||
| In this example: | ||
| - Accounts with either `PauseManager` or `EmergencyPauser` roles can pause features | ||
| - Accounts with either `UnpauseManager` or `ServiceRestorer` roles can unpause features | ||
| - An account might have multiple roles (e.g., both pause and unpause capabilities) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should never remove a variant or add one more in the middle of enums with
#[derive(AccessControlRole)], since it uses bitflags to store permissions for eachAccountId.Please, fix the migration guide.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@r-near can you please also add a migration test for this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cc @r-near
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@karim-en PR here: #163