Last active
August 29, 2015 14:24
-
-
Save robksawyer/5481b23af2d1e8cbb166 to your computer and use it in GitHub Desktop.
A policy that checks various controllers to see if the user accessing is the actual owner.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * isOwner Policy | |
| * | |
| * Policy for authorizing user requests if the current authenticated requesting user is the owner of the record. | |
| * | |
| * @param {Object} req | |
| * @param {Object} res | |
| * @param {Function} next | |
| */ | |
| var changeCase = require('change-case'); | |
| module.exports = function (req, res, next) { | |
| if (req.session.authenticated) { | |
| //Check to see if an id is passed along | |
| //Check the controller that the user is trying to access | |
| var modelName = req.options.controller; | |
| var model = sails.models[modelName]; | |
| if(typeof model !== 'undefined' && typeof req.param('id') !== 'undefined') { | |
| AuthorizationService.isOwner(model, req.param('id'), req.user.id).then( | |
| function (res) { | |
| return next(); | |
| }, | |
| function (err){ | |
| return res.forbidden('You are not permitted to perform this action.'); | |
| } | |
| ) | |
| } else { | |
| return res.forbidden('You are not permitted to perform this action.'); | |
| } | |
| } else { | |
| // User is not allowed | |
| // (default res.forbidden() behavior can be overridden in `config/403.js`) | |
| return res.forbidden('You are not permitted to perform this action.'); | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment