【发布时间】:2016-10-05 09:04:26
【问题描述】:
我想扩展一个模块(或创建我自己的)以自动将用户添加到撇号(aposUsersSafe 集合)。
我在 apostrophe-users 模块中没有看到任何用于执行此操作的内置方法,我正在寻找一些关于如何实现它的指导?谢谢!
【问题讨论】:
标签: apostrophe-cms
我想扩展一个模块(或创建我自己的)以自动将用户添加到撇号(aposUsersSafe 集合)。
我在 apostrophe-users 模块中没有看到任何用于执行此操作的内置方法,我正在寻找一些关于如何实现它的指导?谢谢!
【问题讨论】:
标签: apostrophe-cms
如前所述,我是 P'unk Avenue 的 Apostrophe 的主要建筑师。
aposUsersSafe 集合仅用于存储密码哈希和一些密切相关属性的非规范化副本。您通常永远不需要直接与它交互。与 Apostrophe 中的所有其他文档一样,用户位于 aposDocs 集合中。最好通过管理该类型片段的模块提供的方法与它们进行交互。在这种情况下,就是apos.users(apostrophe-users 模块)。
看看这个方法;这是从apostrophe-users 的addFromTask 方法轻松重构的,该方法实现了添加用户并将他们添加到组中,您几乎肯定也想这样做。
这里没有代码对密码进行哈希处理,因为apos.users 的insert 方法将为我们完成此操作。
self.addUser = function(req, username, password, groupname, callback) {
// find the group
return self.apos.groups.find(req, { title: groupname }).permission(false).toObject(function(err, group) {
if (err) {
return callback(err);
}
if (!group) {
return callback('That group does not exist.');
}
return self.apos.users.insert(req, {
username: username,
password: password,
title: username,
firstName: username,
groupIds: [ group._id ]
}, { permissions: false }, callback);
});
};
permission(false) 在光标上被调用,并且带有{ permissions: false } 的选项对象被传递给插入,因为我假设您希望在此时发生这种情况,而不管是谁触发它。
我建议reading this tutorial on Apostrophe's model layer 打下坚实的基础,了解如何使用 Apostrophe 的内容类型而不会遇到麻烦。你可以直接使用 MongoDB,但你必须知道什么时候该做,什么时候不该做。
插入用户时可以传递更多属性;这只是合理行为的最低限度。
至于调用方法,如果你要在construct里面的项目级别将它添加到lib/modules/apostrophe-users/index.js,那么你可以从中间件这样调用它:
return self.apos.users.addUser(req, username, password, groupname, function(err, newUser) {
if (err) {
// Handle the error as you see fit, one way is a 403 forbidden response
res.statusCode = 403;
return res.send('forbidden');
}
// newUser is the new user. You could log them in and redirect,
// with code I gave you elsewhere, or continue request:
return next();
});
希望对您有所帮助!
【讨论】:
middleware: [ function(req, res, next) { const email = //from cookie const auth = //from cookie if(email && auth) { //how do I invoke addUser here? //apostrophe-users.addUser(req, email, '', 'admin', null) } }不确定如何调用?