I have DB with few permission tables in form:
UserId, Object Id, lot of bit fields
I’ve added users groups to my DB and I need to update permissions to work with users and users groups. I think about 2 approaches.
-
Create copy of each permission table and have 2 tables for each object permission (user and group permission) – in each table I’ll have one foreign key to ‘permission owner’ tables (in one table – to users and in second table to user groups):
UserId NOT NULL, Object Id NOT NULL, lot of bit fields GroupId NOT NULL, Object Id NOT NULL, lot of bit fields -
Add one field (GroupId) to each permission table and use one of fields (UserId or GroupId) to identify if it is permission for group or user. So I’ll have table with 2 foreign keys – to users and user groups, but for each record only one of those FK will be used – other will be null.
Table could looks like this:UserId NULL, GroupId NULL, ObjectId NOT NULL, lot of bit fields
What is the best solution in your opinion? What are pros and cons of both? Are there other, better solution?
EDIT: I need to know what to do with foreign keys to users and groups, not with bit fields.
If the types of permissions that both sets can be granted are always the same, I’d probably keep it in the same table. But make sure you add a check constraint:
Or, have you considered modelling groups as users – either just modifying the
Userstable to accept groups directly, or having the Groups table (with “group only” columns) referencing theUserstable? It may be a simpler route to go down (depending on which other parts of your database need to only work with users, for example). Then all your permission checks can just be “Here’s a list of IDs (one of which is the user, the other are groups), please work out the aggregate permissions for this user”.