This page describes how row-level-access can be implemented in an AI Agent that uses SQL queries to retrieve data, such as when using Text-to-SQL and RAG.

Example setup

In order to apply permissions, the user id must be captured from the chat. Here’s an example setup in n8n:

Applying row-level permissions in queries

Row level access is implemented by providing the AI Agent with a set of views, making sure each view has a fixed column on which permissions can be applied (e.g. user_role).

The views are invoked with a filter (e.g. filter on user_role) and then prepended to the SQL query that the AI Agent wants to execute.

In the below example, we assume that users can have multiple roles, user roles have permissions on departments and that all resources are linked to one department.

Example description of the Data model in the System prompt of the AI Agent:

* Table **invoices**:     id, amount, date, department etc.
* Table **customers:**    id, name, department etc.
***** Table **transactions:** id, amount, department etc.
* etc.