Search…
Policy
1
@policy({
2
name: 'effective policy',
3
action: 'Script',
4
weight: 1
5
})
6
myPolicy({ runtime }) {
7
8
return true
9
10
}
Copied!

@policy(options)

Arguments
  • options { Object } Options object
    • name { String }
    • environment { String = "*" }
    • weight { Number = 0 }
    • action { String } Policy action (Script, Transform)

Method Options

  • methodOptions { Object } Options passed to the method
    • req { Object }
    • body { Object }
    • halt {Function} When called, haults the policy and exits the script
    • runtime { Object }
      • name { String }
      • environment { String = "*" }
      • weight { Number = 0 }
      • metadata { Object }
        • resource { String }
        • className { String }
        • methodName { String }
        • static { Boolean }
        • loc { Object }
          • line { String }
          • column { String }

Method Options (Transform Action)

  • methodOptions { Object } Options passed to the method
    • runtime { Object }
      • name { String }
      • environment { String = "*" }
      • weight { Number = 0 }
      • metadata { Object }
        • resource { String }
        • className { String }
        • methodName { String }
        • static { Boolean }
        • loc { Object }
          • line { String }
          • column { String }

Examples

1
const { policy, route, log, transform } = require('decorators'),
2
{ Transform } = require('runtime.transform')
3
4
class RoutePolicies {
5
6
@policy
7
static redirectPolicy = {
8
name: 'c_redirect',
9
priority: 1,
10
methods: 'get',
11
paths: '/routes/test-policy-redirect',
12
action: 'Redirect',
13
redirectUrl: '/routes/test-policy-after-redirect',
14
weight: 1,
15
trace: true
16
}
17
18
@log({ traceError: true })
19
@route('POST test-route', { priority: 1 })
20
testRoute({ body }) {
21
return { text: 'Hi!', ...body() }
22
}
23
24
@log({ traceError: true })
25
@route('GET test-route-halt', { priority: 1 })
26
testHaltRoute() {
27
return 'Hello!'
28
}
29
30
@log({ trace: true })
31
@policy({ methods: ['post'], paths: '/routes/test-route', action: 'Script', trace: true, weight: 1 })
32
testRoutePolicy({ body }) {
33
if (body('end')) {
34
return 'ended!'
35
}
36
if (body('end_throw')) {
37
throw Fault.create('cortex.accessDenied.policy', { reason: 'Because!' })
38
}
39
if (body('end_response')) {
40
return require('response').end()
41
}
42
body('param', 'this is a param from policy')
43
}
44
45
@log({ trace: true })
46
@policy({ methods: ['get'], paths: '/routes/test-route-halt', priority: 1 })
47
routeHaltPolicy({ halt }) {
48
return halt()
49
}
50
51
@log({ trace: true })
52
@policy({ methods: ['get'], paths: '/routes/test-route-halt' })
53
routeHaltPolicy2({ halt }) {
54
const res = require('response')
55
res.setStatusCode(404)
56
res.setHeader('Content-Type', 'application/json')
57
res.end(JSON.stringify(new RangeError('no way, jesus maria!').toJSON()))
58
}
59
60
@route('GET get-all-accounts')
61
getAllAccounts({ req, res, body, runtime }) {
62
return org.objects.accounts.find().skipAcl(true).grant(8)
63
}
64
65
@policy
66
static accountsTransform = {
67
methods: ['get'],
68
paths: '/routes/get-all-accounts',
69
action: 'Transform',
70
priority: 999,
71
transform: 'c_accounts_transform'
72
}
73
74
}
75
76
@transform('c_accounts_transform')
77
class AccountsTransform extends Transform {
78
79
each(object) {
80
if (object.object === 'account' && object.email !== script.principal.email) {
81
object.name.first = '*******'
82
object.name.last = '*******'
83
}
84
return object
85
}
86
87
}
Copied!
Last modified 2mo ago