File

libs/acl/angular/src/lib/services/acl.service.ts

Index

Properties
Methods
Accessors

Constructor

constructor(injectedPolicyTxtContent?: string, aclStoreAdapter?: ExpressionStoreAdapter)
Parameters :
Name Type Optional
injectedPolicyTxtContent string Yes
aclStoreAdapter ExpressionStoreAdapter Yes

Methods

_addGlobalRule
_addGlobalRule(rule: AclRule)

Makes it possible to inject new global acl rules during runtime. This should only be used for development purposes, like the ACL Inspector. We might remove this from the public API at any time.

Parameters :
Name Type Optional
rule AclRule No
Returns : void
_setGlobalRules
_setGlobalRules(rules: AclRule[])

Allows to completely swap the global rules for a new one. This should only be used for development purposes, like from inside the ACL Inspector. We might remove this from the public API at any time.

Parameters :
Name Type Optional
rules AclRule[] No
Returns : void
addDynamicFormRules
addDynamicFormRules(formId: string, aclRules: AclRule[])
Parameters :
Name Type Optional
formId string No
aclRules AclRule[] No
Returns : void
canEdit
canEdit(aclPath: string)
Parameters :
Name Type Optional
aclPath string No
Returns : boolean
canEdit$
canEdit$(aclPath: string)
Parameters :
Name Type Optional
aclPath string No
Returns : any
canShow
Use `!aclService.isHidden(aclPath)` instead
canShow(aclPath: string)
Parameters :
Name Type Optional
aclPath string No
Returns : boolean
canShow$
Use `aclService.isHidden$(aclPath).pipe(map((value) => !value))` instead
canShow$(aclPath: string)
Parameters :
Name Type Optional
aclPath string No
Returns : any
currentState$
currentState$(aclPath: string)
Parameters :
Name Type Optional
aclPath string No
isDisabled
isDisabled(aclPath: string)
Parameters :
Name Type Optional
aclPath string No
Returns : boolean
isDisabled$
isDisabled$(aclPath: string)
Parameters :
Name Type Optional
aclPath string No
Returns : Observable<boolean>
isHidden
isHidden(aclPath: string)
Parameters :
Name Type Optional
aclPath string No
Returns : boolean
isHidden$
isHidden$(aclPath: string)
Parameters :
Name Type Optional
aclPath string No
Returns : Observable<boolean>
isReadonly
isReadonly(aclPath: string)
Parameters :
Name Type Optional
aclPath string No
Returns : boolean
isReadonly$
isReadonly$(aclPath: string)
Parameters :
Name Type Optional
aclPath string No
Returns : Observable<boolean>
removeDynamicFormRules
removeDynamicFormRules(formId: string)
Parameters :
Name Type Optional
formId string No
Returns : void
setEnvironmentValue
setEnvironmentValue(key: string, value: string)
Parameters :
Name Type Optional
key string No
value string No
Returns : void

Properties

aclEngine
Type : AclEngine
currentPolicy$
Type : ReplaySubject<AclRule[]>
Default value : new ReplaySubject<AclRule[]>(1)

Accessors

unscoped
getunscoped()
import {
  AclEngine,
  AclEvaluationInterface,
  AclRule,
  AclRuleState,
  ExpressionStoreAdapter
} from '@allianz/taly-acl';
import { Inject, Injectable, Optional } from '@angular/core';
import { Observable, ReplaySubject, combineLatest } from 'rxjs';
import { map } from 'rxjs/operators';
import { ACL_POLICY_CONTENT_TOKEN, ACL_STORE_ADAPTER_TOKEN } from '../tokens/public-api';
import { missingPolicyWarning } from './missing-policy-warning';

@Injectable()
export class AclService implements AclEvaluationInterface {
  currentPolicy$: ReplaySubject<AclRule[]> = new ReplaySubject<AclRule[]>(1);
  aclEngine: AclEngine;

  constructor(
    @Optional() @Inject(ACL_POLICY_CONTENT_TOKEN) injectedPolicyTxtContent?: string,
    @Optional() @Inject(ACL_STORE_ADAPTER_TOKEN) aclStoreAdapter?: ExpressionStoreAdapter
  ) {
    const initialPolicy = injectedPolicyTxtContent ?? '# empty';

    if (injectedPolicyTxtContent === undefined || injectedPolicyTxtContent === null) {
      missingPolicyWarning();
    }

    this.aclEngine = new AclEngine(aclStoreAdapter);
    this.aclEngine.setGlobalRulesFromPolicyTxtFile(initialPolicy);
    this.currentPolicy$.next(this.aclEngine.rules);
  }

  /**
   * Allows to completely swap the global rules for a new one.
   * This should only be used for development purposes, like from inside the ACL Inspector.
   * We might remove this from the public API at any time.
   */
  _setGlobalRules(rules: AclRule[]) {
    this.aclEngine._setGlobalRules(rules);
    this.currentPolicy$.next(this.aclEngine.rules);
  }

  /**
   * Makes it possible to inject new global acl rules during runtime.
   * This should only be used for development purposes, like the ACL Inspector.
   * We might remove this from the public API at any time.
   */
  _addGlobalRule(rule: AclRule) {
    this._setGlobalRules([rule, ...this.aclEngine.rules]);
  }

  addDynamicFormRules(formId: string, aclRules: AclRule[]) {
    this.aclEngine.setDynamicFormRules(formId, aclRules);
    this.currentPolicy$.next(this.aclEngine.rules);
  }

  removeDynamicFormRules(formId: string) {
    this.aclEngine.removeDynamicFormRules(formId);
    this.currentPolicy$.next(this.aclEngine.rules);
  }

  setEnvironmentValue(key: string, value: string) {
    this.aclEngine.setEnvironmentValue(key, value);
  }

  /**
   * @deprecated Use `aclService.isHidden$(aclPath).pipe(map((value) => !value))` instead
   */
  canShow$(aclPath: string) {
    return this.isHidden$(aclPath).pipe(map((value) => !value));
  }

  canEdit$(aclPath: string) {
    return this.aclEngine.canEdit$(aclPath);
  }

  /**
   * @deprecated Use `!aclService.isHidden(aclPath)` instead
   */
  canShow(aclPath: string) {
    return !this.isHidden(aclPath);
  }

  canEdit(aclPath: string) {
    return !this.aclEngine.canEdit(aclPath);
  }

  isReadonly$(aclPath: string): Observable<boolean> {
    return this.aclEngine.isReadonly$(aclPath);
  }

  isReadonly(aclPath: string): boolean {
    return this.aclEngine.isReadonly(aclPath);
  }

  isDisabled$(aclPath: string): Observable<boolean> {
    return this.aclEngine.isDisabled$(aclPath);
  }

  isDisabled(aclPath: string): boolean {
    return this.aclEngine.isDisabled(aclPath);
  }

  isHidden$(aclPath: string): Observable<boolean> {
    return this.aclEngine.isHidden$(aclPath);
  }

  isHidden(aclPath: string): boolean {
    return this.aclEngine.isHidden(aclPath);
  }

  currentState$(aclPath: string): Observable<AclRuleState> {
    return combineLatest([
      this.isHidden$(aclPath),
      this.isDisabled$(aclPath),
      this.isReadonly$(aclPath)
    ]).pipe(
      map(([isHidden, isDisabled, isReadonly]) => {
        if (isHidden) {
          return 'hidden';
        } else if (isDisabled) {
          return 'disabled';
        } else if (isReadonly) {
          return 'readonly';
        } else {
          return 'editable';
        }
      })
    );
  }

  get unscoped() {
    return this;
  }
}

results matching ""

    No results matching ""