File

src/lib/has-write-permission.directive.ts

Implements

OnInit OnDestroy

Metadata

Index

Properties
Inputs
HostBindings

Constructor

constructor(authorization: AuthorizationService, cdr: ChangeDetectorRef, scope: string | null)
Parameters :
Name Type Optional
authorization AuthorizationService No
cdr ChangeDetectorRef No
scope string | null No

Inputs

rxapHasWritePermission
Type : string
Required :  true

HostBindings

readonly
Type : boolean
Default value : false

Properties

Public readonly
Default value : false
Decorators :
@HostBinding('readonly')
import {
  ChangeDetectorRef,
  Directive,
  HostBinding,
  Inject,
  Input,
  OnDestroy,
  OnInit,
  Optional,
} from '@angular/core';
import { Subscription } from 'rxjs';
import { tap } from 'rxjs/operators';
import { AuthorizationService } from './authorization.service';
import { RXAP_AUTHORIZATION_SCOPE } from './tokens';

@Directive({
  selector: '[rxapHasWritePermission]',
  standalone: true,
})
export class HasWritePermissionDirective implements OnInit, OnDestroy {
  @Input({
    alias: 'rxapHasWritePermission',
    required: true,
  })
  public identifier!: string;
  @HostBinding('readonly')
  public readonly = false;
  private _subscription?: Subscription;

  constructor(
    @Inject(AuthorizationService)
    private readonly authorization: AuthorizationService,
    @Inject(ChangeDetectorRef)
    protected readonly cdr: ChangeDetectorRef,
    @Optional()
    @Inject(RXAP_AUTHORIZATION_SCOPE)
    private readonly scope: string | null = null,
  ) {
  }

  public ngOnInit() {
    this._subscription = this.authorization
                             .hasPermission$(this.identifier, this.scope || null)
                             .pipe(
                               tap((hasPermission) => {
                                 this.readonly = !hasPermission;
                                 this.cdr.markForCheck();
                               }),
                             )
                             .subscribe();
  }

  public ngOnDestroy() {
    this._subscription?.unsubscribe();
  }
}

results matching ""

    No results matching ""