自动将用户添加到撇号

Auto add users to Apostrophe

我想扩展一个模块(或创建我自己的模块)以自动将用户添加到 Apostrophe(aposUsersSafe 集合)。

我在 apostrophe-users 模块中没有看到任何用于执行此操作的内置方法,我正在寻找有关如何实现它的一些指导?谢谢!

如前所述,我是 P'unk Avenue 的 Apostrophe 的首席建筑师。

aposUsersSafe 集合仅用于存储密码哈希值和一些密切相关属性的非规范化副本。您通常永远不需要直接与它交互。与 Apostrophe 中的所有其他文档一样,用户生活在 aposDocs 集合中。最好通过管理该类型片段的模块提供的方法与它们进行交互。在这种情况下,那将是 apos.usersapostrophe-users 模块)。

看看这个方法;这是从 apostrophe-usersaddFromTask 方法轻微重构的,它实现了添加用户并将他们添加到组中,您几乎肯定也想这样做。

这里没有散列密码的代码,因为 apos.usersinsert 方法会为我们做这件事。

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 } 的选项对象被传递给 insert 因为我假设你希望这在此时发生而不管是谁触发它。

我建议 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();
});

希望对您有所帮助!