8

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?

BeaverusIV
  • 988
  • 1
  • 11
  • 26

1 Answers1

4
  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 .

Somnath Muluk
  • 55,015
  • 38
  • 216
  • 226
  • Your answer works for getting the data for one Role but it doesn't say much about getting all permissions into a readable form. – BeaverusIV Oct 30 '15 at 20:46
  • 1
    Yes this can be done. @BeaverusIV. Giving you example to get all permissions. And I missed the bounty :( – Somnath Muluk Nov 02 '15 at 06:36