Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sails-permissions getting all permissions

I am trying to send all the permissions for an authenticated user via JSON from Sails.

My current code to find permissions for a single model type:

hasPermission: function hasPermission(req, res) {
    var permitted = PermissionService.isAllowedToPerformAction({
        method: req.param('method'),
        model: sails.models[req.param('model')],
        user: req.user
    });

    return res.json(200, { permitted: permitted });
}

This code doesn't work as isAllowedToPerformAction wants a single instance of a model. Is there a way to return a single JSON file accounting for all permissions?

like image 855
BeaverusIV Avatar asked Oct 21 '15 21:10

BeaverusIV


1 Answers

  1. Try creating roles and give them permissions.
  2. Assign role to users

Ex.

PermissionService.createRole({
   name: 'carsCategoryAdmin', 
   permissions: [
      { action: 'update', model: 'review', criteria: [{ where: { category: 'cars'}}]}, 
      { action: 'delete', model: 'review', criteria: [{ where: { category: 'cars'}}]}
   ],
   users: ['venise']
})

You can examine the role and related permissions and users,

Role.find({name:'carsCategoryAdmin'})
  .populate('users')
  .populate('permissions')
  .exec(console.log) 

See more @ sails-permissions-by-example

See how to get user permissions with code in comment given by skrichten on May 10, 2014 .

like image 196
Somnath Muluk Avatar answered Oct 08 '22 20:10

Somnath Muluk