Added in v2.4.0.
Scenario
This article will demonstrate how to enforce role-based access control on a table named employees, which stores employee information including department and salary. The goal is to implement the following access rules:- The HR manager can view all employees in the HR department.
- The Engineering manager can view all employees in the Engineering department.
- The CEO can view all employees.
Step-by-step guide
-
Create the
employees
table to store employee data: -
Insert some sample data into the
employees
table: -
Create user accounts. For example, create three users:
hr_manager
,engineering_manager
, andceo
. -
Define logical views and grant access.
HR manager can only view employees in the HR department:
Engineering manager can only view employees in the Engineering department:View for HR managerThe CEO can view all employees:View for engineering managerView for CEO
-
Verify user access.
Now we can connect to the database as the user
hr_manager
to ensure that they can only query thehr_employee_view
and cannot access theemployees
table or other views. Connect ashr_manager
and test access:Query the HR view:Access to unauthorized data will be denied:
Related topics
- Learn how to define logical views, see CREATE VIEW.
- Learn more about security policies, see Access control.