1 |
|
2 |
|
3 |
|
4 | 'use strict';
|
5 |
|
6 | const Base = require('../base/Behavior');
|
7 |
|
8 | module.exports = class ActionFilter extends Base {
|
9 |
|
10 | constructor (config) {
|
11 | super({
|
12 | only: null,
|
13 | except: null,
|
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 |
|
48 | const Module = require('../base/Module');
|
49 | const Controller = require('../base/Controller'); |
\ | No newline at end of file |