I have users that fall into the following
- Not logged in
- Not Verified
- Verified
- Moderator
- Admin
All code that only admin and moderators can access (like banning) is in ModeratorUser which inherits from verified which inherits from BaseUser. Some pages are accessible to all users such as public profiles. If a user is logged in he can leave a comment. To check this i use if (IsVerifiedUser)
. Now here is the problem. To avoid problems if a user is banned he is not recognized as a verified user. However in the rare case i need to know if he is verified i can use usertype & Verified
.
Should i not be doing this? I have a bunch of code in my VerifiedUser class and find i am moving tons of it to BaseUser. Is this something i help because a not logged in user can access the page? Should i handle the ban user in a different way and allow IsVerifiedUser 开发者_JS百科to be true even if the user is banned?
At least in my opinion, most situations like this should be handled in data, not code. Hard-coding the fact that (for example) operation X can only be done by an administrator tends to be relatively brittle. Right now, you have five classes of users, but (just for example) you'll almost inevitably (somewhere along the line) end up inventing some other class of user, and have to re-organize quite a bit of code to fit (e.g. a new step halfway between moderator and admin, or perhaps a "restricted user" that's a step below a normal verified user, etc.) In fact, you're already basically running into that with your "banned" user who's mostly like an unverified user, but in a few ways like a verified user.
Having to rewrite the code every time you decide on a change like this is a poor idea. Instead, you should (probably) pre-define your five (or maybe six) user groups, and (for example) assign a bit to each. Likewise, assign a bit-mask to each function. To verify whether a given user can execute a given function, you AND those bitmasks together, and see whether the user has the appropriate bits set in their mask.
This makes it considerably easier to create new groups as needed, and/or change the assignments of rights to execute specific functions to groups of users. In particular, it allows changes in such rights to be done administratively rather than requiring a code rewrite.
精彩评论