Access ✎
Allow or deny access on certain resources from your application, e.g.
// register extension
Grown.use(require('@grown/access'));
// overload definition
Grown('Access', {
access_filter: ctx => {
// retrieve role from the URL
const matches = ctx.req.url.match(/[&?]role=(\w+)/);
if (matches) {
return matches[1];
}
},
});
// setup rules
server.plug([
Grown.Access.rules({
roles: [
'Guest.User.Admin',
],
resources: {
// this rule will block all requests
Website: '/**',
// but only these requests will be allowed
Public: /^\/(?:login|logout|public)/,
},
permissions: {
Website: {
// User and its parents' roles get access too!
User: 'allow',
},
Public: 'allow',
},
}),
]);
// once Access is plugged on the server
// all new middleware gets protected by default
server.mount(ctx => {
ctx.res.write('You are welcome!');
// validate against undefined rules
return ctx.check('Foo', 'Bar', 'baz')
.catch(() => {
ctx.res.write('\nNot here...');
});
});
Click ▷ RUN above and then try different URLs like
/etc
or/login
below.
Methods mixin
check(role, resource[, action])
— Validate given rules through the current connection, returns a promise. If norole
is given it'll try to callaccess_filter
to retrieve one.
Public props static
resources
— Collected resources fromrules
calls.permissions
— Collected permissions fromrules
calls.
Public methods static
$install(ctx)
— Used byserver.plug
calls.$mixins()
— ExtraGrown.Conn.Builder
definitions.rules(config)
— Compile givenconfig
as access rules and returns a middleware.access_filter(ctx)
— When given, it'll be used as described oncheck
. It must be passed through patching the extension.
Private* props static
_groups
— Graph from collected roles._ruleset
— Collection of compiled rules.
Private* methods static
_reduceHandler(handler, permissions)
— Check ifhandler
exists withinpermissions
, returnsnull
otherwise._compileMatch(rule)
— Turns a singlerule
into a middleware callback._makeMatcher(ruleset)
— Iterates the givenruleset
and compile each one. It returns a middleware callback._makeTree(role, groups, property)
— Returns a flat representation of the givenrole
in thegroups
graph,property
can bechildren
orparent
._runACL(ctx, role, handlers)
— Validaterole
access throughctx
. Givenhandlers
should be an array of single resources and actions. It returns a promise.