Understanding RBAC

September 25, 2023 · 659 words · 4 min

Role-based access control(RBAC) is a policy-neutral access control mechanism defined around roles and privileges. The components of RBAC such as role-permissions, user-role and role-role relationships make it simple to perform user assignments.

In this article, I’ll share some information about RBAC.

The basic idea of RBAC is to separate permission management from users to reduce management complexity and provider higher flexibility and security. By using roles as a middle layer, administrators can more easily manage user permissions without having to focus on each user’s permission settings.

Terminology

There are 3 terminologies in RBAC:

  • Role
  • Permisson
  • User

Role

  • Role is an abstract concept to define a set of related permission. For example, there are some roles in one system: administrator, normal user, guest, each role has a related permissions.
  • Roles gives users corresponding access rights by assigning permissions to them.
  • In some implementation, Roles can form hierachies to provider higher flexibility.

Permission

Permission is an ability to perform specific action or access specific resource.

  • Permission describes a requirement to access resource or action in a system.
  • There are 2 ways to define permission: allow and deny. Allow permission allows user to access a resource or perform an action, it likes a whitelist; Deny permission denied user to access a resource or perform an action, it likes a blacklist.

User

An user is an identity in a system, which could be assigned different roles to grant different permissions.

Implementation

It’s simple to implement a RBAC in a system. In the following content, I’ll provider a simple implementation.

Table Definitation

Role

column namecolumn typecolumn description
idintRole id
namevarchar(40)role name
descriptionvarchar(128)A brief description of the role
enabledbooleanwhether the role is enabled
created_atdatetimewhen the role is created

Permission

column namecolumn typecolumn description
idintpermission id
namevarchar(40)permission name
descriptionvarchar(128)A brief description of the permission
created_atdatetimewhen the permission is created

Role-Permission

An table represents what permissions the specific role has.

column namecolumn typecolumn description
role_idintRole id
permission_idintpermission id

User

User represents an identity who has the access to log in this system.

column namecolumn typecolumn description
idintuser id
usernamevarchar(20)-
passwordvarchar(255)A hashed password
namevarchar(20)-
created_atdatetimewhen the user is created

User Role

An table represents what roles the specific user has.

column namecolumn typecolumn description
user_idint
role_idInt

Example Data

Imaging you are designing a RBAC in a CRM system.

User

idusernamepasswordnamecreated_at
1staff1-staff12023-01-01 10:00:00
2manager1-manager12023-01-01 10:00:00

Role

idnamedescriptionenabledcreated_at
1salesman-true2023-01-01 10:00:00
2manager-true2023-01-01 10:00:00

Permission

idnamedescriptioncreated_at
1customer:create-2023-01-01 10:00:00
2customer:update-2023-01-01 10:00:00
3customer:delete-2023-01-01 10:00:00
4customer:view-2023-01-01 10:00:00

Role-Permission

The following data shows the salesman allows create/update/view a customer and the manager has the full access.

role_idpermission_id
11
12
14
21
22
23
24

User-Role

user_idrole_id
11
22

Sequence diagram

The following diagram shows an example of an user logged in and create a customer.

sequenceDiagram
User->>+LoginService: Logged in
LoginService->>+Database: Query user by username and password
Database-->>-LoginService: An user or empty
LoginService->>+Database: Query roles and permissions of current user
Database-->>-LoginService: Roles and permissions list
LoginService-->>+LoginService: add roles and permissions to session.
LoginService-->>-LoginService: add completed
LoginService-->>-User: Logged in
User-->>+CustomerService: create customer
CustomerService-->>+CustomerService: check whether there is a 'customer:create' permission in session
CustomerService-->>-CustomerService: found permission, allow operation
CustomerService-->>+Database: create a customer
Database-->>-CustomerService: create completed
CustomerService-->>-User: create succeed

Conclusion

Role-based access control (RBAC) has important application value in the field of access control. By associating permissions with roles, RBAC implements a structured access control management method. The implementation of RBAC can improve management efficiency and enhance system security.

References