Skip to content
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

Adjust permissions for update_provider method #44

Merged
merged 1 commit into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,19 @@ fn get_providers() -> Vec<ProviderView> {
#[update(guard = "require_register_provider")]
#[candid_method]
fn register_provider(provider: RegisterProvider) -> u64 {
do_register_provider(provider)
do_register_provider(ic_cdk::caller(), provider)
}

#[update(guard = "require_register_provider")]
#[candid_method]
fn unregister_provider(provider_id: u64) -> bool {
do_unregister_provider(provider_id)
do_unregister_provider(ic_cdk::caller(), provider_id)
}

// #[update(guard = "require_register_provider")]
#[update(guard = "require_admin_or_controller")]
#[update(guard = "require_register_provider")]
#[candid_method]
fn update_provider(provider: UpdateProvider) {
do_update_provider(provider)
do_update_provider(ic_cdk::caller(), provider)
}

#[query(guard = "require_register_provider")]
Expand Down Expand Up @@ -176,7 +175,7 @@ fn init() {
});

for provider in get_default_providers() {
do_register_provider(provider);
do_register_provider(ic_cdk::caller(), provider);
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/providers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub fn get_default_providers() -> Vec<RegisterProvider> {
]
}

pub fn do_register_provider(provider: RegisterProvider) -> u64 {
pub fn do_register_provider(caller: Principal, provider: RegisterProvider) -> u64 {
let parsed_url = url::Url::parse(&provider.base_url).expect("unable to parse service_url");
let host = parsed_url.host_str().expect("service_url host missing");
validate_base_url(host);
Expand All @@ -42,7 +42,7 @@ pub fn do_register_provider(provider: RegisterProvider) -> u64 {
provider_id,
Provider {
provider_id,
owner: ic_cdk::caller(),
owner: caller,
chain_id: provider.chain_id,
base_url: provider.base_url,
credential_path: provider.credential_path,
Expand All @@ -56,10 +56,10 @@ pub fn do_register_provider(provider: RegisterProvider) -> u64 {
provider_id
}

pub fn do_unregister_provider(provider_id: u64) -> bool {
pub fn do_unregister_provider(caller: Principal, provider_id: u64) -> bool {
PROVIDERS.with(|p| {
if let Some(provider) = p.borrow().get(&provider_id) {
if provider.owner == ic_cdk::caller() || is_authorized(Auth::Admin) {
if provider.owner == caller || is_authorized(Auth::Admin) {
return p.borrow_mut().remove(&provider_id).is_some();
} else {
ic_cdk::trap("Not authorized");
Expand All @@ -69,12 +69,12 @@ pub fn do_unregister_provider(provider_id: u64) -> bool {
})
}

pub fn do_update_provider(update: UpdateProvider) {
pub fn do_update_provider(caller: Principal, update: UpdateProvider) {
PROVIDERS.with(|p| {
let mut p = p.borrow_mut();
match p.get(&update.provider_id) {
Some(mut provider) => {
if provider.owner != ic_cdk::caller() && !is_authorized(Auth::Admin) {
if provider.owner != caller && !is_authorized(Auth::Admin) {
ic_cdk::trap("Provider owner != caller");
}
if let Some(url) = update.base_url {
Expand Down
Loading