2

I have a user model and a widget model. I have a relationship setup between user and widgets. I also have a policy defined for authentication, if the user is authenticated then I'm saving the user ID in the req.session.

The user is required on the widget:

attributes: {
user: {model: 'user', required: true}
}

is there some way to tell the WidgetController to use the user from the auth policy and still use all the default actions provided by blueprint?

matt
  • 103
  • 1
  • 7
  • possible duplicate of [How to add a default value from a function to the model in Sailsjs](http://stackoverflow.com/questions/26029948/how-to-add-a-default-value-from-a-function-to-the-model-in-sailsjs) – sgress454 Jan 06 '15 at 18:49

2 Answers2

3

As @MrVinz writes in his answer, policies are a great way to accomplish this. However, altering req.body or req.query isn't recommended, because you might want access to their original values. Instead, you can use req.options.values to provide defaults for blueprint values. For example, to default the name value to a logged-in user's name, you could create a policy similar to:

module.exports = function defaultNamePolicy (req, res, next) {

  // Make sure req.options.values is an object, and don't overwrite
  // values from prior policies
  req.options.values = req.options.values || {};

  // If there's a logged in user, default to using their name.      
  // Otherwise this will be undefined and will have no effect
  req.options.values.name = req.session.user && req.session.user.name

  return next();

}

The values in req.options.values are used as defaults for the request, so if req.param('name') exists, it will be used instead of req.options.values.name.

sgress454
  • 24,870
  • 4
  • 74
  • 92
1

Yes there is :) You can use a policy to achieve this :)

module.exports = function(req, res, next) {
                            if(req.session.user){
                              //then it depends on your implementation but 
                              req.params["widget"].user=req.session.user
                              //cab be req.query["widget"] or req.body["widget"]
                              /*
                                 if(req.params["widget"]){
                                   req.params["widget"].user=req.session.user   
                                 }
                                 if(req.query["widget"]){
                                   req.query ["widget].user=req.session.user
                                 }
                                 if(req.body["widget"]){
                                    req.body ["widget].user=req.session.user
                                 }
                              */
                            }
                            //other stuff
                            next();
                 }

you can also make this more generic by doing something like this

var model = req.options.model || req.options.controller;
if(model && sails.models[model].attributes['user']){
     //do the same trick
}

if you store a "user" full object in your session adapt the code and use a req.session.user.id

EDIT

My apologies i use custom blueprints and queries as i'am using sails ember blueprints :) however it makes things even easier you just have to check req.params["user"] or req.query["user"] if my memory is correct :)

MrVinz
  • 874
  • 6
  • 15
  • You *cannot* alter `req.params` in a policy. The original `params` object is re-applied to every middleware in the chain, so any changes you make in a policy won't be persisted to any controller action (or blueprint) that follows it. – sgress454 Jan 06 '15 at 18:20
  • @sgress454 good to know about options will have to change a bit my code. – MrVinz Jan 06 '15 at 21:28
  • @sgress454 btw can you provide any documentation reference to this statement i checked the ExpressJs API and sails doc on req and policies and i didn't find any statement about this – MrVinz Jan 06 '15 at 23:11
  • Yeah, it should be on [this page](http://sailsjs.org/#/documentation/reference/req) right between `req.method` and `req.param()`, but clearly it's not there. We'll get that added. There was [already a question about this on SO](http://stackoverflow.com/questions/26029948/how-to-add-a-default-value-from-a-function-to-the-model-in-sailsjs), though... – sgress454 Jan 06 '15 at 23:32
  • @sgress454 thanks for you clarification, but i would prioritize the `req.options` documentation (as there is no doc on this same page...) and about req.param well [there is another question on SO](http://stackoverflow.com/questions/18801661/modify-node-js-req-object-parameters) and an [open issue on git ...](https://github.com/balderdashy/sails/issues/1977) even if you closed [another one](https://github.com/balderdashy/sails/issues/1667) i've already voted up your answer hope it will get to a high rank ... so that it's become a reference ... – MrVinz Jan 07 '15 at 00:10