带有 LINQ to SQL 的 ASP.NET MVC:如何实现对存储库中实体的授权?
考虑到领域驱动设计,您将如何在存储库中实现用户授权?具体来说,您将如何限制用户提供的登录名可以看到哪些数据?
假设我们有一个存储产品的电子商务商城,其中只有部分产品由任何给定的商店经理维护。在这种情况下,任何给定的登录名都不应看到所有产品。
问题:
- 您会选择存储库中的所有产品,然后使用过滤器来限制退回的产品吗?
像 GetProducts("keyword:boat").restrictBy("myusername") 一样?
- 您会从存储库中的控制器上下文中读取登录信息并被动地过滤结果吗?
- 您将如何存储用户角色与其可以访问的实体之间的关系?您是否会简单地将实体键和角色存储在多对多表中,并为每个商店经理可以访问的每种产品保留一条记录?
代码示例或代码示例的链接会很棒。谢谢。
With Domain Driven Design in mind, how would you implement user Authorization in a repository? Specifically, how would you restrict what data you can see by the user provided login?
Lets say we have an e-commerce Mall that stores products, where only some products are maintained by any given store manager. In this case, not all products should be seen by any given login.
Questions:
- Would you select all products in the Repo, then use a filter to restrict what products are returned?
Like GetProducts("keyword: boat").restrictBy("myusername")?
- Would you read the login from the controllercontext within the repository and filter results passively?
- How would you store the relation between a user role and what entities it could access? Would you simply store the entity key and the role in a many-to-many table, having one record for each product that each store manager could access?
Code examples or links to code examples would be fantastic. Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我采取的策略是在控制器操作上使用属性,该属性检查当前用户与所请求的实体之间的关系,然后根据查找结果允许或禁止该操作。我有几个不同的属性,具体取决于它是通过连接表还是具有直接关系。它使用反射来获取和检查值是否匹配,在我的例子中是数据上下文,但在您的存储库中。我将包含下面的代码(我已经做了一些努力来通用化,因此它可能无法编译)。请注意,您可以扩展它以包含一些权限概念(在连接表中)。
直接关系属性的代码。它验证当前用户是否是该记录的所有者(路由参数中指定的“id”属性与用户表中当前用户的id匹配)。
这是关联属性的代码,即路由参数中的id 和用户表中的用户id 在连接表中存在关联。请注意,存在对 VS2008 示例 中的 System.Linq.Dynamic 代码的依赖性。
The tack that I've taken is to use attributes on the controller action that examines the relation between the current user and the entity being requested, then either allows or disallows the action based on the results of the look up. I have a couple of different attributes depending on whether it goes through a join table or has a direct relationship. It uses reflection against, in my case the data context, but in yours the repository(ies) to get and check that the values match. I'll include the code below (which I've made some efforts to genericize so it may not compile). Note you could extend this to include some notion of permission as well (in the join table).
Code for the direct relationship attribute. It verifies that the current user is the owner of the record (the specified "id" attribute in the routing parameters matches the id of the current user in the user table).
This is the code for the association attribute, i.e., there exists an association in a join table between the id in the routing parameter and the user's id from the user table. Note that there is a dependency on the System.Linq.Dynamic code from the VS2008 Samples.
我在 类似问题 ="http://groups.google.com/group/sharp-architecture" rel="nofollow noreferrer">S#arp Architecture 新闻组,其中 Tom Cabanski 建议了一个 AOP 框架,例如 PostSharp 。这看起来是满足我需求的可行解决方案,因此我计划更深入地研究它。不幸的是,这并不是您问题的完整答案,因为我没有可以分享的代码示例。
I asked a very similar question on the S#arp Architecture newsgroup for which Tom Cabanski suggested an AOP framework, such as PostSharp. This looks like a workable solution to my needs, so I'm planning on taking a more in-depth look at it. Unfortunately, this isn't a complete answer to your question, as I don't have code examples to share.