我想扩展一个模块(或创建我自己的)以自动将用户添加到撇号(aposUsersSafe 集合)。
我在 apostrophe-users 模块中没有看到任何内置方法来执行此操作,并且正在寻找有关如何实现它的一些指导?谢谢!
我想扩展一个模块(或创建我自己的)以自动将用户添加到撇号(aposUsersSafe 集合)。
我在 apostrophe-users 模块中没有看到任何内置方法来执行此操作,并且正在寻找有关如何实现它的一些指导?谢谢!
如前所述,我是 P'unk Avenue 的 Apostrophe 的主要建筑师。
该aposUsersSafe
集合仅用于存储密码哈希和一些密切相关属性的非规范化副本。您通常永远不需要直接与它交互。与 Apostrophe 中的所有其他文档一样,用户住在aposDocs
集合中。最好通过管理该类型片段的模块提供的方法与它们进行交互。在这种情况下,那将是apos.users
(apostrophe-users
模块)。
看看这个方法;addFromTask
这是从 的方法中轻松重构的,该方法apostrophe-users
实现了添加用户并将它们添加到组中,您几乎肯定也会想要这样做。
这里没有代码来散列密码,因为 的insert
方法apos.users
将为我们做这件事。
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 }
传递给插入,因为我假设您希望此时发生这种情况,而不管是谁触发它。
我建议阅读有关 Apostrophe 的模型层的本教程,以便为如何使用 Apostrophe 的内容类型而不遇到麻烦打下坚实的基础。你可以直接使用 MongoDB,但你必须知道什么时候该做,什么时候不该做。
插入用户时可以传递更多属性;这只是合理行为的最低限度。
至于调用方法,如果你要lib/modules/apostrophe-users/index.js
在项目级别里面添加它construct
,那么你可以从中间件这样调用它:
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();
});
希望这有帮助!