Comments (3)
Hi!
I spotted some problems in your implementation.
- Problem 1: you are obtaining the group from request.auth.credentials:
var roles = request.auth.credentials.group;
I believe that you don't really want to do this. hapi-rbac
reads this values for you when you specify credentials:group
in the target type. What you want to do instead, is retrieving the groups from your database (in your case from MySQL) that will match the request.auth.credentials.group
.
- Problem 2: You are configuring the policy as global. This policy will be applied to all your routes. I am not sure if that's what you want.
- Problem 3: You are passing a string instead of a JSON object to the callback.
So, try to do this instead:
server.register({
register: require('hapi-rbac'),
options: {
policy: function (request, callback) {
const path = request.route.path;
const method = request.route.method;
// Obtain the policy groups for the path and method (because this is a global configuration)
getPolicyGroupsFromMySQL(path, method, function (err, data) {
if(err) {
return callback(err);
}
// Assuming your data is an array of groups
// We add 'any-of' as first element
data.unshift('any-of');
const policy = {
target: data,
apply: 'permit-overrides',
rules: [
{
target: ['any-of',
{type: 'credentials:group', value: 'admin'}
],
effect: 'permit'
}
]
};
callback(null, policy);
});
}
}
}, function(err) {
if(err) {
throw err;
}
// load other plugins / start server...
});
from hapi-rbac.
Hello sir,
Thank you for your prompt reply which help me to resolve issue pending for more than a week. Appreciate your effort.
Thanks,
Subodh Sonawane.
From: Francisco Gouveia [mailto:[email protected]]
Sent: 11 February 2016 22:40
To: franciscogouveia/hapi-rbac
Cc: subodh2508
Subject: Re: [hapi-rbac] dynamic policy create (#19)
Hi!
I spotted some problems in your implementation.
- Problem 1: you are obtaining the group from request.auth.credentials:
var roles = request.auth.credentials.group;
I believe that you don't really want to do this. hapi-rbac reads this values for you when you specify credentials:group in the target type. What you want to do instead, is retrieving the groups from your database (in your case from MySQL) that will match the request.auth.credentials.group.
ยท Problem 2: You are configuring the policy as global. This policy will be applied to all your routes. I am not sure if that's what you want.
ยท Problem 3: You are passing a string instead of a JSON object to the callback.
So, try to do this instead:
server.register({
register: require('hapi-rbac'),
options: {
policy: function (request, callback) {
const path = request.route.path;
const method = request.route.method;
// Obtain the policy groups for the path and method (because this is a global configuration)
getPolicyGroupsFromMySQL(path, method, function (err, data) {
if(err) {
return callback(err);
}
// Assuming your data is an array of groups
// We add 'any-of' as first element
data.unshift('any-of');
const policy = {
target: data,
apply: 'permit-overrides',
rules: [
{
target: ['any-of',
{type: 'credentials:group', value: 'admin'}
],
effect: 'permit'
}
]
};
callback(null, policy);
});
}
}
}, function(err) {
if(err) {
throw err;
}
// load other plugins / start server...
});
โ
Reply to this email directly or view it on GitHub #19 (comment) .Description: Image removed by sender.
from hapi-rbac.
Great that it helped you. Closing the issue then.
from hapi-rbac.
Related Issues (20)
- [feature request]possibility of retrieving rbac rules via promise HOT 4
- Allow to evaluate more fields HOT 1
- Simplify target HOT 4
- Extract core HOT 4
- Increase test coverage
- Create own data retrievers
- Allow returning HTTP 403 (Forbidden) instead of only 401 HOT 3
- Test dict objects on target rules HOT 4
- Evaluation of nested properties HOT 1
- Match between sources HOT 2
- Update dependencies
- field level access control HOT 1
- Manage request headers in data retriever
- hapi-rbac on data collection
- Method of defining Role hierarchy? HOT 1
- Support Hapi v17?? HOT 2
- Is this project active? HOT 1
- Replace grunt with npm HOT 1
- Update dependencies HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from hapi-rbac.