.NET oData DataService 是否可以强制过滤子记录?
这应该是一个简单的场景 - 我有一个具有父/子关系的数据模型。例如,假设它是 Orders 和 OrderDetails - 1 Order ->许多订单详细信息。
我想使用标准 DataService 通过 oData 公开模型,但有一些限制。
首先,我应该只看到我的订单。使用 EntitySetRights.ReadSingle 和 QueryInterceptor 来确保订单确实是我的,这很简单。
到目前为止,一切都很好!但是,如何才能在 oData feed 中公开关联的 OrderDetail 记录,让我可以读取特定(读取单个)订单的 OrderDetails,而无需授予对整个 OrderDetails 表的访问权限?
换句话说,我想允许阅读我的详细信息
myUrl.com/OrderService.svc/Orders(5)/OrderDetails <-- Good! My order is #5
但不是每个人的详细信息
myUrl.com/OrderService.svc/OrderDetails <-- Danger, Scarry, Keep Out!
感谢您的帮助!
This should be a simple scenario - I have a data model with a parent/child relationship. For example's sake, let's say it's Orders and OrderDetails - 1 Order -> many OrderDetails.
I'd like to expose the model via oData using a standard DataService, but with a few limitations.
First, I should only see my Orders. That's simple enough using EntitySetRights.ReadSingle and a QueryInterceptor to make sure the order is in fact mine.
So far, so good! But how can the associated OrderDetail records be exposed in the oData feed in a way where I can read OrderDetails for a specific (read single) Order without giving access to the entire OrderDetails table?
In other words, I want to allow reading my details
myUrl.com/OrderService.svc/Orders(5)/OrderDetails <-- Good! My order is #5
but not everyone's details
myUrl.com/OrderService.svc/OrderDetails <-- Danger, Scarry, Keep Out!
Thanks for the help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这就是所谓的“遏制” - 您的示例在这里进行了准确描述:http://data.uservoice.com/forums/72027-wcf-data-services-feature-suggestions/suggestions/1012615-support-containment -hierarchical-models-in-odata?ref=title
WCF 数据服务尚不支持这种开箱即用的功能。
理论上可以使用自定义 LINQ 提供程序来实现此类限制。在您的 LINQ 实现中,您可以检测扩展(并不难)并在这种情况下允许它。但是您可以阻止对实体集本身的查询(也很容易识别)。有关 LINQ 表达式的更多详细信息,请参阅本系列:http://blogs.msdn.com/b/vitek/archive/2010/02/25/data-services-expressions-part-1-intro.aspx
这取决于您最初想要使用哪个提供商。如果您已经有自定义提供程序,那么这并不难。如果您有一个基于反射的提供程序,则可以将其分层。如果您有 EF,这可能会相当棘手(不确定是否可能)。
This is so called "containment" - your sample exactly described here: http://data.uservoice.com/forums/72027-wcf-data-services-feature-suggestions/suggestions/1012615-support-containment-hierarchical-models-in-odata?ref=title
WCF Data Services doesn't support this out of the box yet.
It is theoretically possible to implement such restriction with a custom LINQ provider. In your LINQ implementation you could detect the expansion (not that hard) and in that case allow it. But you could prevent queries to the entity set itself (also rather easy to recognize). For more details of how the LINQ expressions look plese refere to this series: http://blogs.msdn.com/b/vitek/archive/2010/02/25/data-services-expressions-part-1-intro.aspx
It depends on what provider you wanted to use originally. If you had a custom provider already this is not that hard. If you had a reflection based provider, it is possible to layer this on top. If you had EF, this might be rather tricky (not sure if it's even possible).