Skip to content

Commit

Permalink
feat: revert as on main
Browse files Browse the repository at this point in the history
Signed-off-by: David Dal Busco <[email protected]>
  • Loading branch information
peterpeterparker committed Nov 14, 2023
1 parent e4abd38 commit 45e2d93
Show file tree
Hide file tree
Showing 20 changed files with 15 additions and 109 deletions.
2 changes: 0 additions & 2 deletions src/declarations/satellite/satellite.did.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ export interface Rule {
memory: [] | [Memory];
updated_at: bigint;
max_size: [] | [bigint];
allow_anonymous: [] | [boolean];
read: Permission;
created_at: bigint;
mutable_permissions: [] | [boolean];
Expand All @@ -147,7 +146,6 @@ export interface SetRule {
memory: [] | [Memory];
updated_at: [] | [bigint];
max_size: [] | [bigint];
allow_anonymous: [] | [boolean];
read: Permission;
mutable_permissions: [] | [boolean];
write: Permission;
Expand Down
2 changes: 0 additions & 2 deletions src/declarations/satellite/satellite.factory.did.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@ export const idlFactory = ({ IDL }) => {
memory: IDL.Opt(Memory),
updated_at: IDL.Nat64,
max_size: IDL.Opt(IDL.Nat),
allow_anonymous: IDL.Opt(IDL.Bool),
read: Permission,
created_at: IDL.Nat64,
mutable_permissions: IDL.Opt(IDL.Bool),
Expand All @@ -173,7 +172,6 @@ export const idlFactory = ({ IDL }) => {
memory: IDL.Opt(Memory),
updated_at: IDL.Opt(IDL.Nat64),
max_size: IDL.Opt(IDL.Nat),
allow_anonymous: IDL.Opt(IDL.Bool),
read: Permission,
mutable_permissions: IDL.Opt(IDL.Bool),
write: Permission
Expand Down
2 changes: 0 additions & 2 deletions src/declarations/satellite/satellite.factory.did.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@ export const idlFactory = ({ IDL }) => {
memory: IDL.Opt(Memory),
updated_at: IDL.Nat64,
max_size: IDL.Opt(IDL.Nat),
allow_anonymous: IDL.Opt(IDL.Bool),
read: Permission,
created_at: IDL.Nat64,
mutable_permissions: IDL.Opt(IDL.Bool),
Expand All @@ -173,7 +172,6 @@ export const idlFactory = ({ IDL }) => {
memory: IDL.Opt(Memory),
updated_at: IDL.Opt(IDL.Nat64),
max_size: IDL.Opt(IDL.Nat),
allow_anonymous: IDL.Opt(IDL.Bool),
read: Permission,
mutable_permissions: IDL.Opt(IDL.Bool),
write: Permission
Expand Down
7 changes: 2 additions & 5 deletions src/frontend/src/lib/api/satellites.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ export const setRule = async ({
memory,
rule,
maxSize,
mutablePermissions,
allowAnonymous
mutablePermissions
}: {
satelliteId: Principal;
collection: string;
Expand All @@ -76,16 +75,14 @@ export const setRule = async ({
rule: Rule | undefined;
maxSize: number | undefined;
mutablePermissions: boolean;
allowAnonymous: boolean;
}) => {
const updateRule: SetRule = {
read: permissionFromText(read),
write: permissionFromText(write),
updated_at: isNullish(rule) ? [] : [rule.updated_at],
max_size: toNullable(nonNullish(maxSize) && maxSize > 0 ? BigInt(maxSize) : undefined),
memory: isNullish(rule) ? [memoryFromText(memory)] : [fromNullable(rule.memory) ?? MemoryHeap],
mutable_permissions: toNullable(mutablePermissions),
allow_anonymous: toNullable(allowAnonymous)
mutable_permissions: toNullable(mutablePermissions)
};

const actor = await getSatelliteActor(satelliteId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,6 @@
};
$: initMutable($store.rule?.[1] ?? undefined);
let currentAllowAnonymous: boolean;
let allowAnonymous: boolean;
const initAllowAnonymous = (initialRule: Rule | undefined) => {
currentAllowAnonymous = fromNullable(initialRule?.allow_anonymous ?? []) ?? false;
allowAnonymous = currentAllowAnonymous;
};
$: initAllowAnonymous($store.rule?.[1] ?? undefined);
let maxSize: number | undefined;
const initMaxLength = (size: [] | [bigint]) => {
const tmp = fromNullable(size);
Expand All @@ -86,8 +78,7 @@
type,
rule,
maxSize,
mutablePermissions: !immutable,
allowAnonymous
mutablePermissions: !immutable
});
toasts.success(
Expand Down Expand Up @@ -190,17 +181,6 @@
</div>
{/if}

<div class="checkbox">
<label>
<input
type="checkbox"
checked={allowAnonymous}
on:change={() => (allowAnonymous = !allowAnonymous)}
/>
<span>{$i18n.collections.allow_anonymous}</span>
</label>
</div>

{#if !currentImmutable}
<div class="checkbox">
<label>
Expand Down
3 changes: 1 addition & 2 deletions src/frontend/src/lib/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -356,8 +356,7 @@
"memory": "Memory",
"heap": "Heap",
"stable": "Stable",
"immutable": "Make permissions immutable",
"allow_anonymous": "Allow anonymous calls"
"immutable": "Make permissions immutable"
},
"sort": {
"title": "Sort results",
Expand Down
3 changes: 1 addition & 2 deletions src/frontend/src/lib/i18n/it.json
Original file line number Diff line number Diff line change
Expand Up @@ -356,8 +356,7 @@
"memory": "Memory",
"heap": "Heap",
"stable": "Stable",
"immutable": "Make permissions immutable",
"allow_anonymous": "Allow anonymous calls"
"immutable": "Make permissions immutable"
},
"sort": {
"title": "Ordina i risultati",
Expand Down
3 changes: 1 addition & 2 deletions src/frontend/src/lib/i18n/zh-cn.json
Original file line number Diff line number Diff line change
Expand Up @@ -356,8 +356,7 @@
"memory": "内存",
"heap": "堆栈",
"stable": "稳定",
"immutable": "将权限改为只读",
"allow_anonymous": "Allow anonymous calls"
"immutable": "将权限改为只读"
},
"sort": {
"title": "排序结果",
Expand Down
1 change: 0 additions & 1 deletion src/frontend/src/lib/types/i18n.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,6 @@ interface I18nCollections {
heap: string;
stable: string;
immutable: string;
allow_anonymous: string;
}

interface I18nSort {
Expand Down
2 changes: 0 additions & 2 deletions src/satellite/satellite.did
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ type Rule = record {
memory : opt Memory;
updated_at : nat64;
max_size : opt nat;
allow_anonymous : opt bool;
read : Permission;
created_at : nat64;
mutable_permissions : opt bool;
Expand All @@ -123,7 +122,6 @@ type SetRule = record {
memory : opt Memory;
updated_at : opt nat64;
max_size : opt nat;
allow_anonymous : opt bool;
read : Permission;
mutable_permissions : opt bool;
write : Permission;
Expand Down
19 changes: 3 additions & 16 deletions src/satellite/src/db/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,7 @@ fn get_doc_impl(
match value {
None => Ok(None),
Some(value) => {
if !assert_permission(
&rule.read,
&rule.allow_anonymous,
value.owner,
caller,
controllers,
) {
if !assert_permission(&rule.read, value.owner, caller, controllers) {
return Ok(None);
}

Expand Down Expand Up @@ -252,24 +246,17 @@ fn assert_write_permission(
user_timestamp: Option<u64>,
) -> Result<(), String> {
let permission = &rule.write;
let allow_anonymous = &rule.allow_anonymous;

// For existing collection and document, check user editing is the caller
if !public_permission(permission) {
match current_doc {
None => {
if !assert_create_permission(permission, allow_anonymous, caller, controllers) {
if !assert_create_permission(permission, caller, controllers) {
return Err(ERROR_CANNOT_WRITE.to_string());
}
}
Some(current_doc) => {
if !assert_permission(
permission,
allow_anonymous,
current_doc.owner,
caller,
controllers,
) {
if !assert_permission(permission, current_doc.owner, caller, controllers) {
return Err(ERROR_CANNOT_WRITE.to_string());
}
}
Expand Down
8 changes: 1 addition & 7 deletions src/satellite/src/db/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,7 @@ pub fn filter_values(
filter_key_matcher(&regex_key, key)
&& filter_description_matcher(&regex_description, &doc.description)
&& filter_owner(owner, &doc.owner)
&& assert_permission(
&rule.read,
&rule.allow_anonymous,
doc.owner,
caller,
controllers,
)
&& assert_permission(&rule.read, doc.owner, caller, controllers)
})
.map(|(key, doc)| (key.clone(), doc.clone()))
.collect()
Expand Down
2 changes: 0 additions & 2 deletions src/satellite/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ impl Default for HeapState {
write: rule.write,
memory: Some(rule.memory.unwrap_or(Memory::Heap)),
mutable_permissions: Some(rule.mutable_permissions.unwrap_or(false)),
allow_anonymous: Some(rule.allow_anonymous.unwrap_or(false)),
max_size: rule.max_size,
created_at: now,
updated_at: now,
Expand All @@ -57,7 +56,6 @@ impl Default for HeapState {
write: rule.write,
memory: Some(rule.memory.unwrap_or(Memory::Heap)),
mutable_permissions: Some(rule.mutable_permissions.unwrap_or(false)),
allow_anonymous: Some(rule.allow_anonymous.unwrap_or(false)),
max_size: rule.max_size,
created_at: now,
updated_at: now,
Expand Down
10 changes: 0 additions & 10 deletions src/satellite/src/rules/assert_stores.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,10 @@ use shared::utils::{principal_equal, principal_not_anonymous};

pub fn assert_permission(
permission: &Permission,
allow_anonymous: &Option<bool>,
owner: Principal,
caller: Principal,
controllers: &Controllers,
) -> bool {
if !assert_anonymous(allow_anonymous, caller) {
return false;
}

match permission {
Permission::Public => true,
Permission::Private => principal_equal(owner, caller),
Expand All @@ -29,14 +24,9 @@ pub fn assert_permission(
/// This can be useful e.g. when a collection read permission is set to public but only the administrator can add content.
pub fn assert_create_permission(
permission: &Permission,
allow_anonymous: &Option<bool>,
caller: Principal,
controllers: &Controllers,
) -> bool {
if !assert_anonymous(allow_anonymous, caller) {
return false;
}

match permission {
Permission::Public => true,
Permission::Private => true,
Expand Down
2 changes: 0 additions & 2 deletions src/satellite/src/rules/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ pub const DEFAULT_DB_COLLECTIONS: [(&str, SetRule); 1] = [(
write: Managed,
memory: Some(Memory::Heap),
mutable_permissions: Some(false),
allow_anonymous: Some(false),
max_size: None,
updated_at: None,
},
Expand All @@ -24,7 +23,6 @@ pub const DEFAULT_ASSETS_COLLECTIONS: [(&str, SetRule); 1] = [(
write: Controllers,
memory: Some(Memory::Heap),
mutable_permissions: Some(false),
allow_anonymous: Some(false),
max_size: None,
updated_at: None,
},
Expand Down
1 change: 0 additions & 1 deletion src/satellite/src/rules/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ fn set_rule_impl(
write: user_rule.write,
memory: Some(user_rule.memory.unwrap_or(default_memory)),
mutable_permissions: Some(user_rule.mutable_permissions.unwrap_or(true)),
allow_anonymous: user_rule.allow_anonymous,
max_size: user_rule.max_size,
};

Expand Down
5 changes: 1 addition & 4 deletions src/satellite/src/rules/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ pub mod rules {
use crate::types::core::CollectionKey;
use candid::CandidType;
use serde::{Deserialize, Serialize};
use shared::serializers::{deserialize_default_as_false, deserialize_default_as_true};
use shared::serializers::deserialize_default_as_true;
use std::collections::HashMap;

pub type Rules = HashMap<CollectionKey, Rule>;
Expand All @@ -13,8 +13,6 @@ pub mod rules {
pub write: Permission,
#[serde(default = "deserialize_default_as_true")]
pub mutable_permissions: Option<bool>,
#[serde(default = "deserialize_default_as_false")]
pub allow_anonymous: Option<bool>,
pub memory: Option<Memory>,
pub max_size: Option<u128>,
pub created_at: u64,
Expand Down Expand Up @@ -52,7 +50,6 @@ pub mod interface {
pub read: Permission,
pub write: Permission,
pub mutable_permissions: Option<bool>,
pub allow_anonymous: Option<bool>,
pub memory: Option<Memory>,
pub max_size: Option<u128>,
}
Expand Down
18 changes: 3 additions & 15 deletions src/satellite/src/storage/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,13 +193,7 @@ fn delete_asset_impl(
match asset {
None => Err(ERROR_ASSET_NOT_FOUND.to_string()),
Some(asset) => {
if !assert_permission(
&rule.write,
&rule.allow_anonymous,
asset.key.owner,
caller,
controllers,
) {
if !assert_permission(&rule.write, asset.key.owner, caller, controllers) {
return Err(ERROR_ASSET_NOT_FOUND.to_string());
}

Expand Down Expand Up @@ -451,7 +445,7 @@ fn secure_commit_chunks(

match current {
None => {
if !assert_create_permission(&rule.write, &rule.allow_anonymous, caller, controllers) {
if !assert_create_permission(&rule.write, caller, controllers) {
return Err(ERROR_CANNOT_COMMIT_BATCH.to_string());
}

Expand All @@ -476,13 +470,7 @@ fn secure_commit_chunks_update(
return Err("Provided collection does not match existing collection.".to_string());
}

if !assert_permission(
&rule.write,
&rule.allow_anonymous,
current.key.owner,
caller,
controllers,
) {
if !assert_permission(&rule.write, current.key.owner, caller, controllers) {
return Err(ERROR_CANNOT_COMMIT_BATCH.to_string());
}

Expand Down
8 changes: 1 addition & 7 deletions src/satellite/src/storage/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,7 @@ pub fn filter_values(
&& filter_full_path(&regex_key, asset)
&& filter_description(&regex_description, asset)
&& filter_owner(*owner, asset)
&& assert_permission(
&rule.read,
&rule.allow_anonymous,
asset.key.owner,
caller,
controllers,
)
&& assert_permission(&rule.read, asset.key.owner, caller, controllers)
})
.collect()
}
Expand Down
4 changes: 0 additions & 4 deletions src/shared/src/serializers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,3 @@ pub fn deserialize_from_bytes<T: for<'a> Deserialize<'a>>(bytes: Cow<'_, [u8]>)
pub fn deserialize_default_as_true() -> Option<bool> {
Some(true)
}

pub fn deserialize_default_as_false() -> Option<bool> {
Some(false)
}

0 comments on commit 45e2d93

Please sign in to comment.