Prompt
How do I make internal search respect permissions automatically?
Latest observation
To make internal search respect permissions automatically, the key is:
- Store permissions in a structured way
- Filter search results by the current user’s effective access
- Apply the same permission logic at index/query time, not just in the UI
Recommended approach
1) Attach ACL metadata to each searchable item
For every document/item in the search index, store fields like:
owner_idteam_idsrole_idsvisibility(public,private,org, etc.)permission_tagsoracl_principals
Example:
{
"id": "doc_123",
"title": "Q4 roadmap",
"content": "…",
"acl": ["user:42", "team:7", "role:manager"]
}
2) Compute the user’s effective permissions on login/request
Build a set of principals the user belongs to, e.g.:
["user:42", "team:7", "team:9", "role:manager", "org:acme"]
Then every search query uses this set to filter results.
3) Filter at query time
When searching, only return items where the user’s principals intersect with the document’s ACL.
Pseudo-query:
WHERE doc.acl INTERSECTS current_user_principals
OR doc.visibility = 'public'
If your search engine supports it, use native ACL filtering:
- Elasticsearch/OpenSearch: terms filter + boolean query
- PostgreSQL full-text search: join against permission tables
- Meilisearch/Typesense/etc.: filterable attributes
- Custom search: post-filter results, but this is less safe/performance-friendly
4) Enforce permissions server-side
Never rely on the frontend to hide unauthorized hits. The backend search endpoint must:
- authenticate the user
- derive permissions
- filter query results before returning them
5) Reindex when permissions change
If permissions are denormalized into the index, keep them updated when:
- sharing changes
- group membership changes
- roles change
A background job or event-driven sync usually works best.
Best practice patterns
Pattern A: ACL in the index
Good for most search systems.
- Fast
- Simple runtime filtering
- Must keep index in sync
Pattern B: Search IDs, then authorize results
Search returns candidate IDs, then app checks permissions per result.
- Safer if permissions are complex
- Can be slower
- Useful if you have a strong authorization service
Pattern C: Pre-filter by tenant/org, then ACL
Best for multi-tenant systems:
- tenant isolation first
- then user/team permissions inside tenant
Important pitfalls
- Don’t filter only in the UI
- Don’t search across all docs and remove unauthorized results later unless you must
- Make sure snippets/highlights also respect authorization
- Watch out for group membership changes causing stale access
- Test with edge cases: inherited roles, sharing links, document owners, admin overrides
If you want a concrete implementation
I can give you a version for:
- Elasticsearch/OpenSearch
- PostgreSQL full-text search
- MongoDB
- Meilisearch/Typesense
- A custom app with RBAC/ABAC
If you tell me your stack, I’ll show the exact query pattern.