|
1 | | -import { PureAbility, AbilityOptions, AbilityOptionsOf } from './PureAbility'; |
2 | | -import { RawRuleFrom } from './RawRule'; |
3 | | -import { AbilityTuple } from './types'; |
4 | | -import { MongoQuery, mongoQueryMatcher } from './matchers/conditions'; |
5 | | -import { fieldPatternMatcher } from './matchers/field'; |
6 | | -import { Public, RawRuleOf } from './RuleIndex'; |
| 1 | +import { RuleIndex, RuleIndexOptions, RuleIndexOptionsOf, Public, RawRuleOf } from './RuleIndex'; |
| 2 | +import { Abilities, AbilityTuple, CanParameters, Subject } from './types'; |
| 3 | +import { Rule } from './Rule'; |
| 4 | + |
| 5 | +export interface AbilityOptions<A extends Abilities, Conditions> |
| 6 | + extends RuleIndexOptions<A, Conditions> {} |
| 7 | +export interface AnyAbility extends Public<Ability<any, any>> {} |
| 8 | +export interface AbilityOptionsOf<T extends AnyAbility> extends RuleIndexOptionsOf<T> {} |
| 9 | + |
| 10 | +export type AbilityClass<T extends AnyAbility> = new ( |
| 11 | + rules?: RawRuleOf<T>[], |
| 12 | + options?: AbilityOptionsOf<T> |
| 13 | +) => T; |
| 14 | + |
| 15 | +export type CreateAbility<T extends AnyAbility> = ( |
| 16 | + rules?: RawRuleOf<T>[], |
| 17 | + options?: AbilityOptionsOf<T> |
| 18 | +) => T; |
7 | 19 |
|
8 | | -/** |
9 | | - * @deprecated use createMongoAbility function instead and MongoAbility<Abilities> interface. |
10 | | - * In the next major version PureAbility will be renamed to Ability and this class will be removed |
11 | | - */ |
12 | 20 | export class Ability< |
13 | | - A extends AbilityTuple = AbilityTuple, |
14 | | - C extends MongoQuery = MongoQuery |
15 | | -> extends PureAbility<A, C> { |
16 | | - constructor(rules: RawRuleFrom<A, C>[] = [], options: AbilityOptions<A, C> = {}) { |
17 | | - super(rules, { |
18 | | - conditionsMatcher: mongoQueryMatcher, |
19 | | - fieldMatcher: fieldPatternMatcher, |
20 | | - ...options, |
21 | | - }); |
| 21 | + A extends Abilities = AbilityTuple, |
| 22 | + Conditions = unknown |
| 23 | +> extends RuleIndex<A, Conditions> { |
| 24 | + can(...args: CanParameters<A>): boolean; |
| 25 | + can(action: string, subject?: Subject, field?: string): boolean { |
| 26 | + const rule = (this as PrimitiveAbility).relevantRuleFor(action, subject, field); |
| 27 | + return !!rule && !rule.inverted; |
22 | 28 | } |
23 | | -} |
24 | 29 |
|
25 | | -export interface AnyMongoAbility extends Public<PureAbility<any, MongoQuery>> {} |
26 | | -export interface MongoAbility< |
27 | | - A extends AbilityTuple = AbilityTuple, |
28 | | - C extends MongoQuery = MongoQuery |
29 | | -> extends PureAbility<A, C> {} |
| 30 | + relevantRuleFor(...args: CanParameters<A>): Rule<A, Conditions> | null; |
| 31 | + relevantRuleFor(action: string, subject?: Subject, field?: string): Rule<A, Conditions> | null { |
| 32 | + const subjectType = this.detectSubjectType(subject); |
| 33 | + const rules = (this as any).rulesFor(action, subjectType, field); |
| 34 | + |
| 35 | + for (let i = 0, length = rules.length; i < length; i++) { |
| 36 | + if (rules[i].matchesConditions(subject)) { |
| 37 | + return rules[i]; |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + return null; |
| 42 | + } |
| 43 | + |
| 44 | + cannot(...args: CanParameters<A>): boolean; |
| 45 | + cannot(action: string, subject?: Subject, field?: string): boolean { |
| 46 | + return !(this as PrimitiveAbility).can(action, subject, field); |
| 47 | + } |
| 48 | +} |
30 | 49 |
|
31 | 50 | /** |
32 | | - * Creates Ability with MongoDB conditions matcher |
| 51 | + * helper interface that helps to emit js methods that have static parameters |
33 | 52 | */ |
34 | | -export function createMongoAbility< |
35 | | - T extends AnyMongoAbility = MongoAbility |
36 | | ->(rules?: RawRuleOf<T>[], options?: AbilityOptionsOf<T>): T; |
37 | | -export function createMongoAbility< |
38 | | - A extends AbilityTuple = AbilityTuple, |
39 | | - C extends MongoQuery = MongoQuery |
40 | | ->(rules?: RawRuleFrom<A, C>[], options?: AbilityOptions<A, C>): MongoAbility<A, C>; |
41 | | -export function createMongoAbility(rules: any[] = [], options = {}): AnyMongoAbility { |
42 | | - return new PureAbility(rules, { |
43 | | - conditionsMatcher: mongoQueryMatcher, |
44 | | - fieldMatcher: fieldPatternMatcher, |
45 | | - ...options, |
46 | | - }); |
| 53 | +interface PrimitiveAbility<A extends Abilities = AbilityTuple, Conditions = unknown> { |
| 54 | + can(action: string, subject?: Subject, field?: string): boolean; |
| 55 | + relevantRuleFor(action: string, subject?: Subject, field?: string): Rule<A, Conditions> | null |
47 | 56 | } |
0 commit comments