I'm developing an application using Sails.js in the backend, and I'm having trouble validating requests as follows:
I want to block access to certain resources based on an attribute of the logged in user. I have the REST API blueprints enabled, and the shortcut routes disabled. I have the following piece of code:
User.findOne()
.where(query)
.exec(function(err, user) {
if (user.team !== req.user.team) {
return res.view('403');
}
return next();
});
where query
is the criteria by which I'd like to do the database search. The idea is that the user can only access the requested user if they're in the same "team".
The problem is that the user can make at least the following kinds of requests to the backend, maybe more (I'm not too experienced with sails, please enlighten me if there's even more types):
localhost:1337/user?id=1
In this case, the req
object will have a req.query
attribute, which I can pass on as query
as it is. Now, the following kind of request is also possible:
localhost:1337/user/1
Here req.query
will be an empty object, while req.params
is [ id: '1']
.
Now this is troublesome; if I understand correctly, the the type of req.params
isn't a JSON object, so I can't pass it as query
as it is. In addition, I'd have to convert the id
parameter into Int since it's originally a string for some reason (?).
What I'm asking is if there's a way I may have missed that handles both kinds of requests in the same way, or whether I'll have to take both cases into account in a way like
var query = isEmpty(req.query) ? req.params : req.query
in which case I'd have to convert req.params
into something I could pass to the database search and would generally be troublesome and inconvenient. I'd like to know what the best way to handle this situation is.