Giter Club home page Giter Club logo

Comments (3)

franciscogouveia avatar franciscogouveia commented on September 8, 2024

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.

subodh2508 avatar subodh2508 commented on September 8, 2024

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.

franciscogouveia avatar franciscogouveia commented on September 8, 2024

Great that it helped you. Closing the issue then.

from hapi-rbac.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.