sails-permissions getting all permissions

825 views Asked by At

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?

1

There are 1 answers

2
Somnath Muluk On BEST ANSWER
  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 .