设计应用程序的最佳方法

发布于 2024-11-14 17:54:26 字数 658 浏览 8 评论 0原文

我有一个应用程序,其中有多个具有不同角色的用户。 每个用户对资源的访问都受到他所拥有的角色的限制。

现在,如果我正在构建一个应用程序来管理这些资源,我应该如何构建我的应用程序。

角色:管理员、版主、作者 资源:文章

我看到有两个选择。

1:为每个角色创建单独的模块,并授予该角色访问资源的权限 例如: 行政/ - 添加/编辑/删除文章 - 添加/编辑/删除用户 作者/ - 添加/编辑/删除自己的文章 主持人/ -编辑文章

2:为每个资源创建单独的模块,并根据访问它们的用户的角色授予对它们的访问权限。 例如:
articles/ 添加/编辑/删除 [根据用户角色管理操作]

语言是 PHP,我将使用 Zend 框架。我的问题与访问权限无关,因为 Zend 的 ACL 组件会处理它。我的问题是应用程序的组织。

例如,如果引入新用户,在第一种情况下我将必须创建一个新模块。但在第二种方法的情况下,我将不得不更新每个模块。

显然,第一种方法将涉及更多文件和不太复杂的逻辑 第二种方法需要更复杂的逻辑,但我根本不确定要遵循哪一种并寻求建议。任何帮助表示赞赏。

谢谢。

I have an application with multiple users with different roles.
Each users access to a resource is limited by the role he has.

Now if I am building an application to manage these resources how should I structure my application.

roles : admin, moderator, author
resources: articles

I see two options.

1: Create separate modules for each role with give access to resources for the role
eg:
admin/
-add/edit/delete article
-add/edit/delete user
author/
-add/edit/delete own articles
moderator/
-edit articles

2: Create separate modules for each resource and give access to them based on the role of the user accessing them.
eg:
articles/ add/edit/delete [manage operations depending on the user's role ]

The language is PHP and I will be using Zend framework. My issue is not about access permissions since Zend's ACL component takes care of it. My issue is with organization of the application.

For example if a new user is introduced, in the first case I will have to create a new module. But in the case of the second method, I will have to update each and every module.

It is obvious that first approach will involve more files and less complex logic
second approach will need more complex logic but I am simply not sure of which one to follow and looking for advice. Any help is appreciated.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

天冷不及心凉 2024-11-21 17:54:26

我认为一般方法是将访问逻辑与被访问的资源一起保留,而不是与访问资源一起保留。

在您的示例中,这意味着如果文章只想由指定用户编辑,那么文章的职责就是检查谁编辑它。

所有的逻辑都是这样的:

if user is article.owner or admin
    ...make things happen

如果您想添加新的用户角色或更改现有用户的行为,您只需要编辑这个条件,而不是创建另一个几乎相同的条件位不同的用户模块。

I think general approach would be to keep access logic with accessed resource, not with accessing one.

In your example it would mean that if article wants to be edited only by specified users, then it's article's business to check who edits it.

All of the logic would be something like:

if user is article.owner or admin
    ...make things happen

and if you ever want to add new user role or change behavior of existing ones, you only need to edit this one condition rather than create another pretty-much-the-same-yet-a-bit-different user module.

美人迟暮 2024-11-21 17:54:26

这实际上取决于您构建所有内容所使用的语言/平台。大多数平台已经内置了对安全性的支持。

这个答案适用于.NET。

不做任何特别的事情。使用内置安全模型(CAS = 代码访问安全)。

您可以使用某些属性来限制对方法的访问。如果您正在构建 MVC 应用程序,则可以使用 [Authorize] 属性,而在常规应用程序中,您可以使用 [PrinicpalPermission]

要使其正常工作,您需要为所有正在运行的线程分配一个主体 (System.Threading.Thread.CurrentPrincipal = new MyPrincipal(myUserInfo))。

ASP.NET 自动执行此操作,其他应用程序使用当前登录 (windows) 用户作为默认值。

It really depends on which language/platform you are building everything in. Most platforms already have built in support for security.

This answer is for .NET.

Do nothing special. Use the built in security model (CAS = Code Access Security).

You can limit the access to methods by using certain attributes. If you are building a MVC app you can use the [Authorize] attribute, while in regular apps you can use [PrinicpalPermission].

To get it working you need to assign a principal to all running threads (System.Threading.Thread.CurrentPrincipal = new MyPrincipal(myUserInfo)).

ASP.NET does this automatically, and other applications use the current logged in (windows)user as default.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文