Skip to content

Commit 0af2b63

Browse files
committed
[fix] additional cases
1 parent 95b1827 commit 0af2b63

File tree

3 files changed

+20
-15
lines changed

3 files changed

+20
-15
lines changed

lib/utils.ts

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,20 @@ export const Utils = {
129129
* Get a Controller's method's policies
130130
*/
131131
getControllerPolicy(app: FabrixApp, handler, routeMethod, pre = [ ]) {
132+
const controller = Utils.getControllerFromHandler(handler)
133+
132134
if (app.config.get('policies.*.*')) {
133135
pre = [...new Set([...pre, ...Utils.stringToArray(app.config.get('policies.*.*'))])]
134136
}
135137
if (app.config.get(`policies.*.${routeMethod}`)) {
136138
pre = [...new Set([...pre, ...Utils.stringToArray(app.config.get(`policies.*.${routeMethod}`))])]
137139
}
140+
if (handler && controller && app.config.get(`policies.${controller}.*.*`)) {
141+
pre = [...new Set([...pre, ...Utils.stringToArray(app.config.get(`policies.${controller}.*.*`))])]
142+
}
143+
if (handler && controller && app.config.get(`policies.${controller}.*.${routeMethod}`)) {
144+
pre = [...new Set([...pre, ...Utils.stringToArray(app.config.get(`policies.${controller}.*.${routeMethod}`))])]
145+
}
138146
if (handler && app.config.get(`policies.${handler}.${routeMethod}`)) {
139147
pre = [...new Set([...pre, ...Utils.stringToArray(app.config.get(`policies.${handler}.${routeMethod}`))])]
140148
}
@@ -172,6 +180,10 @@ export const Utils = {
172180
return get(app.controllers, handler)
173181
},
174182

183+
getControllerFromHandler(handler) {
184+
return isString(handler) ? handler.split('.')[0] : handler
185+
},
186+
175187
/**
176188
* Get handler method from a controller.method string path
177189
*/
@@ -221,20 +233,6 @@ export const Utils = {
221233
})
222234
},
223235

224-
// /**
225-
// *
226-
// */
227-
// getControllerPolicyFromString(app: FabrixApp, handlerString: string) {
228-
// let pre = []
229-
// if (app.config.get('policies.*')) {
230-
// pre.push(Utils.policyStringToArray(app.config.get('policies.*')))
231-
// }
232-
// if (app.config.get(`policies.${handlerString}`)) {
233-
// pre = [...pre, ...Utils.policyStringToArray(app.config.get(`policies.${handlerString}`))]
234-
// }
235-
// return { pre: pre }
236-
// },
237-
238236
/**
239237
* Build a route collection
240238
*/

test/fixtures/app.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ module.exports = {
2929
},
3030
FooGetPolicy: class FooGetPolicy extends Policy {
3131
foo () { }
32+
},
33+
FooWildCardPolicy: class FooWildCardPolicy extends Policy {
34+
foo () { }
3235
}
3336
}
3437
},
@@ -52,6 +55,9 @@ module.exports = {
5255
'GET': ['GetPolicy.foo']
5356
},
5457
'TestController': {
58+
'*': {
59+
'*': ['FooWildCardPolicy.foo']
60+
},
5561
'foo': {
5662
'GET': ['FooGetPolicy.foo']
5763
}

test/integration/lib/util.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@ describe('lib.Util', () => {
110110
})
111111
assert.equal(route.GET.config.pre[0], global.app.policies.GlobalPolicy.foo)
112112
assert.equal(route.GET.config.pre[1], global.app.policies.GetPolicy.foo)
113-
assert.equal(route.GET.config.pre[2], global.app.policies.FooGetPolicy.foo)
113+
assert.equal(route.GET.config.pre[2], global.app.policies.FooWildCardPolicy.foo)
114+
assert.equal(route.GET.config.pre[3], global.app.policies.FooGetPolicy.foo)
114115
})
115116
})
116117
})

0 commit comments

Comments
 (0)