Permission design issues, ie C # implementation ideas [for discussion]

xiaoxiao2021-03-06  106

Now the program, many of the administrative issues involving permissions. A slightly large and government or business software involves many permissions, such as management, publishing, browsing, and receiving permissions such as announcements. What is the implementation on the database? I downloaded some software, and they implemented in the database, that is, each permission is set to 1 Boolean or byte field. I analyzed this design, the advantage is that it is convenient. If you want to know a permission, you can get a certain field, you don't need a lot of programs to judge. However, the disadvantage of this design is: 1. In maintenance, you need to increase or modify the permissions, you need to modify the database; If permissions change or cancel, the code will be modified in many places; 3, the field is very long, if there are dozens of rights, it will be more troublesome. After listening to your friends, consider using a field, use 01 to indicate whether you have this permission, permission bit bit setting: 100100101110110, each one represents a permission. Consider the need for future upgrade, etc., some reserved positions should be set. For example, a company requires 40 kinds of permissions, using 100 strings in design, such as: · Administrator: 12 kinds, set the top 20 bits · Post article: 20 kinds, set 30 bits · Work permission: 20 kinds are required, set up 30 digits ... The no used permission bit is all set to 0 so that we can decode.

Not finely said how this number is recorded, which is still more convenient in user management. I mainly want to say how to read: This 10011011101 ... It's hard to read, it seems to be a machine, there is no one, I want to give it an increase in some people in this C # More than previous ASP Too much, I thought of the encapsulation of the enumeration, structure, and class. The package core of this class should be read and decoding first with ENUM to list this permission, that is, give each bit (permission) take a nice name: Enum personpurview {isadmin, managemember, managearticle, ..... .,

ManagePReset1, ManagePreset2, ...,

Articlesend, ArticleView, .....,

ArticlePreset1, ArticlePreset2, ...}; of course, you can use the structure, but I found that it seems to be more convenient. For example, we have read the permissions fields from the database and use strpurview to represent this permission variable. This will write the function of judging the permissions: It is very important, but it is very simple: // Judging the function of having some permission public Bool HaspurView (PERSONPURVIEW, PURVIEW) {

IF (SRTPURVIEW.SUBSTRING (COVERT.TOINT32 (PurView), 1) == "1")

{

Return True;

}

Else

{

Return False;

}} This function can be used to judge, such as judging article issues (articlesenesend) {"HaspurView (PersonpurView.Articlesend) {

......;} how, you see it clear. Finally, I am still an idea, I haven't put into action. I also want to discuss it as a question, I think a good code is more important than the code.

转载请注明原文地址:https://www.9cbs.com/read-103445.html

New Post(0)