Skip to content

Commit

Permalink
feat(wallet): show global external canister permissions (#329)
Browse files Browse the repository at this point in the history
<img width="973" alt="Screenshot 2024-08-27 at 17 18 13"
src="https://github.com/user-attachments/assets/949adf92-582e-4aeb-83e7-5a293bea5634">
  • Loading branch information
keplervital authored Aug 27, 2024
1 parent f0c1ee1 commit 19241f6
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 1 deletion.
46 changes: 46 additions & 0 deletions apps/wallet/src/configs/permissions.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
isResourceActionContained,
isSystemResourceActionContained,
isUserResourceActionContained,
isExternalCanisterActionContained,
} from '~/utils/permissions.utils';
import { variantIs } from '~/utils/helper.utils';

Expand Down Expand Up @@ -308,6 +309,51 @@ export const globalPermissions = (): AggregatedResoucePermissions[] => [
return isRequestResourceActionContained(specifier.Request, resource.Request);
}

return false;
},
},
{
resourceType: ResourceTypeEnum.ExternalCanister,
resources: [
{
action: ResourceActionEnum.List,
resource: { ExternalCanister: { List: null } },
allow: defaultAllowLevels(),
canEdit: false,
},
{
action: ResourceActionEnum.Create,
resource: { ExternalCanister: { Create: null } },
allow: defaultAllowLevels(),
canEdit: false,
},
{
action: ResourceActionEnum.Read,
resource: { ExternalCanister: { Read: { Any: null } } },
allow: defaultAllowLevels(),
canEdit: false,
},
{
action: ResourceActionEnum.Change,
resource: { ExternalCanister: { Change: { Any: null } } },
allow: defaultAllowLevels(),
canEdit: false,
},
{
action: ResourceActionEnum.Fund,
resource: { ExternalCanister: { Fund: { Any: null } } },
allow: defaultAllowLevels(),
canEdit: false,
},
],
match(specifier: Resource, resource: Resource): boolean {
if (variantIs(specifier, 'ExternalCanister') && variantIs(resource, 'ExternalCanister')) {
return isExternalCanisterActionContained(
specifier.ExternalCanister,
resource.ExternalCanister,
);
}

return false;
},
},
Expand Down
3 changes: 3 additions & 0 deletions apps/wallet/src/locales/en.locale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,7 @@ export default {
request: 'Request',
addressbook: 'Address Book',
managesysteminfo: 'Manage System Info',
externalcanister: 'External Canister',
},
actions: {
list: 'List',
Expand All @@ -749,6 +750,8 @@ export default {
systeminfoconfig: 'Configuration (Upgrades, Metrics, Usage)',
managesysteminfo: 'Manage System Info (e.g. name)',
systemupgrade: 'Upgrade',
change: 'Change',
fund: 'Fund',
},
allow: {
public: 'Anyone',
Expand Down
3 changes: 3 additions & 0 deletions apps/wallet/src/locales/fr.locale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,7 @@ export default {
request: 'Demande',
addressbook: "Carnet d'Adresses",
managesysteminfo: 'Gérer les informations système',
externalcanister: 'Canister Géré',
},
actions: {
list: 'Lister',
Expand All @@ -759,6 +760,8 @@ export default {
systeminfoconfig: 'Configuration (Mises à jour, Métriques, Utilisation)',
managesysteminfo: 'Gérer les informations système (par exemple. nom)',
systemupgrade: 'Mise à jour du système',
change: 'Changement',
fund: 'Financer',
},
allow: {
public: "N'importe qui",
Expand Down
3 changes: 3 additions & 0 deletions apps/wallet/src/locales/pt.locale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,7 @@ export default {
request: 'Pedido',
addressbook: 'Livro de endereços',
managesysteminfo: 'Gerir informações do sistema',
externalcanister: 'Canister gerenciado',
},
actions: {
list: 'Listar',
Expand All @@ -756,6 +757,8 @@ export default {
systeminfoconfig: 'Configuração (Atualizações, Métricas, Uso)',
managesysteminfo: 'Gerir Informações do Sistema (e.g. nome)',
systemupgrade: 'Atualizar o sistema',
change: 'Alterar',
fund: 'Financiar',
},
allow: {
public: 'Acesso público',
Expand Down
2 changes: 2 additions & 0 deletions apps/wallet/src/types/permissions.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export enum ResourceActionEnum {
Create = 'Create',
Read = 'Read',
Update = 'Update',
Change = 'Change',
Fund = 'Fund',
Delete = 'Delete',
Transfer = 'Transfer',
SystemInfoConfig = 'SystemInfoConfig',
Expand Down
46 changes: 45 additions & 1 deletion apps/wallet/src/utils/permissions.utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import {
PermissionResourceAction,
AccountResourceAction,
ExternalCanisterId,
ExternalCanisterResourceAction,
PermissionResourceAction,
RequestResourceAction,
ResourceAction,
ResourceId,
Expand All @@ -24,6 +26,21 @@ export const isResourceIdContained = (a: ResourceId, b: ResourceId) => {
return variantIs(a, 'Any') && variantIs(b, 'Any');
};

/**
* Checks if `a` is contained in `b`
*
* @param a Specifier to check if it is contained in b
* @param b Specifier to check if it contains a
* @returns true if a is contained in b
*/
export const isExternalCanisterIdContained = (a: ExternalCanisterId, b: ExternalCanisterId) => {
if (variantIs(a, 'Canister') && variantIs(b, 'Canister')) {
return a.Canister === b.Canister;
}

return variantIs(a, 'Any') && variantIs(b, 'Any');
};

/**
* Checks if `a` is contained in `b`
*
Expand Down Expand Up @@ -113,6 +130,33 @@ export const isUserResourceActionContained = (a: UserResourceAction, b: UserReso
return false;
};

export const isExternalCanisterActionContained = (
a: ExternalCanisterResourceAction,
b: ExternalCanisterResourceAction,
) => {
if (variantIs(a, 'List') && variantIs(b, 'List')) {
return true;
}

if (variantIs(a, 'Create') && variantIs(b, 'Create')) {
return true;
}

if (variantIs(a, 'Read') && variantIs(b, 'Read')) {
return isExternalCanisterIdContained(a.Read, b.Read);
}

if (variantIs(a, 'Change') && variantIs(b, 'Change')) {
return isExternalCanisterIdContained(a.Change, b.Change);
}

if (variantIs(a, 'Fund') && variantIs(b, 'Fund')) {
return isExternalCanisterIdContained(a.Fund, b.Fund);
}

return false;
};

export const isAccountResourceActionContained = (
a: AccountResourceAction,
b: AccountResourceAction,
Expand Down

0 comments on commit 19241f6

Please sign in to comment.