Google Drive Example
This example demonstrates how to implement Relationship-Based Access Control (ReBAC) for a collaborative document platform similar to Google Drive.
The schema models hierarchical folder structures where files and folders can have different permission levels, and permissions inherit from parent folders.
Schema
Schema Components
Types
user: Individual users in the systemGroup: Groups of users that can be assigned permissions collectively (e.g., "Engineering Team", "Marketing Department")File: Individual files (e.g., documents, spreadsheets, presentations)Folder: Collections of files and subfolders that form a hierarchical structure
File Relations
Files support four relationship types:
owner: Full control over the file, including deletionwriter: Can edit the file content and metadatacommenter: Can add comments but cannot edit contentreader: Can only view the fileparent: Links the file to its parent folder
Folder Relations
Folders support the same four relationship types as files:
owner: Full control over the folder, including deletionwriter: Can add, remove, and modify files in the foldercommenter: Can comment on files in the folderreader: Can view files in the folderparent: Links the folder to its parent folder (enables nested folder structures)
Permission Hierarchy
The schema implements a hierarchical permission model with inheritance:
-
File Permissions:
can_delete_file: Only owners can delete filescan_access_historical_revisions: Writers, parent folder writers, or owners can view version historycan_modify_content: Writers or parent folder writers can edit file contentcan_modify_metadata: Writers or parent folder writers can change file propertiescan_add_comment: Commenters, parent folder commenters, or those who can modify contentcan_read: Readers, parent folder readers, or those who can modify contentcan_read_metadata: Readers or parent folder readers can view file properties
-
Folder Permissions:
can_delete_folder: Only owners can delete folderscan_share_files_from_folder: Writers, parent folder writers, or owners can share filescan_remove_files_from_folder: Writers, parent folder writers, or owners can remove filescan_add_files_to_folder: Writers, parent folder writers, or owners can add filescan_modify_metadata: Writers or parent folder writers can change folder propertiescan_read_items: Readers, parent folder readers, or those who can add filescan_read_metadata: Readers or parent folder readers can view folder properties
Use Cases
Personal Document Management
Scenario: A user organizes personal documents in a folder hierarchy (Work, Personal, Projects).
- User (Owner): Has full control over all folders and files
- Shared with Family (Reader): Family members can view files in the "Personal" folder but cannot edit
- Collaborator (Writer): A colleague can edit files in the "Work" folder
Example Relations:
Team Collaboration
Scenario: An engineering team collaborates on project documentation with nested folder structures.
- Team Lead (Owner): Owns the main project folder and all subfolders
- Engineering Team (Group - Writer): All team members can edit files in project folders
- Product Manager (Commenter): Can comment on files but cannot edit
- Stakeholders (Reader): Can view project files but cannot modify
Example Relations:
Enterprise Document Sharing
Scenario: A company manages documents across departments with strict access controls.
- Department Head (Owner): Owns department folders
- Department Members (Group - Writer): Can manage files in their department
- Cross-Department Collaborators (Writer): Specific users can edit files across departments
- External Partners (Reader): Limited read-only access to specific folders
Example Relations:
How It Works
Permission Inheritance
The schema uses hierarchical permission inheritance:
-
Folder → File: Permissions from the parent folder flow down to files
- If a user is a
writerof a folder, they cancan_modify_contenton all files in that folder - If a user is a
readerof a folder, they cancan_readall files in that folder
- If a user is a
-
Parent Folder → Child Folder: Permissions cascade through nested folders
- If a user is a
writerof a parent folder, they cancan_add_files_to_folderin child folders - Readers of parent folders can
can_read_itemsin child folders
- If a user is a
-
Direct File Relations: Files can have direct relations that complement folder permissions
- A file can have its own
commenterwho can comment even if not a commenter of the parent folder - A file can have its own
ownerwho has full control regardless of folder permissions
- A file can have its own
Access Control Flow
When checking if a user can perform an action on a file or folder:
- Check Direct Relations: First, check if the user has a direct relation to the file/folder
- Check Parent Folder: If no direct relation, check if the user has a relation to the parent folder
- Check Permissions: Evaluate permission expressions that may combine multiple relations
- Inherit from Parent: Parent folder permissions apply to all files and subfolders
Example: Checking Access
To check if user alice can read file project-plan.docx, you use the Descope SDK to check the can_read permission. The permission is defined as can_read: reader | parent.reader | can_modify_content, which means alice can read if she's a direct reader, a reader of the parent folder, or has permission to modify content.
Complete Implementation Example
Here's a complete example showing how to set up relations and check access for files and folders:
Implementation Considerations
- File Organization: When a new file is created, it must be linked to a folder via the
parentrelation - Permission Propagation: Changes to folder permissions automatically affect all files and subfolders
- Nested Folders: Folders can have parent folders, creating deep hierarchies
- User Groups: Use groups for team-based permissions (e.g., all "Engineering Team" members)
- Sharing Workflow: Implement sharing by creating relations between users/groups and files/folders
This schema provides a robust foundation for managing document access in collaborative platforms, from personal file management to enterprise document sharing systems.