@casl/angular
Version:
Angular module for CASL which makes it easy to add permissions in any Angular app
100 lines (95 loc) • 4.13 kB
JavaScript
import * as i0 from '@angular/core';
import { Pipe, Inject, Injectable, signal, inject } from '@angular/core';
import { PureAbility } from '@casl/ability';
import { Observable } from 'rxjs';
/**
* @deprecated use AbilityService instead
*/
class AblePipe {
constructor(ability) {
this._ability = ability;
}
transform(...args) {
return this._ability.can(...args);
}
}
AblePipe.ɵfac = function AblePipe_Factory(__ngFactoryType__) { return new (__ngFactoryType__ || AblePipe)(i0.ɵɵdirectiveInject(PureAbility, 16)); };
AblePipe.ɵpipe = /*@__PURE__*/ i0.ɵɵdefinePipe({ name: "able", type: AblePipe, pure: false });
(() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(AblePipe, [{
type: Pipe,
args: [{ name: 'able', pure: false, standalone: true }]
}], () => [{ type: undefined, decorators: [{
type: Inject,
args: [PureAbility]
}] }], null); })();
/**
* @deprecated use AbilityService instead
*/
class AblePurePipe {
constructor(ability) {
this._ability = ability;
}
transform(...args) {
return new Observable((s) => {
const emit = () => s.next(this._ability.can(...args));
emit();
return this._ability.on('updated', emit);
});
}
}
AblePurePipe.ɵfac = function AblePurePipe_Factory(__ngFactoryType__) { return new (__ngFactoryType__ || AblePurePipe)(i0.ɵɵdirectiveInject(PureAbility, 16)); };
AblePurePipe.ɵpipe = /*@__PURE__*/ i0.ɵɵdefinePipe({ name: "ablePure", type: AblePurePipe, pure: true });
(() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(AblePurePipe, [{
type: Pipe,
args: [{ name: 'ablePure', standalone: true }]
}], () => [{ type: undefined, decorators: [{
type: Inject,
args: [PureAbility]
}] }], null); })();
class AbilityService {
constructor(ability) {
this.ability$ = new Observable((observer) => {
observer.next(ability);
return ability.on('updated', () => observer.next(ability));
});
}
}
AbilityService.ɵfac = function AbilityService_Factory(__ngFactoryType__) { return new (__ngFactoryType__ || AbilityService)(i0.ɵɵinject(PureAbility)); };
AbilityService.ɵprov = /*@__PURE__*/ i0.ɵɵdefineInjectable({ token: AbilityService, factory: AbilityService.ɵfac, providedIn: 'root' });
(() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(AbilityService, [{
type: Injectable,
args: [{ providedIn: 'root' }]
}], () => [{ type: undefined, decorators: [{
type: Inject,
args: [PureAbility]
}] }], null); })();
class AbilityServiceSignal {
constructor() {
this._rules = signal([], ...(ngDevMode ? [{ debugName: "_rules" }] : []));
this._ability = inject(PureAbility);
this.can = (...args) => {
this._rules(); // generate side effect for angular to track changes in this signal
return this._ability.can(...args);
};
this.cannot = (...args) => {
return !this.can(...args);
};
this._disposeAbilitySubscription = this._ability.on('updated', (event) => {
this._rules.set(event.rules);
});
}
ngOnDestroy() {
this._disposeAbilitySubscription();
}
update(rules) {
this._ability.update(rules);
}
}
AbilityServiceSignal.ɵfac = function AbilityServiceSignal_Factory(__ngFactoryType__) { return new (__ngFactoryType__ || AbilityServiceSignal)(); };
AbilityServiceSignal.ɵprov = /*@__PURE__*/ i0.ɵɵdefineInjectable({ token: AbilityServiceSignal, factory: AbilityServiceSignal.ɵfac, providedIn: 'root' });
(() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(AbilityServiceSignal, [{
type: Injectable,
args: [{ providedIn: 'root' }]
}], () => [], null); })();
export { AbilityService, AbilityServiceSignal, AblePipe, AblePurePipe };
//# sourceMappingURL=index.mjs.map