What is the best way to manage permissions for a web application - bitmask or database table?

前端 未结 9 729
感动是毒
感动是毒 2020-12-22 19:06

I\'m considering the best way to design a permissions system for an \"admin\" web application. The application is likely to have many users, each of whom could be assigned a

相关标签:
9条回答
  • 2020-12-22 19:17

    I've done it both ways. But I don't use bit masks much anymore. A separate table would be fine that you can use as a cross reference, given a user id or a group id as a foreign key.

    UserID | Permission
    ===================
    1      | 1              1 representing manage users
    1      | 2              2 being manger products
    2      | 3 
    

    This way would be easier to maintain and add on to later on.

    I'd also use a separate table to manage what the permissions are.

    PermissionID | Description
    ==========================
    1            | Manage Users
    2            | Manager Products
    
    0 讨论(0)
  • 2020-12-22 19:23

    Permissions are usually key words with a 1, 0 or null (indicating inherit). With an bit system, you probably cannot create indexes on the user id and permission keyword; instead, you would have to scan every record to get the permission value.

    I would say go for the first option. It seems to me the better solution:

    create table permissions (
        user_id INT NOT Null,
        permission VARCHAR(255) NOT NULL,
        value TINYINT(1) NULL
    )
    alter table `permissions` ADD PRIMARY KEY ( `user_id` , `permission` ) 
    
    0 讨论(0)
  • 2020-12-22 19:27

    I'd suggest abstracting your web application permissions with the concept of a Role Provider. As of version 2.0, this is provided for you in .NET as System.Web.Security.RoleProvider.

    The basic idea is that you leverage an existing framework by writing your permission checks against the framework rather than a specific storage mechanism. You can then plug-in whatever storage mechanism is available, whether it's an XML file, a database, or even an authorization store using the Windows software Authorization Manager (which lets you seamlessly tie in your custom permissions to LDAP, as one example - no code required to configure).

    If you decide to use a database as a storage mechanism, several databases are supported for the automatic creation of the underlying tables that the framework needs. This includes running .NET on Mono and using the role provider model on top of MySQL.

    See Implementing a Role Provider for more information. It is entirely possible that other languages/environments also have libraries you could leverage to implement this concept - it would be worth looking into.

    EDIT: I should also point out the configuration of how your web application ties in to the storage mechanism is done through a web.config file, and doesn't require code changes. I have found this very useful to test a production version of a codebase on my local machine, using an XML file to mimic permissions instead of the normal database provider - all by modifying two lines in web.config.

    The other thing I forgot to mention is that you can plug-in your own custom providers by extending the base classes, allowing you to leverage the permission model but still use a proprietary storage system (eg. bit masks, if you really wanted to).

    0 讨论(0)
  • 2020-12-22 19:33

    I think it's a general rule of thumb to stay away from mystical bitstrings that encode the meaning of the universe.

    While perhaps clunkier, having a table of possible permissions, a table of users, and a link table between them is the best and clearest way to organize this. It also makes your queries and maintenance (especially for the new guy) a lot easier.

    0 讨论(0)
  • 2020-12-22 19:34

    Usually I have a Users table, a Roles table, and a UserRoles table. This way you can have an unlimited amount of roles without changing your db structure and users can be in multiple roles.

    I force the application to only authorize against roles (never users). Notice how the "id" column in the roles table is not an identity column. This is because you may need to control the IDs which get put in this table because your application is going to have to look for specific IDs.

    The structure looks like this:

    create table Users (
     id int identity not null,
     loginId varchar(30) not null,
     firstName varchar(50) not null,
     etc...
    )
    
    create table Roles (
     id int not null,
     name varchar(50) not null
    )
    
    create table UserRoles (
     userId int not null,
     roleId int not null
    )
    
    0 讨论(0)
  • 2020-12-22 19:35

    Can't comment because I am new to SO, but in regards to the accepted answer - a huge advantage that comes with this solution is the ability to universally handle permissions, instead of just if statements everywhere in the code, as well as special abilities such as allowing temporary permissions (permissions that have an expiration date)

    0 讨论(0)
提交回复
热议问题