@superawesome/permissions
Version:
Fine grained permissions / access control with ownerships & attribute picking, done right.
69 lines (62 loc) • 3.14 kB
TypeScript
import { RequireExactlyOne, MergeExclusive } from 'type-fest';
import { EPossession, TActionsList, TAttributes, TlimitOwned, TGrants, TisOwner, TlistOwned, Tid } from './types';
/**
This is the actual PermissionDefinition implementation, but the docs are in [PermissionDefinition_DOCS](/classes/PermissionDefinition_DOCS.html).
Implements the ownership hooks rules.
*/
export declare type PermissionDefinition<TUserId extends Tid = number, TResourceId extends Tid = number> = MergeExclusive<PermissionDefinitionNoOwnershipInternal, RequireExactlyOne<PermissionDefinitionWithOwnershipInternal<TUserId, TResourceId>, 'listOwned' | 'limitOwned'>>;
/**
* @internal
* This is an internal class - see [PermissionDefinition_DOCS](/classes/PermissionDefinition_DOCS.html)
*/
declare class PermissionDefinitionNoOwnershipInternal {
roles?: string | string[];
resource?: string;
descr?: string;
grant?: TGrants | TActionsList;
attributes?: TAttributes;
possession?: EPossession | 'own' | 'any';
}
/**
* @internal
* This is an internal class - see [PermissionDefinition_DOCS](/classes/PermissionDefinition_DOCS.html)
*/
declare class PermissionDefinitionWithOwnershipInternal<TUserId extends Tid, TResourceId extends Tid> extends PermissionDefinitionNoOwnershipInternal {
isOwner: TisOwner<TUserId, TResourceId>;
listOwned: TlistOwned<TUserId, TResourceId>;
limitOwned: TlimitOwned<TUserId, any>;
}
/**
The optional `PermissionDefinitionDefaults` is a single object (a Partial of [`PermissionDefinition`](/classes/PermissionDefinition_DOCS.html)) whose property values are merged with each [`PermissionDefinition`](/classes/PermissionDefinition_DOCS.html) instance, if an instance's property value is missing.
For example, in the code below:
```typescript
const pdDefaults: PermissionDefinitionDefaults = { resource: 'document' };
permissions.addDefinitions([ {PD1}, {PD2}, ..., {PDn} ], pdDefaults);
```
all PDs that are missing the `resource` property, they will end up with the `{ resource: 'document' }`.
*/
export declare class PermissionDefinitionDefaults {
roles?: string | string[];
resource?: string;
possession?: EPossession;
attributes?: TAttributes;
}
export interface ICompletePermissionDefinitions<TUserId extends Tid = number, TResourceId extends Tid = number> {
defaults?: PermissionDefinitionDefaults;
definitions: PermissionDefinition<TUserId, TResourceId>[];
}
/**
@internal
All `PermissionDefinition` are converted internally to a set of `PermissionDefinitionInternal`, after some consolidation takes place to settle defaults, remove duplicates etc.
A `PermissionDefinitionInternal` is **strict** and **self complete**, i.e it has settled/inherited the defaults and thus nas no missing props.
*/
export declare class PermissionDefinitionInternal<TUserId extends Tid = number, TResourceId extends Tid = number> {
roles: string[];
resource: string;
descr: string;
isOwner?: TisOwner<TUserId, TResourceId>;
listOwned?: TlistOwned<TUserId, TResourceId>;
limitOwned?: TlimitOwned<TUserId, any>;
grant: TGrants;
}
export {};