UNPKG

@ngx-security/roles

Version:
363 lines (351 loc) 15.7 kB
import * as i0 from '@angular/core'; import { Injectable, Input, Directive, Pipe, NgModule, makeEnvironmentProviders } from '@angular/core'; import { BehaviorSubject } from 'rxjs'; import { map } from 'rxjs/operators'; import * as i1 from '@ngx-security/core'; import { StandardSubjectService, SubjectService } from '@ngx-security/core'; class SubjectRolesProvider { constructor() { } hasRoleAsync(role) { return this.roles$.pipe(map((subjectRoles) => this._hasRole(role, subjectRoles))); } hasAnyRoleAsync(roles) { return this.roles$.pipe(map((subjectRoles) => this._hasAnyRole(roles, subjectRoles))); } hasRolesAsync(roles) { return this.roles$.pipe(map((subjectRoles) => this._hasRoles(roles, subjectRoles))); } hasRole(role) { return this._hasRole(role); } hasAnyRole(roles) { return this._hasAnyRole(roles); } hasRoles(roles) { return this._hasRoles(roles); } _hasRole(role, subjectRoles = this.getRoles()) { if (!subjectRoles || !role) { return false; } return subjectRoles.indexOf(role) >= 0; } _hasAnyRole(roles, subjectRoles = this.getRoles()) { if (!subjectRoles || !roles) { return false; } return subjectRoles.filter((subjectRole) => roles.indexOf(subjectRole) >= 0).length > 0; } _hasRoles(roles, subjectRoles = this.getRoles()) { if (!subjectRoles || !roles) { return false; } return subjectRoles.filter((subjectRole) => roles.indexOf(subjectRole) >= 0).length === roles.length; } } class StandardSubjectRolesProvider extends SubjectRolesProvider { constructor(subject) { super(); this.subject = subject; this.roles$ = this.subject.authorities$; } getRoles() { return this.subject.getAuthorities(); } static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.14", ngImport: i0, type: StandardSubjectRolesProvider, deps: [{ token: i1.SubjectService }], target: i0.ɵɵFactoryTarget.Injectable }); } static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.2.14", ngImport: i0, type: StandardSubjectRolesProvider }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.14", ngImport: i0, type: StandardSubjectRolesProvider, decorators: [{ type: Injectable }], ctorParameters: () => [{ type: i1.SubjectService }] }); class UpdatableSubjectRolesProvider extends SubjectRolesProvider { constructor() { super(); this.roles = new BehaviorSubject([]); this.roles$ = this.roles.asObservable(); } ngOnDestroy() { this.roles.complete(); } getRoles() { return this.roles.getValue(); } update(roles) { this.roles.next(roles); } static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.14", ngImport: i0, type: UpdatableSubjectRolesProvider, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); } static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.2.14", ngImport: i0, type: UpdatableSubjectRolesProvider }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.14", ngImport: i0, type: UpdatableSubjectRolesProvider, decorators: [{ type: Injectable }], ctorParameters: () => [] }); class HasAnyRoleDirective { set hasAnyRole(roles) { this.roles = roles; this.updateView(); } constructor(element, templateRef, viewContainer, subject) { this.element = element; this.templateRef = templateRef; this.viewContainer = viewContainer; this.subject = subject; this.roles = null; this.embeddedViewRef = null; } ngOnInit() { this.sub = this.subject.roles$.subscribe(() => this.updateView()); } ngOnDestroy() { if (this.sub) { this.sub.unsubscribe(); } } updateView() { if (!this.subject.hasAnyRole(this.roles)) { this.viewContainer.clear(); this.embeddedViewRef = null; } else if (!this.embeddedViewRef) { this.embeddedViewRef = this.viewContainer.createEmbeddedView(this.templateRef); } } static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.14", ngImport: i0, type: HasAnyRoleDirective, deps: [{ token: i0.ElementRef }, { token: i0.TemplateRef }, { token: i0.ViewContainerRef }, { token: SubjectRolesProvider }], target: i0.ɵɵFactoryTarget.Directive }); } static { this.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "19.2.14", type: HasAnyRoleDirective, isStandalone: true, selector: "[hasAnyRole]", inputs: { hasAnyRole: "hasAnyRole" }, ngImport: i0 }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.14", ngImport: i0, type: HasAnyRoleDirective, decorators: [{ type: Directive, args: [{ selector: '[hasAnyRole]' }] }], ctorParameters: () => [{ type: i0.ElementRef }, { type: i0.TemplateRef }, { type: i0.ViewContainerRef }, { type: SubjectRolesProvider }], propDecorators: { hasAnyRole: [{ type: Input }] } }); class HasAnyRolePipe { constructor(ref, subject) { this.ref = ref; this.subject = subject; this.hasAnyRole = null; this.sub = null; } transform(roles, rolesAsArg) { roles = rolesAsArg || roles; this.clear(); this.sub = this.subject.hasAnyRoleAsync(roles).subscribe((hasAnyRole) => { if (this.hasAnyRole !== hasAnyRole) { this.hasAnyRole = hasAnyRole; this.ref.markForCheck(); } }); return this.hasAnyRole; } ngOnDestroy() { this.clear(); } clear() { this.hasAnyRole = null; if (this.sub) { this.sub.unsubscribe(); this.sub = null; } } static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.14", ngImport: i0, type: HasAnyRolePipe, deps: [{ token: i0.ChangeDetectorRef }, { token: SubjectRolesProvider }], target: i0.ɵɵFactoryTarget.Pipe }); } static { this.ɵpipe = i0.ɵɵngDeclarePipe({ minVersion: "14.0.0", version: "19.2.14", ngImport: i0, type: HasAnyRolePipe, isStandalone: true, name: "hasAnyRole", pure: false }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.14", ngImport: i0, type: HasAnyRolePipe, decorators: [{ type: Pipe, args: [{ name: 'hasAnyRole', pure: false }] }], ctorParameters: () => [{ type: i0.ChangeDetectorRef }, { type: SubjectRolesProvider }] }); class HasRoleDirective { set hasRole(role) { this.role = role; this.updateView(); } constructor(element, templateRef, viewContainer, subject) { this.element = element; this.templateRef = templateRef; this.viewContainer = viewContainer; this.subject = subject; this.role = null; this.embeddedViewRef = null; } ngOnInit() { this.sub = this.subject.roles$.subscribe(() => this.updateView()); } ngOnDestroy() { if (this.sub) { this.sub.unsubscribe(); } } updateView() { if (!this.subject.hasRole(this.role)) { this.viewContainer.clear(); this.embeddedViewRef = null; } else if (!this.embeddedViewRef) { this.embeddedViewRef = this.viewContainer.createEmbeddedView(this.templateRef); } } static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.14", ngImport: i0, type: HasRoleDirective, deps: [{ token: i0.ElementRef }, { token: i0.TemplateRef }, { token: i0.ViewContainerRef }, { token: SubjectRolesProvider }], target: i0.ɵɵFactoryTarget.Directive }); } static { this.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "19.2.14", type: HasRoleDirective, isStandalone: true, selector: "[hasRole]", inputs: { hasRole: "hasRole" }, ngImport: i0 }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.14", ngImport: i0, type: HasRoleDirective, decorators: [{ type: Directive, args: [{ selector: '[hasRole]' }] }], ctorParameters: () => [{ type: i0.ElementRef }, { type: i0.TemplateRef }, { type: i0.ViewContainerRef }, { type: SubjectRolesProvider }], propDecorators: { hasRole: [{ type: Input }] } }); class HasRolePipe { constructor(ref, subject) { this.ref = ref; this.subject = subject; this.hasRole = null; this.sub = null; } transform(role, roleAsArg) { role = roleAsArg || role; this.clear(); this.sub = this.subject.hasRoleAsync(role).subscribe((hasRole) => { if (this.hasRole !== hasRole) { this.hasRole = hasRole; this.ref.markForCheck(); } }); return this.hasRole; } ngOnDestroy() { this.clear(); } clear() { this.hasRole = null; if (this.sub) { this.sub.unsubscribe(); this.sub = null; } } static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.14", ngImport: i0, type: HasRolePipe, deps: [{ token: i0.ChangeDetectorRef }, { token: SubjectRolesProvider }], target: i0.ɵɵFactoryTarget.Pipe }); } static { this.ɵpipe = i0.ɵɵngDeclarePipe({ minVersion: "14.0.0", version: "19.2.14", ngImport: i0, type: HasRolePipe, isStandalone: true, name: "hasRole", pure: false }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.14", ngImport: i0, type: HasRolePipe, decorators: [{ type: Pipe, args: [{ name: 'hasRole', pure: false }] }], ctorParameters: () => [{ type: i0.ChangeDetectorRef }, { type: SubjectRolesProvider }] }); class HasRolesDirective { set hasRoles(roles) { this.roles = roles; this.updateView(); } constructor(element, templateRef, viewContainer, subject) { this.element = element; this.templateRef = templateRef; this.viewContainer = viewContainer; this.subject = subject; this.roles = null; this.embeddedViewRef = null; } ngOnInit() { this.sub = this.subject.roles$.subscribe(() => this.updateView()); } ngOnDestroy() { if (this.sub) { this.sub.unsubscribe(); } } updateView() { if (!this.subject.hasRoles(this.roles)) { this.viewContainer.clear(); this.embeddedViewRef = null; } else if (!this.embeddedViewRef) { this.embeddedViewRef = this.viewContainer.createEmbeddedView(this.templateRef); } } static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.14", ngImport: i0, type: HasRolesDirective, deps: [{ token: i0.ElementRef }, { token: i0.TemplateRef }, { token: i0.ViewContainerRef }, { token: SubjectRolesProvider }], target: i0.ɵɵFactoryTarget.Directive }); } static { this.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "19.2.14", type: HasRolesDirective, isStandalone: true, selector: "[hasRoles]", inputs: { hasRoles: "hasRoles" }, ngImport: i0 }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.14", ngImport: i0, type: HasRolesDirective, decorators: [{ type: Directive, args: [{ selector: '[hasRoles]' }] }], ctorParameters: () => [{ type: i0.ElementRef }, { type: i0.TemplateRef }, { type: i0.ViewContainerRef }, { type: SubjectRolesProvider }], propDecorators: { hasRoles: [{ type: Input }] } }); class HasRolesPipe { constructor(ref, subject) { this.ref = ref; this.subject = subject; this.hasRoles = null; this.sub = null; } transform(roles, rolesAsArg) { roles = rolesAsArg || roles; this.clear(); this.sub = this.subject.hasRolesAsync(roles).subscribe((hasRoles) => { if (this.hasRoles !== hasRoles) { this.hasRoles = hasRoles; this.ref.markForCheck(); } }); return this.hasRoles; } ngOnDestroy() { this.clear(); } clear() { this.hasRoles = null; if (this.sub) { this.sub.unsubscribe(); this.sub = null; } } static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.14", ngImport: i0, type: HasRolesPipe, deps: [{ token: i0.ChangeDetectorRef }, { token: SubjectRolesProvider }], target: i0.ɵɵFactoryTarget.Pipe }); } static { this.ɵpipe = i0.ɵɵngDeclarePipe({ minVersion: "14.0.0", version: "19.2.14", ngImport: i0, type: HasRolesPipe, isStandalone: true, name: "hasRoles", pure: false }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.14", ngImport: i0, type: HasRolesPipe, decorators: [{ type: Pipe, args: [{ name: 'hasRoles', pure: false }] }], ctorParameters: () => [{ type: i0.ChangeDetectorRef }, { type: SubjectRolesProvider }] }); class SecurityRolesModule { static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.14", ngImport: i0, type: SecurityRolesModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); } static { this.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "19.2.14", ngImport: i0, type: SecurityRolesModule, imports: [HasAnyRoleDirective, HasRoleDirective, HasRolePipe, HasAnyRolePipe, HasRolesPipe, HasRolesDirective], exports: [HasAnyRoleDirective, HasRoleDirective, HasRolePipe, HasAnyRolePipe, HasRolesPipe, HasRolesDirective] }); } static { this.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "19.2.14", ngImport: i0, type: SecurityRolesModule }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.14", ngImport: i0, type: SecurityRolesModule, decorators: [{ type: NgModule, args: [{ imports: [ HasAnyRoleDirective, HasRoleDirective, HasRolePipe, HasAnyRolePipe, HasRolesPipe, HasRolesDirective ], exports: [ HasAnyRoleDirective, HasRoleDirective, HasRolePipe, HasAnyRolePipe, HasRolesPipe, HasRolesDirective ] }] }] }); function provideSecurityRoles(config = {}) { return makeEnvironmentProviders([ config.subject || { provide: SubjectService, useClass: StandardSubjectService }, config.subjectRoles || { provide: SubjectRolesProvider, useClass: StandardSubjectRolesProvider } ]); } /** * Generated bundle index. Do not edit. */ export { HasAnyRoleDirective, HasAnyRolePipe, HasRoleDirective, HasRolePipe, HasRolesDirective, HasRolesPipe, SecurityRolesModule, StandardSubjectRolesProvider, SubjectRolesProvider, UpdatableSubjectRolesProvider, provideSecurityRoles }; //# sourceMappingURL=ngx-security-roles.mjs.map