多个权限作为DB中的单个整数

时间:2013-03-04 12:32:16

标签: c# asp.net sql-server enums

我正在论坛上工作,我想在DB中保存用户权限。 我的想法是:

USER TABLE HAS: (besides name, login etc)
fk_role_id

ROLE TABLE HAS:
role_id
role_name
role_rights

然后我有Enum用户权限和用户类:

[Flags] public enum PermissionTypes : int {
    None = 0,
    Register = 1,
    ThreadCreate = 2,
    ThreadEdit = 4,
    ThreadDelete = 8,
    ReplyCreate = 16,
    ReplyEdit = 32,
    ReplyDelete = 64,
    CatCreate = 128,
    CatEdit = 256,
    CatDelete = 512,
    UserCreate = 1024,
    UserEdit = 2048,
    UserDelete = 4096,

    All = Register | ThreadCreate | ThreadEdit | ThreadDelete | ReplyCreate | ReplyEdit | ReplyDelete | CatCreate | CatEdit | CatDelete | UserCreate | UserEdit | UserDelete
}

public class User {
    public PermissionTypes Permissions = PermissionTypes.None;
}

现在可以将用户权限保存为role_rights中的int吗?并让他们再次离开?

更新:

只是为了澄清我的问题是什么...... FX。如果我有一个具有权限的用户:CatCreate& UserCreate,总共会给我:1152 ... 现在,我将如何使用该数字来测试用户拥有的权限?现在它是一个小数

1 个答案:

答案 0 :(得分:1)

如果您想检查某个值,例如“CatDelete”,请执行以下操作:Permissions & PermissionTypes.CatDelete == PermissionTypes.CatDelete

删除一个值:Permissions &= ~PermissionTypes.CatDelete

添加一个值:Permissions |= PermissionTypes.CatDelete