-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from kozzztya/owner-rule-request
Owner rule request
- Loading branch information
Showing
17 changed files
with
150 additions
and
215 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
language: node_js | ||
node_js: | ||
- '4' | ||
- '6' | ||
services: | ||
- mongodb | ||
cache: yarn | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const _ = require('lodash'); | ||
|
||
module.exports = (adminRoles, payload) => { | ||
return _.some(adminRoles, (role) => _.includes(_.get(payload, 'roles'), role)); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,16 @@ | ||
const _ = require('lodash'); | ||
const httpError = require('http-errors'); | ||
const hasRole = require('../has-role'); | ||
|
||
module.exports = () => (payload, allow) => { | ||
const allowRolesList = _.get(allow, 'roles'); | ||
if (!allowRolesList) return Promise.resolve(true); | ||
|
||
const roles = _.get(payload, 'roles'); | ||
const isAllowed = _.some(roles, (role) => _.includes(allowRolesList, role)); | ||
if (!roles) return Promise.reject(httpError(403, 'No role.')); | ||
|
||
const isAllowed = hasRole(allowRolesList, payload); | ||
if (!isAllowed) return Promise.reject(httpError(403, 'Wrong roles: ' + roles)); | ||
|
||
return Promise.resolve(isAllowed); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
const { test, App } = require('../env'); | ||
|
||
const userId = '5901af327b35960019ee8b2e'; | ||
const config = [{ | ||
url: '/posts', method: 'POST', | ||
allow: {} | ||
}, { | ||
url: '/posts/:id', method: 'GET', | ||
allow: { owner: { ownerField: 'userId' } } | ||
}]; | ||
|
||
test('should be no error for owner', async (t) => { | ||
const app = App(config, {}, { userId }); | ||
const post = await app.post('/posts').send({ userId }); | ||
const { error } = await app.get('/posts/' + post.body._id); | ||
|
||
t.falsy(error); | ||
}); | ||
|
||
test('should be resolved for one of many owners', async (t) => { | ||
const config = [{ | ||
url: '/posts', method: 'POST', | ||
allow: {} | ||
}, { | ||
url: '/posts/:id', method: 'GET', | ||
allow: { owner: { ownerField: 'usersIds' } } | ||
}]; | ||
const app = App(config, {}, { userId }); | ||
const post = await app.post('/posts').send({ usersIds: [userId, '2', '3'] }); | ||
const { error } = await app.get('/posts/' + post.body._id); | ||
|
||
t.falsy(error); | ||
}); | ||
|
||
test('should be rejected for not owner', async (t) => { | ||
const app = App(config, {}, { userId: '2' }); | ||
const post = await app.post('/posts').send({ userId }); | ||
const { error } = await app.get('/posts/' + post.body._id); | ||
|
||
t.is(error.status, 403); | ||
t.is(error.text, 'No permissions.'); | ||
}); | ||
|
||
test('should be rejected if no userId field', async (t) => { | ||
const app = App(config, {}, {}); | ||
const post = await app.post('/posts').send({ userId }); | ||
const { error } = await app.get('/posts/' + post.body._id); | ||
|
||
t.is(error.status, 403); | ||
t.is(error.text, 'No user id.'); | ||
}); | ||
|
||
test('should be resolved if no rule', async (t) => { | ||
const config = [ | ||
{ url: '/posts', method: 'POST', allow: {} }, | ||
{ url: '/posts/:id', method: 'GET', allow: {} } | ||
]; | ||
const app = App(config, {}, {}); | ||
const post = await app.post('/posts').send({ userId }); | ||
const { error } = await app.get('/posts/' + post.body._id); | ||
|
||
t.falsy(error); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
const test = require('ava'); | ||
const lib = require('../../src'); | ||
const Supertest = require('supertest'); | ||
const express = require('express'); | ||
|
||
const app = express(); | ||
|
||
test('should work for express', async (t) => { | ||
const config = [{ url: '/blog', method: 'GET', allow: {} }]; | ||
const options = { denyNotAllowed: true }; | ||
|
||
lib(config, options)(app); | ||
app.get('/blog', (req, res) => { | ||
res.send({ message: 'hi' }); | ||
}); | ||
|
||
app.get('/secret', (req, res) => { | ||
res.send({ message: 'secret' }); | ||
}); | ||
|
||
const supertest = Supertest(app); | ||
|
||
const { error: getBlogError } = await supertest.get('/blog'); | ||
t.falsy(getBlogError); | ||
|
||
const { error: getSecretError } = await supertest.get('/secret'); | ||
t.truthy(getSecretError); | ||
}); |
Oops, something went wrong.