2022-01-18 16:38:03 +08:00
|
|
|
import lodash from 'lodash';
|
2022-01-18 20:29:41 +08:00
|
|
|
import { ACL } from './acl';
|
2022-01-18 16:38:03 +08:00
|
|
|
type StrategyValue = false | '*' | string | string[];
|
|
|
|
|
|
|
|
export interface AvailableStrategyOptions {
|
2022-01-18 20:29:41 +08:00
|
|
|
acl: ACL;
|
2022-01-18 16:38:03 +08:00
|
|
|
displayName?: string;
|
|
|
|
actions: false | string | string[];
|
|
|
|
resource?: '*';
|
|
|
|
}
|
|
|
|
|
|
|
|
export function strategyValueMatched(strategy: StrategyValue, value: string) {
|
|
|
|
if (strategy === '*') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lodash.isString(strategy) && strategy === value) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lodash.isArray(strategy) && strategy.includes(value)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class ACLAvailableStrategy {
|
|
|
|
options: AvailableStrategyOptions;
|
|
|
|
|
|
|
|
constructor(options: AvailableStrategyOptions) {
|
|
|
|
this.options = options;
|
|
|
|
}
|
|
|
|
|
|
|
|
matchAction(actionName: string) {
|
|
|
|
return strategyValueMatched(this.options.actions, actionName);
|
|
|
|
}
|
|
|
|
|
|
|
|
allow(resourceName: string, actionName: string) {
|
2022-01-18 20:29:41 +08:00
|
|
|
return this.matchAction(this.options.acl.resolveActionAlias(actionName));
|
2022-01-18 16:38:03 +08:00
|
|
|
}
|
|
|
|
}
|