简体   繁体   English

使Meteor方法调用在客户端上同步

[英]Making Meteor method calls synchronous on the client

I feel like I'm missing something. 我觉得我想念什么。 I was trying to tailor my product UI so that certain items are only displayed for admins. 我试图定制产品用户界面,以便某些项目仅向管理员显示。 In addition, restricting access to certain "pages" in the router requires a synchronous call guard. 另外,限制对路由器中某些“页面”的访问需要同步呼叫保护。 I'd certainly prefer to keep things asynchronous, but that doesn't seem to meet the requirements. 我当然希望保持异步,但这似乎不符合要求。

In other environments, I'd create a boolean function like isAdmin() that checks user authorization/roles. 在其他环境中,我将创建一个类似于isAdmin()的布尔函数来检查用户授权/角色。 But since users shouldn't see the implementation of the function for security reasons, it's best to use a server-only Meteor method. 但由于出于安全原因用户不应看到该功能的实现,因此最好使用仅服务器的Meteor方法。 Although the method could be synchronous on the client (if no callback specified), the only way to get the return value is to use the asynchronous callback form of Meteor.call(). 尽管该方法可以在客户端同步(如果未指定回调),但是获取返回值的唯一方法是使用Meteor.call()的异步回调形式。

It seems like there are three ways to handle this, none of which are as simple as the boolean function: 似乎有三种方法可以解决此问题,没有一种方法比布尔函数简单:

  1. Return an Observable from isAdmin(), but this pushes the issue out to wherever isAdmin() is called. 从isAdmin()返回一个Observable,但这会将问题推送到调用isAdmin()的任何地方。 It doesn't solve the router guard issue. 它不能解决路由器防护问题。
  2. Do a Meteor.call() wherever isAdmin() is needed, discarding the isAdmin() function. 在需要isAdmin()的任何地方执行Meteor.call(),丢弃isAdmin()函数。 This also pushes the problem to all the call sites, and doesn't address the router guard. 这也将问题推送到所有呼叫站点,并且没有解决路由器防护措施。
  3. Don't return a value at all, but have the server-side method throw an exception if the user is not an admin. 根本不返回任何值,但是如果用户不是管理员,则让服务器端方法引发异常。 This, however, doesn't change the asynchronous nature of the method call. 但是,这不会改变方法调用的异步性质。

I've seen examples using Meteor.wrapAsync or Future, but these use Fibers on the server, not the client. 我已经看到了使用Meteor.wrapAsync或Future的示例,但是这些示例在服务器而不是客户端上使用Fibers。

Any other suggestions for other patterns, perhaps using rxjs? 对于其他模式,还有其他建议,也许使用rxjs? Thanks. 谢谢。

i think i'm not understanding your overall security strategy, but here are a couple things to think about. 我认为我不了解您的总体安全策略,但是这里有几点需要考虑。

security happens on the server, which it looks like you're well aware of. 安全性发生在服务器上,您似乎很了解。

in my current project, we use roles ( alanning:roles ), and have several admin roles. 在我当前的项目中,我们使用角色( alanning:roles ),并具有多个管理员角色。 these roles are published with the Meteor.user object, so on the client we can enable/disable links to pages by checking that. 这些角色与Meteor.user对象一起发布,因此在客户端上,我们可以通过检查启用/禁用到页面的链接。

so we agree that doing so isn't real security, because a user can simply navigate to that link or change their client Meteor.user object to expose that. 因此我们同意这样做并不是真正的安全性,因为用户可以直接导航到该链接或更改其客户端Meteor.user对象以公开该链接。

but on that page, we're probably going to be accessing admin-only data. 但在该页面上,我们可能将访问仅管理员数据。 since we handle the publish on the server, where we can check those admin roles for real, we can detect a non-admin user access and throw an error. 由于我们处理服务器上的发布,因此可以在其中检查那些管理员角色的真实身份,因此我们可以检测到非管理员用户访问权限并引发错误。

similarly, if we're publishing data to a user, and the items they get are based on roles, we can check that in the publish and publish only those items they're allowed to see. 同样,如果我们要向用户发布数据,并且他们获得的项目是基于角色的,则可以在发布中检查并仅发布允许他们查看的项目。 same idea with restricting fields on those published items. 限制那些已发布项目的字段的相同想法。

to your question about "what does that client code look like that checks the roles?", it's really doing nothing more than querying the contents of the "roles" field on Meteor.user. 关于“该客户端代码看起来像什么来检查角色?”的问题,它实际上只是在查询Meteor.user上“角色”字段的内容而已。 so while it does give them a hint of what roles exist and how the app users them for visibility, imho it doesn't provide a path for mischief. 因此,尽管它确实向他们暗示了存在的角色以及应用如何使用它们来获得可见性,但是恕我直言,它并没有提供解决之道。 so long as they can't access any restricted data, or successfully execute any restricted calls, it's secure. 只要他们无法访问任何受限数据或成功执行任何受限调用,它都是安全的。

so for each of our publishers and Meteor methods, we have tons of error checking, including roles (admin and otherwise). 因此,对于我们的每个发布者和Meteor方法,我们都会进行大量错误检查,包括角色(管理员和其他角色)。 if anything doesn't line up, we throw an error. 如果没有排队,我们将抛出一个错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM