如果您有管理员权限,是否可以在Meteor中设置其他用户的密码?

时间:2016-04-02 02:19:44

标签: meteor meteor-accounts change-password

我现在正试图在Meteor管理页面中设置另一个用户的密码。

这是我的代码。

Meteor.methods({
  updateUserPassword: function(userId, password) {
    var loggedInUser = Meteor.user()

    if (!loggedInUser ||
        !(Roles.userIsInRole(loggedInUser, ['admin'], 'default_group')) || (loggedInUser._id == userId) ) {
      throw new Meteor.Error(403, "Access denied")
    }

    return Accounts.setPassword(userId, password);
  }
});

但是当我运行此代码时,我得到Accounts.setPassword是未定义的错误。

我添加了帐户密码和基于帐户的软件包,但它仍然显示未定义的错误,因此我怀疑是否不再支持Accounts.setPassword。

请帮我解决这个问题!

1 个答案:

答案 0 :(得分:3)

Accounts.setPasswordMeteor仅服务器的功能。如果您在浏览器控制台中收到错误,那是因为您的updateUserPassword方法在lib/文件夹或类似的地方声明,并且客户端和服务器都可以访问侧的。

通常,Meteor.methods需要在lib/文件夹中声明,以便利用Meteor的Latency Compensation技术(也称为方法模拟)。

在您不喜欢的情况下,因为Accounts.setPassword 仅限服务器

解决方案1:

您可以使用Meteor.isClientMeteor.isServer来确定要在哪里运行的代码。 (您也可以使用this.isSimulation)。

Meteor.methods({
  updateUserPassword: function(userId, password) {
    var loggedInUser = Meteor.user()

    if (!loggedInUser ||
        !(Roles.userIsInRole(loggedInUser, ['admin'], 'default_group')) || (loggedInUser._id == userId) ) {
      throw new Meteor.Error(403, "Access denied")
    }

    if(Meteor.isServer) {
        return Accounts.setPassword(userId, password);
    } else if(Meteor.isClient) {
        // do something else
    }
  }
});

解决方案2:

您可以通过将文件放在仅服务器Meteor.methods文件夹中,或将整个server/声明放在{{1}中来声明服务器端的Meteor.methods检查。

当不需要延迟补偿时,应该使用它。