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.
|
|
_removeAllInspectorInjectedRules
|
_removeAllInspectorInjectedRules(rulePath: string)
|
|
|
Removes all global acl rules added by the ACL Inspector during runtime.
This should only be used for development purposes.
We might remove this from the public API at any time.
Parameters :
| Name |
Type |
Optional |
| rulePath |
string
|
No
|
|
|
_replaceInspectorInjectedRules
|
_replaceInspectorInjectedRules(rulePath: string, rules: AclRule[])
|
|
|
Replaces the inspector injected rules of a single path in one _setGlobalRules call,
avoiding the second full re-evaluation a remove-then-add would cause.
This should only be used for development purposes, like the ACL Inspector.
We might remove this from the public API at any time.
|
|
_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.
|
|
addDynamicFormRules
|
addDynamicFormRules(formId: string, aclRules: AclRule[])
|
|
|
|
|
|
canEdit
|
canEdit(aclPath: string)
|
|
|
Parameters :
| Name |
Type |
Optional |
| aclPath |
string
|
No
|
|
|
canEdit$
|
canEdit$(aclPath: string)
|
|
|
Parameters :
| Name |
Type |
Optional |
| aclPath |
string
|
No
|
|
|
canShow
|
|
Use `!aclService.isHidden(aclPath)` instead
|
canShow(aclPath: string)
|
|
|
Parameters :
| Name |
Type |
Optional |
| aclPath |
string
|
No
|
|
|
canShow$
|
|
Use `aclService.isHidden$(aclPath).pipe(map((value) => !value))` instead
|
canShow$(aclPath: string)
|
|
|
Parameters :
| Name |
Type |
Optional |
| aclPath |
string
|
No
|
|
|
currentState$
|
currentState$(aclPath: string)
|
|
|
Parameters :
| Name |
Type |
Optional |
| aclPath |
string
|
No
|
|
|
isDisabled
|
isDisabled(aclPath: string)
|
|
|
Parameters :
| Name |
Type |
Optional |
| aclPath |
string
|
No
|
|
|
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
|
|
|
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
|
|
|
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
|
|
|
setEnvironmentValue
|
setEnvironmentValue(key: string, value: string)
|
|
|
|
|
import {
AclEngine,
AclEvaluationInterface,
AclRule,
AclRuleState,
ExpressionStoreAdapter
} from '@allianz/taly-acl';
import { Injectable, inject } 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() {
const injectedPolicyTxtContent = inject<string>(ACL_POLICY_CONTENT_TOKEN, { optional: true });
const aclStoreAdapter =
inject<ExpressionStoreAdapter>(ACL_STORE_ADAPTER_TOKEN, {
optional: true
}) || undefined;
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]);
}
/**
* Removes all global acl rules added by the ACL Inspector during runtime.
* This should only be used for development purposes.
* We might remove this from the public API at any time.
*/
_removeAllInspectorInjectedRules(rulePath: string) {
this._setGlobalRules(this.rulesWithoutInspectorInjectedRules(rulePath));
}
/**
* Replaces the inspector injected rules of a single path in one `_setGlobalRules` call,
* avoiding the second full re-evaluation a remove-then-add would cause.
* This should only be used for development purposes, like the ACL Inspector.
* We might remove this from the public API at any time.
*/
_replaceInspectorInjectedRules(rulePath: string, rules: AclRule[]) {
this._setGlobalRules([...rules, ...this.rulesWithoutInspectorInjectedRules(rulePath)]);
}
private rulesWithoutInspectorInjectedRules(rulePath: string) {
return this.aclEngine.rules.filter((rule) => !(rule.path === rulePath && rule.inspectorRule));
}
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;
}
}