Overview
Permissions are defined per Role and Collection. Each permission rule consists of:- Operation:
create,read,update,delete - Rule Expression: A logic string that evaluates to
true(allow) orfalse(deny) - Allowed Fields: A list of fields (or
*) that are accessible
Permission Resolution Order
SnackBase resolves permissions in the following order:- Role-Specific Rules: Rules for the user’s role on the specific collection
- Wildcard Collection Rules: Rules for the
*collection apply if no specific collection rule matches - Deny by Default: If no rule matches, access is denied
Superadmin Bypass
Users withaccount_id == "00000000-0000-0000-0000-000000000000" (superadmins) bypass ALL permission checks.
Rule Syntax
Variables
| Variable | Description | Fields |
|---|---|---|
user | The currently authenticated user | id, email, role, account_id, groups |
record | The record being accessed | All record fields (e.g., id, owner_id, status) |
account | The current account context | id |
Operators
| Category | Operator | Description | Example |
|---|---|---|---|
| Comparison | == | Equal to | user.id == record.owner_id |
!= | Not equal to | record.status != "archived" | |
< > | Less/Greater than | record.score > 10 | |
<= >= | Less/Greater or equal | record.amount >= 100 | |
in | Membership check | "admin" in user.groups | |
| Logical | and | Logical AND | user.isActive and record.public |
or | Logical OR | user.role == "admin" or record.public | |
not | Logical NOT | not record.is_locked |
Functions
| Function | Description | Example |
|---|---|---|
contains(list, item) | Checks if list contains item | contains(user.groups, "manager") |
starts_with(str, prefix) | Checks string prefix | starts_with(record.sku, "PROD-") |
ends_with(str, suffix) | Checks string suffix | ends_with(user.email, "@company.com") |
Literals
- Strings:
"text"or'text' - Numbers:
123,45.67 - Booleans:
true,false - Null/None:
null - Lists:
['a', 'b', 'c']
Built-in Macros
Built-in macros are predefined functions that simplify common permission patterns.@has_group(group_name)
Check if the user belongs to a specific group.@has_role(role_name)
Check if the user has a specific role.@owns_record() / @is_creator()
Check if the user owns the record.@in_time_range(start_hour, end_hour)
Check if the current time is within a specific hour range.@has_permission(operation, collection)
Check if the user has a specific permission on a collection.SQL Macros
SQL macros allow you to create custom permission logic using SQL queries.Creating SQL Macros
Using SQL Macros
SQL Macro Features
- Parameter Binding: Safe parameter binding to prevent SQL injection
- 5-Second Timeout: Queries are automatically terminated after 5 seconds
- Error Handling: Query errors return
false(deny) for security - Result Caching: Results are cached per-request for performance
System Fields
System fields are automatically managed by SnackBase and cannot be written via the API.| Field | Description |
|---|---|
id | Auto-generated record identifier |
account_id | Account/tenant identifier (auto-set) |
created_at | Record creation timestamp (auto-set) |
updated_at | Record update timestamp (auto-set) |
created_by | User ID who created the record (auto-set) |
updated_by | User ID who last updated the record |
Field-Level Access Control
Each permission rule can specify allowed fields:* to allow all fields (except system fields):
Field Filtering Behavior
| Context | System Fields | Behavior |
|---|---|---|
| Request | Excluded | Only allowed fields can be written |
| Response | Always Included | Allowed fields + system fields are returned |
Permission Caching
SnackBase uses a high-performance permission cache:- Default TTL: 5 minutes (300 seconds)
- Configurable: Set via
SNACKBASE_PERMISSION_CACHE_TTL_SECONDS - Automatic Invalidation: Cache is cleared when permissions change
Cache Key Format
user_123:posts:read
API Endpoints
Create Permission
List Permissions
Delete Permission
Common Patterns
1. Public Read Access
Allow anyone to read records, but only admins to modify.| Operation | Rule |
|---|---|
read | true |
create/update/delete | user.role == "admin" |
2. Owner-Only Access
Users can only manage their own data.| Operation | Rule |
|---|---|
create | true |
read/update/delete | user.id == record.created_by |
| Alternative Macro | @is_creator() |
3. Group-Based Access
| Operation | Rule |
|---|---|
read | "managers" in user.groups |
| Alternative Macro | @has_group("managers") |
4. Status-Based Workflow
Only allow updates if the record is in a specific state.5. Field-Level Restrictions
Role:employee
- Operation:
read - Rule:
user.id == record.user_id - Allowed Fields:
["id", "name", "department"](Excludesalary)
hr_manager
- Operation:
read - Rule:
true - Allowed Fields:
*(All fields)