Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/koajs/koa-roles
koa version of Connect-Roles
https://github.com/koajs/koa-roles
Last synced: 2 months ago
JSON representation
koa version of Connect-Roles
- Host: GitHub
- URL: https://github.com/koajs/koa-roles
- Owner: koajs
- License: other
- Created: 2014-03-27T12:24:09.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2019-07-03T18:59:13.000Z (over 5 years ago)
- Last Synced: 2024-04-14T13:08:40.312Z (8 months ago)
- Language: JavaScript
- Size: 52.7 KB
- Stars: 117
- Watchers: 5
- Forks: 10
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: History.md
- License: LICENSE.txt
Awesome Lists containing this project
- awesome-koa - koa-roles - koa version of Connect-Roles (Middleware)
- awesome-koa - koa-roles - Koa版本的[connect-roles](https://github.com/ForbesLindesay/connect-roles)。 ![](https://img.shields.io/github/stars/koajs/koa-roles.svg?style=social&label=Star) ![](https://img.shields.io/npm/dm/koa-roles.svg?style=flat-square) (仓库 / 中间件)
README
koa-roles
=======[![NPM version][npm-image]][npm-url]
[![build status][travis-image]][travis-url]
[![Test coverage][coveralls-image]][coveralls-url]
[![Gittip][gittip-image]][gittip-url]
[![David deps][david-image]][david-url]
[![npm download][download-image]][download-url][npm-image]: https://img.shields.io/npm/v/koa-roles.svg?style=flat-square
[npm-url]: https://npmjs.org/package/koa-roles
[travis-image]: https://img.shields.io/travis/koajs/koa-roles.svg?style=flat-square
[travis-url]: https://travis-ci.org/koajs/koa-roles
[coveralls-image]: https://img.shields.io/coveralls/koajs/koa-roles.svg?style=flat-square
[coveralls-url]: https://coveralls.io/r/koajs/koa-roles?branch=master
[gittip-image]: https://img.shields.io/gittip/fengmk2.svg?style=flat-square
[gittip-url]: https://www.gittip.com/fengmk2/
[david-image]: https://img.shields.io/david/koajs/koa-roles.svg?style=flat-square
[david-url]: https://david-dm.org/koajs/koa-roles
[download-image]: https://img.shields.io/npm/dm/koa-roles.svg?style=flat-square
[download-url]: https://npmjs.org/package/koa-roleskoa version of [connect-roles](https://github.com/ForbesLindesay/connect-roles)
## Install
```bash
$ npm install koa-roles
```## Usage
```js
const Roles = require('koa-roles');
const Koa = require('koa');
const Router = require('koa-router');const app = new Koa();
const router = new Router();
const user = new Roles({
async failureHandler(ctx, action) {
// optional function to customise code that runs when
// user fails authorisation
ctx.status = 403;
var t = ctx.accepts('json', 'html');
if (t === 'json') {
ctx.body = {
message: 'Access Denied - You don\'t have permission to: ' + action
};
} else if (t === 'html') {
ctx.render('access-denied', {action: action});
} else {
ctx.body = 'Access Denied - You don\'t have permission to: ' + action;
}
}
});app.use(user.middleware());
app.use(router.routes())
.use(router.allowedMethods());
// anonymous users can only access the home page
// returning false stops any more rules from being
// considered
user.use(async (ctx, action) => {
return ctx.user || action === 'access home page';
});// moderator users can access private page, but
// they might not be the only ones so we don't return
// false if the user isn't a moderator
user.use('access private page', ctx => {
if (ctx.user.role === 'moderator') {
return true;
}
})//admin users can access all pages
user.use((ctx, action) => {
if (ctx.user.role === 'admin') {
return true;
}
});router.get('/', user.can('access home page'), async ctx => {
await ctx.render('private');
});
router.get('/private', user.can('access private page'), async ctx => {
await ctx.render('private');
});
router.get('/admin', user.can('access admin page'), async ctx => {
await ctx.render('admin');
});app.listen(3000);
```## License
[MIT](LICENSE.txt)