@policy({
name: 'effective policy',
action: 'Script',
weight: 1
})
myPolicy({ runtime }) {
return true
}
const { policy, route, log, transform } = require('decorators'),
{ Transform } = require('runtime.transform')
class RoutePolicies {
@policy
static redirectPolicy = {
name: 'c_redirect',
priority: 1,
methods: 'get',
paths: '/routes/test-policy-redirect',
action: 'Redirect',
redirectUrl: '/routes/test-policy-after-redirect',
weight: 1,
trace: true
}
@log({ traceError: true })
@route('POST test-route', { priority: 1 })
testRoute({ body }) {
return { text: 'Hi!', ...body() }
}
@log({ traceError: true })
@route('GET test-route-halt', { priority: 1 })
testHaltRoute() {
return 'Hello!'
}
@log({ trace: true })
@policy({ methods: ['post'], paths: '/routes/test-route', action: 'Script', trace: true, weight: 1 })
testRoutePolicy({ body }) {
if (body('end')) {
return 'ended!'
}
if (body('end_throw')) {
throw Fault.create('cortex.accessDenied.policy', { reason: 'Because!' })
}
if (body('end_response')) {
return require('response').end()
}
body('param', 'this is a param from policy')
}
@log({ trace: true })
@policy({ methods: ['get'], paths: '/routes/test-route-halt', priority: 1 })
routeHaltPolicy({ halt }) {
return halt()
}
@log({ trace: true })
@policy({ methods: ['get'], paths: '/routes/test-route-halt' })
routeHaltPolicy2({ halt }) {
const res = require('response')
res.setStatusCode(404)
res.setHeader('Content-Type', 'application/json')
res.end(JSON.stringify(new RangeError('no way, jesus maria!').toJSON()))
}
@route('GET get-all-accounts')
getAllAccounts({ req, res, body, runtime }) {
return org.objects.accounts.find().skipAcl(true).grant(8)
}
@policy
static accountsTransform = {
methods: ['get'],
paths: '/routes/get-all-accounts',
action: 'Transform',
priority: 999,
transform: 'c_accounts_transform'
}
}
@transform('c_accounts_transform')
class AccountsTransform extends Transform {
each(object) {
if (object.object === 'account' && object.email !== script.principal.email) {
object.name.first = '*******'
object.name.last = '*******'
}
return object
}
}