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?
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.
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.
精彩评论