我应该在我的代码中的什么地方放置我的 Meteor 用户方法和 allow() 权限?

Where in my code should I put my Meteor user methods and allow() permissions?

我正在使用 alanning:roles 并在我的服务器 - 主文件中成功地将我的用户放入管理员组。但是现在我正在尝试将 Meteor.users.allow() 添加到我的管理员中,以允许他们删除其他用户。我无法在我的代码中找到正确位置的示例来放置它。我是否将它放在启动时的主服务器中?或者在单独的用户集合中(我使用的是 React)?

我认为这说明了我对 meteor 或 react 理解的盲点,所以如果你觉得有启发性,请帮助我:) 谢谢!

如果您使用的 Meteor 比 1.3.0 更新,您可以将 hooks/allows/methods 文件放在 server 子目录下的任何位置,只要您 import(如ES6 模块导入)它们。从逻辑上将它们分开是有帮助的。 这是我们在项目中使用的示例目录结构:

public/ (static files, assets)
settings/ (to be loaded as command-line args for different environments)
test/
imports/
  client/
    startup/
    components/
    views/
  server/
    startup/
    allows/
    hooks/
    methods/
    publications/
  both/
    utils/
    collections/ (collections are here, because they're shared)

老实说,这是一个较旧的项目,因此此处未考虑 React,但这在您组织导入时仍然对您有所帮助。显然,您将需要为您的客户端和服务器导入所有必要依赖项的几个入口文件。 从那时起,例如,在您的 imports/server/allows/<collection_name>.js 文件中,您可以像这样放置允许:

import { SomeCollection } from '/imports/both/collections/someCollection.js';

SomeCollection.allow({
  insert: function () {
    return true;
  },
  update: function () {
    return true;
  },
  remove: function () {
    return true;
  }
});

我更喜欢在 Meteor 项目中使用绝对文件路径导入,因为根路径解析为项目的根目录。使它们更容易复制粘贴。

希望对您有所帮助。