Database schema for ACL

前端 未结 3 1126
梦如初夏
梦如初夏 2020-12-04 09:12

I want to create a schema for a ACL; however, I\'m torn between a couple of ways of implementing it.

I am pretty sure I don\'t want to deal with cascading permission

3条回答
  •  一向
    一向 (楼主)
    2020-12-04 09:36

    This means that I can exercise blatant disregard for data normalization as I will never have more than a couple hundred possible records.

    The number of rows you expect isn't a criterion for choosing which normal form to aim for. Normalization is concerned with data integrity. It generally increases data integrity by reducing redundancy.

    The real question to ask isn't "How many rows will I have?", but "How important is it for the database to always give me the right answers?" For a database that will be used to implement an ACL, I'd say "Pretty danged important."

    If anything, a low number of rows suggests you don't need to be concerned with performance, so 5NF should be an easy choice to make. You'll want to hit 5NF before you add any id numbers.

    A query to figure out if a user was allowed somewhere would look like this:

    SELECT id FROM resources WHERE name = ?
    SELECT * FROM permissions 
    WHERE role_id = ? AND resource_id = ? ($user_role_id, $resource->id)
    

    That you wrote that as two queries instead of using an inner join suggests that you might be in over your head. (That's an observation, not a criticism.)

    SELECT p.* 
    FROM permissions p
    INNER JOIN resources r ON (r.id = p.resource_id AND 
                               r.name = ?)
    

提交回复
热议问题