现在的程序,很多涉及到权限这个管理问题。一稍大和政务或商务软件都会涉及许多的权限,如管理、发表、浏览、收发公告等权限。
对这个问题,在数据库上怎样实现才好呢?



考虑以后的升级等需要,应设置一些预留位。比如一家公司需要40种权限,在设计时就用100位的字串来表示,如:
·管理员: 需要12种,设置前20位
·发表文章:需要20种,设置30位
·工作权限:需要20种,设置30位
......
没有用到的权限位全部设为0,

不细说这位数怎样记录,这在用户管理中还是比较方便的。我主要想说一下如何读取:

好在这C#比以前用ASP强太多了,就想到了用枚举、结构,用类的封装。该类的封装核心应该是读取和解码
首先用个enum来列举一下这个权限,也就是给每个位(权限)取个好听的名字:
enum PersonPurview {IsAdmin, ManageMember, ManageArticle, ......,
ManagePreSet1, ManagePreSet2, ......,
ArticleSend, ArticleView, .....,
ArticlePreSet1, ArticlePreSet2, ......};
当然你可以用结构,但我发觉用枚举似乎更方便。比如我们从数据库中已经读取了权限字段,并用strPurview来表示这个权限变量。
这样就可以先写判断权限的函数了:

// 判断拥有某种权限的函数
public bool HasPurview(PersonPurview, purview)
{
if ( srtPurview.SubString(Covert.ToInt32(purview), 1) == "1" )
{
return true;
}
else
{
return false;
}
}

if ( HasPurview(PersonPurview.ArticleSend) )
{
......;
}
怎么样,你看清楚了吧。
