Row-Level Authorization
Row-level authorization enables control over specific rows of an entity based on user roles, ownership, or custom logic. This feature is essential for applications that need fine-grained permissions.
Consider the following example:
import { Entity, Fields, remult, getEntityRef } from 'remult'
@Entity<Task>('tasks', { allowApiRead: true, allowApiInsert: remult.authenticated, allowApiDelete: 'admin', allowApiUpdate: (task) => remult.isAllowed('admin') || task.ownerId === remult.user?.id,})export class Task { @Fields.id() id = ''17 collapsed lines
@Fields.string({ required: true, }) title = ''
@Fields.boolean() completed = false
@Fields.createdAt() createdAt?: Date
@Fields.string({ allowApiUpdate: false, }) ownerId = remult.user?.id || ''}Understanding Each Authorization Option
-
allowApiRead: trueallowApiReadcontrols whether rows are accessible for reading through the API, and it defaults totrue, unlike other options that default tofalse.- Although you cannot use an arrow function with
allowApiReadto restrict specific rows, this can be achieved using row-level filters, which we’ll cover in the next lesson, “Filtering Rows Based on User Permissions”.
-
allowApiInsert: remult.authenticated- Restricts the ability to create new tasks to authenticated users. Any user who is not logged in will be denied insert access.
-
allowApiDelete: 'admin'- Limits deletion of tasks to users with the
adminrole, preventing other users from deleting tasks through the API.
- Limits deletion of tasks to users with the
-
allowApiUpdatewith Conditional Logic- The
allowApiUpdateoption here uses an arrow function to set conditional update access based on role and ownership:allowApiUpdate: (task) =>remult.isAllowed('admin') || task.ownerId === remult.user?.id, - This configuration allows:
- Admin users to update any task, and
- Non-admin users to update only their own tasks, identified by matching
task.ownerIdto the current user’s ID.
- Such logic provides flexibility for controlling access at a granular level, aligning permissions with both general access and specific ownership.
- The
Versatility Across Options
Each of these options—allowApiRead, allowApiInsert, allowApiDelete, and allowApiUpdate—can accept different inputs to define permissions:
- Boolean values (
trueorfalse) for universal access or denial. - Role strings or arrays of roles (e.g.,
'admin'or['admin', 'manager']) for access control based on user roles. - Arrow functions for
allowApiInsert,allowApiDelete, andallowApiUpdate, providing custom logic based on roles, user IDs, or specific row attributes.
In the upcoming lesson, “Filtering Rows Based on User Permissions”, we’ll explore how to apply row-level access control dynamically, allowing each user to view only the rows they are permitted to access using specific filters.
Files
Preparing Environment
- Installing dependencies
- Starting http server