Skip to content

Commit

Permalink
refactor: use async function and support egg@2 (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
dead-horse committed Nov 23, 2017
1 parent 7517e28 commit 1919cc1
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 68 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
@@ -1,8 +1,8 @@
sudo: false
language: node_js
node_js:
- '6'
- '7'
- '8'
- '9'
install:
- npm i npminstall && npminstall
script:
Expand Down
34 changes: 18 additions & 16 deletions README.md
Expand Up @@ -45,26 +45,24 @@ exports.userrole = {
`Roles` build-in `failureHandler`:

```javascript
function failureHandler(action) {
function failureHandler(ctx, action) {
const message = 'Forbidden, required role: ' + action;
if (this.acceptJSON) {
this.body = {
if (ctx.acceptJSON) {
ctx.body = {
message: message,
stat: 'deny',
};
} else {
this.status = 403;
this.body = message;
ctx.status = 403;
ctx.body = message;
}
};
```

Build-in `user` role define:

```javascript
app.role.use('user', function() {
return !!this.user;
});
app.role.use('user', ctx => !!ctx.user);
```

### How to custom `failureHandler`
Expand All @@ -76,12 +74,12 @@ Define `app.role.failureHandler(action)` method in `config/role.js`
```javascript
// {app_root}/config/role.js or {framework_root}/config/role.js
module.exports = app => {
app.role.failureHandler = function(action) {
if (this.acceptJSON) {
this.body = { target: loginURL, stat: 'deny' };
app.role.failureHandler = function(ctx, action) {
if (ctx.acceptJSON) {
ctx.body = { target: loginURL, stat: 'deny' };
} else {
this.realStatus = 200;
this.redirect(loginURL);
ctx.realStatus = 200;
ctx.redirect(loginURL);
}
};
}
Expand All @@ -92,8 +90,13 @@ module.exports = app => {
```javascript
// {app_root}/config/role.js or {framework_root}/config/role.js
module.exports = function(app) {
app.role.use('admin', function() {
return this.user && this.user.isAdmin;
app.role.use('admin', ctx => {
return ctx.user && ctx.user.isAdmin;
});

app.role.use('can write', async ctx => {
const post = await ctx.service.post.fetch(ctx.request.body.id);
return ctx.user.name === post.author;
});
};
```
Expand All @@ -105,4 +108,3 @@ Please open an issue [here](https://github.com/eggjs/egg/issues).
## License

[MIT](https://github.com/eggjs/egg-userrole/blob/master/LICENSE)

6 changes: 3 additions & 3 deletions appveyor.yml
@@ -1,7 +1,7 @@
environment:
matrix:
- nodejs_version: '6'
- nodejs_version: '7'
- nodejs_version: '8'
- nodejs_version: '9'

install:
- ps: Install-Product node $env:nodejs_version
Expand All @@ -10,6 +10,6 @@ install:
test_script:
- node --version
- npm --version
- npm run ci
- npm run test

build: off
8 changes: 4 additions & 4 deletions config/config.default.js
@@ -1,11 +1,11 @@
'use strict';

exports.userrole = {
failureHandler(action) {
if (this.roleFailureHandler) {
return this.roleFailureHandler(action);
failureHandler(ctx, action) {
if (ctx.roleFailureHandler) {
return ctx.roleFailureHandler(action);
}
defaultFailureHandler(this, action);
defaultFailureHandler(ctx, action);
},
};

Expand Down
4 changes: 1 addition & 3 deletions config/role.js
@@ -1,7 +1,5 @@
'use strict';

module.exports = app => {
app.role.use('user', function() {
return !!this.user;
});
app.role.use('user', ctx => !!ctx.user);
};
20 changes: 10 additions & 10 deletions package.json
Expand Up @@ -15,19 +15,19 @@
"app.js"
],
"dependencies": {
"koa-roles": "^1.0.3"
"koa-roles": "^2.0.0"
},
"devDependencies": {
"autod": "^2.8.0",
"egg": "^1.4.0",
"egg-bin": "^3.4.2",
"egg-ci": "^1.7.0",
"egg-mock": "^3.7.1",
"eslint": "^3.19.0",
"eslint-config-egg": "^4.2.0"
"autod": "^3.0.1",
"egg": "^2.0.0",
"egg-bin": "^4.3.5",
"egg-ci": "^1.8.0",
"egg-mock": "^3.13.1",
"eslint": "^4.11.0",
"eslint-config-egg": "^5.1.1"
},
"engines": {
"node": ">=6.0.0"
"node": ">=8.0.0"
},
"scripts": {
"test": "npm run lint -- --fix && npm run test-local",
Expand All @@ -38,7 +38,7 @@
"autod": "autod"
},
"ci": {
"version": "6, 7"
"version": "8, 9"
},
"repository": {
"type": "git",
Expand Down
4 changes: 1 addition & 3 deletions test/fixtures/userrole/config/role.js
@@ -1,7 +1,5 @@
'use strict';

module.exports = function(app) {
app.role.use('admin', function() {
return this.user && this.user.isAdmin;
});
app.role.use('admin', ctx => ctx.user && ctx.user.isAdmin);
};
54 changes: 27 additions & 27 deletions test/userrole.test.js
Expand Up @@ -22,8 +22,8 @@ describe('test/lib/plugins/userrole.test.js', () => {
},
});
return app.httpRequest()
.get('/user?name=user2')
.expect(200, 'hello user2');
.get('/user?name=user2')
.expect(200, 'hello user2');
});

it('should GET /admin 200 when admin login', () => {
Expand All @@ -34,17 +34,17 @@ describe('test/lib/plugins/userrole.test.js', () => {
},
});
return app.httpRequest()
.get('/admin?name=suqian.yf')
.expect(200, 'hello admin');
.get('/admin?name=suqian.yf')
.expect(200, 'hello admin');
});

it('should GET /user 403 when user not login', () => {
app.mockContext({
user: null,
});
return app.httpRequest()
.get('/user')
.expect(403, 'Forbidden, required role: user');
.get('/user')
.expect(403, 'Forbidden, required role: user');
});

it('should GET /admin 403 when user is not admin', () => {
Expand All @@ -54,8 +54,8 @@ describe('test/lib/plugins/userrole.test.js', () => {
},
});
return app.httpRequest()
.get('/admin?name=user2')
.expect(403, 'Forbidden, required role: admin');
.get('/admin?name=user2')
.expect(403, 'Forbidden, required role: admin');
});

it('should get 403 with json format when accept json', () => {
Expand All @@ -65,39 +65,39 @@ describe('test/lib/plugins/userrole.test.js', () => {
},
});
return app.httpRequest()
.get('/admin?name=user2&ctoken=foo')
.set('Accept', 'application/json')
.set('Cookie', 'ctoken=foo')
.expect(403, {
message: 'Forbidden, required role: admin',
stat: 'deny',
});
.get('/admin?name=user2&ctoken=foo')
.set('Accept', 'application/json')
.set('Cookie', 'ctoken=foo')
.expect(403, {
message: 'Forbidden, required role: admin',
stat: 'deny',
});
});

it('should get 403 with json format when endsWith json', () => {
app.mockContext({
user: null,
});
return app.httpRequest()
.get('/user.json')
.expect(403, {
message: 'Forbidden, required role: user',
stat: 'deny',
});
.get('/user.json')
.expect(403, {
message: 'Forbidden, required role: user',
stat: 'deny',
});
});

it('should get 403 with json format and custom failureHandler', () => {
mm(app.role, 'failureHandler', function(action) {
this.status = 403;
this.body = { message: `Permission denied, required role: ${action}` };
mm(app.role, 'failureHandler', (ctx, action) => {
ctx.status = 403;
ctx.body = { message: `Permission denied, required role: ${action}` };
});
app.mockContext({
user: null,
});
return app.httpRequest()
.get('/user.json')
.expect(403, {
message: 'Permission denied, required role: user',
});
.get('/user.json')
.expect(403, {
message: 'Permission denied, required role: user',
});
});
});

0 comments on commit 1919cc1

Please sign in to comment.