Skip to content

Commit abab449

Browse files
authored
feat(frontend-canister): allow setting permissions in init args (#3965)
1 parent 4e14a6f commit abab449

File tree

8 files changed

+68
-10
lines changed

8 files changed

+68
-10
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
# UNRELEASED
44

5+
### Frontend canister
6+
7+
Allow setting permissions lists in init arguments just like in upgrade arguments.
8+
9+
- Module hash: 2c24b5e1584890a7965011d5d1d827aca68c489c9a6308475730420fa53372e8
10+
- https://github.com/dfinity/sdk/pull/3965
11+
512
# 0.24.2
613

714
### feat: Support canister log allowed viewer list

docs/design/asset-canister-interface.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,13 @@ The size of any chunk cannot exceed the message ingress limit.
163163

164164
```candid
165165
service: (asset_canister_args: variant {
166-
Init: record {};
166+
Init: record {
167+
set_permissions: opt record {
168+
prepare: vec principal;
169+
commit: vec principal;
170+
manage_permissions: vec principal;
171+
};
172+
};
167173
Upgrade: record {
168174
set_permissions: opt record {
169175
prepare: vec principal;
@@ -179,7 +185,7 @@ The methods `init` and `post_upgrade` are called automatically by the system aft
179185
Both methods take the same argument type by definition. Therefore, to be able to have different arguments for the two cases, an enum is used to make the distinction.
180186
If `init` is called with the `Upgrade` variant or if `post_upgrade` is called with the `Init` variant the asset canister traps and thereby reverts the code changes.
181187

182-
In `Upgrade`, the field `set_permissions` can be used to (re)set the list of principals with the listed permissions.
188+
In both variants, the field `set_permissions` can be used to (re)set the list of principals with the listed permissions.
183189
If `set_permissions` that is not `null`, then all permissions are set to the newly provided list of principals and the previous lists of principals are discarded.
184190

185191
### Method: `get`

e2e/tests-dfx/assetscanister.bash

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1988,6 +1988,38 @@ WARN: {
19881988
assert_command dfx deploy
19891989
}
19901990

1991+
@test "set permissions through init argument" {
1992+
dfx_start
1993+
dfx deploy
1994+
1995+
dfx identity new alice --storage-mode plaintext
1996+
ALICE="$(dfx --identity alice identity get-principal)"
1997+
1998+
dfx canister install e2e_project_frontend --mode reinstall --yes --argument "(opt variant {
1999+
Init = record {
2000+
set_permissions = opt record {
2001+
prepare = vec {
2002+
principal \"${ALICE}\";
2003+
};
2004+
commit = vec {
2005+
principal \"$(dfx identity get-principal)\";
2006+
principal \"aaaaa-aa\";
2007+
};
2008+
manage_permissions = vec {
2009+
principal \"$(dfx identity get-principal)\";
2010+
};
2011+
}
2012+
}
2013+
})"
2014+
assert_command dfx canister call e2e_project_frontend list_permitted '(record { permission = variant { Prepare }; })'
2015+
assert_match "${ALICE}"
2016+
assert_command dfx canister call e2e_project_frontend list_permitted '(record { permission = variant { Commit }; })'
2017+
assert_match "$(dfx identity get-principal)"
2018+
assert_match '"aaaaa-aa"'
2019+
assert_command dfx canister call e2e_project_frontend list_permitted '(record { permission = variant { ManagePermissions }; })'
2020+
assert_match "$(dfx identity get-principal)"
2021+
}
2022+
19912023
@test "set permissions through upgrade argument" {
19922024
dfx_start
19932025
dfx deploy

src/canisters/frontend/ic-certified-assets/assets.did

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,9 @@ type AssetCanisterArgs = variant {
144144
Upgrade: UpgradeArgs;
145145
};
146146

147-
type InitArgs = record {};
147+
type InitArgs = record {
148+
set_permissions: opt SetPermissions;
149+
};
148150

149151
type UpgradeArgs = record {
150152
set_permissions: opt SetPermissions;

src/canisters/frontend/ic-certified-assets/src/lib.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -423,16 +423,23 @@ fn is_controller() -> Result<(), String> {
423423
}
424424

425425
pub fn init(args: Option<AssetCanisterArgs>) {
426-
if let Some(upgrade_arg) = args {
427-
let AssetCanisterArgs::Init(InitArgs {}) = upgrade_arg else {
428-
ic_cdk::trap("Cannot initialize the canister with an Upgrade argument. Please provide an Init argument.")
429-
};
430-
}
431426
STATE.with(|s| {
432427
let mut s = s.borrow_mut();
433428
s.clear();
434429
s.grant_permission(caller(), &Permission::Commit);
435430
});
431+
432+
if let Some(upgrade_arg) = args {
433+
let AssetCanisterArgs::Init(init_args) = upgrade_arg else {
434+
ic_cdk::trap("Cannot initialize the canister with an Upgrade argument. Please provide an Init argument.")
435+
};
436+
STATE.with(|s| {
437+
let mut state = s.borrow_mut();
438+
if let Some(set_permissions) = init_args.set_permissions {
439+
state.set_permissions(set_permissions);
440+
}
441+
});
442+
}
436443
}
437444

438445
pub fn pre_upgrade() -> StableState {

src/canisters/frontend/ic-certified-assets/src/types.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,9 @@ pub enum AssetCanisterArgs {
207207
}
208208

209209
#[derive(Clone, Debug, CandidType, Deserialize)]
210-
pub struct InitArgs {}
210+
pub struct InitArgs {
211+
pub set_permissions: Option<SetPermissions>,
212+
}
211213

212214
#[derive(Clone, Debug, CandidType, Deserialize)]
213215
pub struct UpgradeArgs {

src/distributed/assetstorage.did

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,9 @@ type AssetCanisterArgs = variant {
144144
Upgrade: UpgradeArgs;
145145
};
146146

147-
type InitArgs = record {};
147+
type InitArgs = record {
148+
set_permissions: opt SetPermissions;
149+
};
148150

149151
type UpgradeArgs = record {
150152
set_permissions: opt SetPermissions;
94 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)