How to manage User Roles in a Database?

后端 未结 5 572
失恋的感觉
失恋的感觉 2020-12-22 22:25

I\'m creating a website in which I will be managing users and their permissions. I am looking to implement user roles, and can\'t seem to wrap my head around how things shou

5条回答
  •  青春惊慌失措
    2020-12-22 23:06

    I just don't know how I can link roles to several permissions.

    You use a join table: role_id and permission_id to identify what permissions are associated with which roles

    EDIT:

    Example tables

    ROLE Table

    Role_ID Role_Name
    1       Standard User
    2       Super User
    3       Guest
    

    PERMISSION Table

    Permission_ID Permission_Name
    1             View User List
    2             Update Own User Account
    3             Update Any User Account
    

    ROLE_PERMISSION Table

    Role_ID Permission_ID
    1       1    // Role 1 (Standard User) grants View User List
    1       2    //        and Update Own User Account
    2       1    // Role 2 (Super User) grants View User List,
    2       2    //        Update Own User Account,
    2       3    //        and Update Any User Account
    3       1    // Role 3 (Guest) grants View User List
    

    Listing the permissions for a specified Role_ID

    select R.role_id,
           P.permission_id,
           P.permission_name
      from role R,
           permission P,
           role_permission RP
     where RP.permission_id = P.permission_id
       and RP.role_id = R.role_id
       and R.role_id = 1 
    

提交回复
热议问题