jedireza / frame

:bulb: A user system API starter
https://jedireza.github.io/frame/
MIT License
739 stars 156 forks source link

Permission check #150

Closed BlackHawkNL closed 8 years ago

BlackHawkNL commented 8 years ago

Hi,

Im having some troubles with the permissions check, and cant figure out where its going wrong. For some reason when i check for permissions i keep getting the following error: TypeError: callback is not a function at hydrateGroups (/server/models/admin.js:115:13) at Async.auto (/server/models/admin.js:90:13)

I used the simple example thats in the wiki, and ofcourse made sure that the permission is assigned to a group and that the admin making the API call is assigned to that specific group.

Is there a work around for this? or can some one help me on the right direction.

Thanks, BlackHawk

jedireza commented 8 years ago

Thanks for opening an issue. Could you provide some code to demonstrate how to reproduce this error?

BlackHawkNL commented 8 years ago

Just a simple example

    server.route({
        method: 'GET',
        path: '/permcheck',
        config: {
            auth: {
                strategy: 'simple',
                scope: 'admin'
            }
        },
        handler: function(request, reply) {
            var credentials = request.auth.credentials;
            if (credentials.roles.admin.hasPermissionTo('adminarea')) {
                return reply(true);
            } else {
                reply(false);
            }
        }
    });
jedireza commented 8 years ago

Ah! We need to update that wiki page, hasPermissionTo requires a callback. Here is a working version of what you posted.

server.route({
    method: 'GET',
    path: '/permcheck',
    config: {
        auth: {
            strategy: 'simple',
            scope: 'admin'
        }
    },
    handler: function(request, reply) {

        const credentials = request.auth.credentials;

        credentials.roles.admin.hasPermissionTo('adminarea', (err, allowed) => {

            if (err) {
                // hydrating failed
            }

            reply(allowed);
        });
    }
});
jedireza commented 8 years ago

Wiki updated: https://github.com/jedireza/frame/wiki/Admin-&-Admin-Group-Permissions#checking-for-permission