问题
I want to use a hook to perform access control on a row so that users can only access rows they own. The hook would add an additional where clause to ensure that the user is the owner of the row (ex: WHERE ownerId=user.id).
The problem is, how do I pass the user id to Sequelize so that the hook has it when the hook fires? I've looked through the documentation and it's not obvious to me if this is possible.
Thanks
reference: http://sequelize.readthedocs.org/en/latest/docs/hooks/
回答1:
To add context you can include whatever you like in the options object. For example let's say you are using Model.findAll(). You would set up your options like this:
options = {
attributes: ['id', 'column1', 'column2'],
where: { someColumn: 'aValue' }
}
options.context = 'foo';
Then in the hook you'll be able to access options.context and use it however you like. Of course you probably should not use any of the "reserved" words used by Sequelize to filter queries.
来源:https://stackoverflow.com/questions/28449896/how-to-add-context-to-a-sequelize-hook