UNPKG

1.3 kBJavaScriptView Raw
1/**
2 * @copyright Copyright (c) 2019 Maxim Khorin <maksimovichu@gmail.com>
3 */
4'use strict';
5
6const Base = require('../base/Behavior');
7
8module.exports = class ActionFilter extends Base {
9
10 constructor (config) {
11 super({
12 only: null, // ['action1', 'action2']
13 except: null, // ['action1', 'action2']
14 ...config
15 });
16 this.setHandler(Controller.EVENT_BEFORE_ACTION, this.beforeFilter);
17 this.setHandler(Controller.EVENT_AFTER_ACTION, this.afterFilter);
18 }
19
20 isActive (action) {
21 const name = this.owner instanceof Module
22 ? action.getRelativeModuleName()
23 : action.name;
24 return (!this.except || !this.except.includes(name))
25 && (!this.only || this.only.includes(name));
26 }
27
28 beforeFilter ({action}) {
29 this._active = this.isActive(action);
30 if (this._active) {
31 return this.beforeAction(action);
32 }
33 }
34
35 async afterFilter ({action}) {
36 if (this._active) {
37 return this.afterAction(action);
38 }
39 }
40
41 beforeAction (action) {
42 }
43
44 afterAction (action) {
45 }
46};
47
48const Module = require('../base/Module');
49const Controller = require('../base/Controller');
\No newline at end of file