-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
_req_user is undefined on pre/post remove hooks #1857
Comments
The admin ui adds @JedWatson are you interested in supporting UpdateHandler.prototype.remove = function(callback) {
if ('function' !== typeof callback) {
callback = function() {};
}
// Make current user available to pre/post remove events
this.item._req_user = this.user;
this.item.remove(callback);
}; Our deletes can be something like req.list.model.findById(req.query['delete']).exec(function (err, item) {
if (err || !item) return res.redirect('/keystone/' + req.list.path);
item.getUpdateHandler(req).remove(function (err) {
if (err) {
console.log('Error deleting ' + req.list.singular);
console.log(err);
req.flash('error', 'Error deleting the ' + req.list.singular + ': ' + err.message);
} else {
req.flash('success', req.list.singular + ' deleted successfully.');
}
return res.redirect('/keystone/' + req.list.path);
});
}); |
One workaround I have been using is to turn on tracking for the target model & use the updatedBy field to determine the user who made the changes. |
I could not get the
1 - You will need to install a couple of npm packagesnpm install --save continuation-local-storage cls-mongoose 2 - At the top of your
|
As I sent my previous answer I was thinking I might have just re-wrote So I just tried to code a simple middleware that does exports.saveUser = function (req :HTTPRequest, res :HTTPResponse, next :ExpressCallback) {
keystone.set('user', req.user);
next();
} and I could retrieve the user from the remove hook on MyModel.schema.pre('remove', function debugSessionUser (next) {
console.log(keystone.get('user'));
return next();
}) It took me a lot of tinkering around to finally use one of the most obvious keystone built-in... |
The question now is: In this case the continuation local storage might be safer. |
If you use update scripts, leveraging the updatedBy field will not work (it is unset in the context of an update script). I am now using @poksme 's solution. |
I'm sure the I'm proposing a session context object (fill like you wish) that will be passed to all hooks in #3187. I'll close this issue in its favor, please discuss requirements there. |
Was there actually a solution for this? Sorry just looked around and all issues are closed but I still have this issue. |
The following middlewares output an undefined
_req_user
:output:
I do not know if it is useful but here is some background about my project regarding this issue:
I am developing a website with some role limitation on the back-office. I am using the
_req_user
on pre save hook to limit modifications. But without the_req_user
on pre remove hook it is impossible to limit the remove rights.The text was updated successfully, but these errors were encountered: