Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/charlesread/hapi-acl-auth-taglib

Taglib for Marko to be used in concert with hapi-acl-auth
https://github.com/charlesread/hapi-acl-auth-taglib

Last synced: 21 days ago
JSON representation

Taglib for Marko to be used in concert with hapi-acl-auth

Awesome Lists containing this project

README

        

# hapi-acl-auth-taglib

Taglib for Marko to be used in concert with [`hapi-acl-auth`](https://www.npmjs.com/package/hapi-acl-auth), or not. If you have an array of roles that a user possesses, and an array of roles the are necessary, you can use this taglib.

- [Installation](#installation)
- [Tags](#tags)
* [`haa:if`](#haaif)
+ [Attributes](#attributes)
- [`userRoles`](#userroles)
- [`roles`](#roles)
- [`mode`](#mode)
- [`not`](#not)
+ [Example](#example)
- [server.js](#serverjs)
- [index.marko](#indexmarko)

## Installation

```bash
npm install --save hapi-acl-auth-taglib
```

## Tags

### `haa:if`

Used to render, or not, the body of the tab based on which roles a user possesses and which are allowed, or not allowed.

#### Attributes

##### `userRoles`

Required. An array of roles that the user possesses.

##### `roles`

An array of roles required by the user needed to display the body of the tag.

##### `mode`

A string. Can be `any` or `all`, `any` by default. If `any` is specified the body will be rendered if the user has any of the roles listed in `roles`. If `all` the specified body will only be rendered if the user has _all_ of the roles in `roles`

##### `not`

An array of roles. The body of the tag will only be displayed if the user does _not_ have one of the roles specified here.

#### Example

##### server.js

```js
'use strict'

// allowing Marko template requires
require('marko/node-require')
require('marko/compiler').defaultOptions.writeToDisk = false

const Hapi = require('hapi')

const server = new Hapi.Server()

const plugins = [
{
register: require('hapi-auth-basic')
},
{
register: require('hapi-acl-auth'),
options: {
handler: function (request, callback) {
// callback(err, obj) takes an error object and an arbitrary object, although
// this object must contain a `roles` attribute that contains an array of
// roles, or a function that returns an array of roles or returns a promise
// that resolves an array of roles, that are possessed by the user
callback(null, request.auth.credentials)
}
}
}
]

const validateFunc = function (request, username, password, callback) {
callback(null, true, {id: username, username: username, roles: ['admin']})
}

server.connection({
host: 'localhost',
port: 8000
})

server.register(
plugins,
function (err) {
if (err) {
throw err
}
server.auth.strategy('simple', 'basic', {validateFunc: validateFunc})
server.route({
method: 'GET',
path: '/',
config: {
auth: 'simple',
plugins: {
hapiAclAuth: {
roles: ['user', 'admin']
}
}
},
handler: function (request, reply) {
const page = require('./index.marko')
return reply(page.stream(
{
userRoles: request.auth.credentials.roles
}
))
}
})

server.start((err) => {
if (err) {
throw err
}
console.log('Server running at:', server.info.uri)
})

}
)
```

##### index.marko

```html


/


/

some content

```