权限设计模式
我想建立一个拥有几种不同类型用户的网站,例如:
- 管理员 - 可以在网站上执行所有
- 操作 注册用户 - 可以在其页面上执行所有操作。
- 未注册用户 - 只能查看网站。
是否有适合这种情况的设计模式?它将如何应用于我的场景?
I want to build a web site that has a few different kinds of users, e.g.:
- Administrator - that can do everything on the site
- Registered user - that can do everything on his page.
- Unregistered User - that can only view the website.
Is there a design pattern that is appropriate for this situation, and how would it apply to my scenario?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
设计模式并不是解决问题的灵丹妙药。它们是将合理的软件工程实践应用于代码设计的经过尝试和测试的方法。
Design patterns aren't a magic bullet for solving problems. They are tried and tested means of applying sensible software engineering practice to code design.
如果用户执行的每个操作都可以映射到 URI,那么您可以拥有一个安全层或使用可以配置 URI 到角色映射以允许访问的框架。
如果是在行为级别,您可能可以在这里使用代理设计模式。当您想要基于安全检查进行快速失败时,它将代理完整对象的实现。
但是,如果该对象对于不同用户的行为不同,则可以对行为不同的各种方法使用装饰器实现。
如果您想要获得对象的不同版本,该对象可能在结构上发生变化以及针对各种角色,则可以使用一系列访问者。此处可以应用访客模式。
If each operation a user carries out can be mapped to URI then you could have a security layer or use frameworks which can be configure with URI to Role mapping to allow access.
If it is at the behavioural level you can probably use the Proxy Design Pattern here. It would proxy the implementation of your full object when you want to fail-fast based on security check.
If however, the object behaves differently for different users, you could use decorator implementations for various methods which are to behave differently.
And if you are wanting to get a different version of the object that is possibly changed structurally as well for various roles than a series of Visitors could be used. A visitor pattern could be applicable here.
这是一种价值问题,但也许你可以看看状态设计模式。当用户只有读取权限时,您加载只读状态类。当用户具有写访问权限时,使用写状态类。
It is a kind of vaue question, but maybe you can look at the state design pattern. When a user has only read permission, you load the read-only state class. When a user has write access, use the write state class.