Skip to content

Commit

Permalink
Added GrantPermissions action to PermissionsController (#780)
Browse files Browse the repository at this point in the history
* Added GrantPermissions action to PermissionsController

* Added test for PermissionController:grantPermissions action
  • Loading branch information
ritave authored Apr 13, 2022
1 parent e4925ee commit e4d59b6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/permissions/PermissionController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import {
RestrictedMethodParameters,
ValidPermission,
} from '.';

// Caveat types and specifications

const CaveatTypes = {
Expand Down Expand Up @@ -4492,6 +4491,27 @@ describe('PermissionController', () => {
);
});

it('action: PermissionsController:grantPermissions', async () => {
const messenger = getUnrestrictedMessenger();
const options = getPermissionControllerOptions({
messenger: getPermissionControllerMessenger(messenger),
});
const controller = new PermissionController<
DefaultPermissionSpecifications,
DefaultCaveatSpecifications
>(options);

const result = messenger.call('PermissionController:grantPermissions', {
subject: { origin: 'foo' },
approvedPermissions: { wallet_getSecretArray: {} },
});

expect(result).toHaveProperty('wallet_getSecretArray');
expect(
controller.hasPermission('foo', 'wallet_getSecretArray'),
).toStrictEqual(true);
});

it('action: PermissionsController:requestPermissions', async () => {
const messenger = getUnrestrictedMessenger();
const options = getPermissionControllerOptions({
Expand Down
14 changes: 14 additions & 0 deletions src/permissions/PermissionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,14 @@ export type HasPermission = {
handler: GenericPermissionController['hasPermission'];
};

/**
* Directly grants given permissions for a specificed origin without requesting user approval
*/
export type GrantPermissions = {
type: `${typeof controllerName}:grantPermissions`;
handler: GenericPermissionController['grantPermissions'];
};

/**
* Requests given permissions for a specified origin
*/
Expand Down Expand Up @@ -272,6 +280,7 @@ export type PermissionControllerActions =
| GetPermissions
| HasPermission
| HasPermissions
| GrantPermissions
| RequestPermissions
| RevokeAllPermissions
| RevokePermissionForAllSubjects
Expand Down Expand Up @@ -690,6 +699,11 @@ export class PermissionController<
(origin: OriginString) => this.hasPermissions(origin),
);

this.messagingSystem.registerActionHandler(
`${controllerName}:grantPermissions` as const,
this.grantPermissions.bind(this),
);

this.messagingSystem.registerActionHandler(
`${controllerName}:requestPermissions` as const,
(subject: PermissionSubjectMetadata, permissions: RequestedPermissions) =>
Expand Down

0 comments on commit e4d59b6

Please sign in to comment.