Practical code snippets and scenarios for Cognee’s permission system
This guide provides practical code snippets demonstrating the permission system in action. These snippets show how to create users, tenants, roles, and datasets, and how to manage permissions effectively.
Complete snippets — All code snippets are complete and runnable, showing the full workflow from setup to permission management.
Creating a User
Users are the foundation of the permission system. Here’s how to create a new user:
Copy
Ask AI
from cognee.modules.users.methods import create_useruser = await create_user( email="alice@company.com", password="password123", is_superuser=True)
Creating a Tenant
Tenants group users together and can receive permissions. Create a tenant with an owner:
Copy
Ask AI
from cognee.modules.users.tenants.methods import create_tenant# Assuming user is already createdawait create_tenant("acme_corp", user.id)
from cognee.modules.users.tenants.methods import add_user_to_tenant# Assuming user2, tenant_id, and owner_id are already definedawait add_user_to_tenant(user2.id, tenant_id, owner_id)
from cognee.modules.users.roles.methods import create_role# Assuming owner_id is the tenant ownerawait create_role("editor", owner_id)
Creating a Dataset
Datasets are the core data containers. Create a dataset with automatic permissions for the creator:
Copy
Ask AI
from cognee.modules.data.methods import create_authorized_dataset# Assuming user is already createddataset = await create_authorized_dataset("project_docs", user)
Granting Read Permission
Grant specific permissions to principals. Give read access to a user:
Copy
Ask AI
from cognee.modules.users.permissions.methods import give_permission_on_dataset# Assuming user2 and dataset are already createdawait give_permission_on_dataset(user2, dataset.id, "read")
Granting Multiple Permissions
Grant different permission types to the same principal. Give comprehensive access:
Copy
Ask AI
from cognee.modules.users.permissions.methods import give_permission_on_dataset# Assuming user2 and dataset are already createdawait give_permission_on_dataset(user2, dataset.id, "read")await give_permission_on_dataset(user2, dataset.id, "write")await give_permission_on_dataset(user2, dataset.id, "delete")
Checking User Permissions
Query what datasets a user can access. Check permissions by type:
Copy
Ask AI
from cognee.modules.users.permissions.methods import get_all_user_permission_datasets# Assuming user is already created# Get all datasets user can readreadable_datasets = await get_all_user_permission_datasets(user, "read")# Get all datasets user can writewritable_datasets = await get_all_user_permission_datasets(user, "write")
Complete Permission Setup
Set up a complete permission scenario from scratch. This example shows the full workflow:
Demonstrate how permissions flow through the hierarchy. Show tenant and role inheritance:
Copy
Ask AI
from cognee.modules.users.permissions.methods import give_permission_on_dataset# Assuming tenant, role, and dataset are already created# Grant permission to tenant (all users inherit)await give_permission_on_dataset(tenant, dataset.id, "read")# Grant permission to role (role members inherit)await give_permission_on_dataset(role, dataset.id, "write")# User gets both: read (from tenant) + write (from role)
# Grant temporary access to external contractorcontractor = await create_user("contractor@external.com", "temp_password")# Grant read access to specific datasetawait give_permission_on_dataset(contractor, project_dataset.id, "read")# Later, revoke access by removing the permission# (This would require a revoke_permission function)
Cross-team Collaboration
Allow teams to collaborate on shared datasets:
Copy
Ask AI
# Allow teams to collaborate on shared datasetsshared_dataset = await create_authorized_dataset("shared_research", admin_user)# Grant different levels of access to different teamsawait give_permission_on_dataset(dev_role, shared_dataset.id, "read")await give_permission_on_dataset(research_role, shared_dataset.id, "write")await give_permission_on_dataset(management_role, shared_dataset.id, "read")
Best Practices
Follow these best practices for permission management:
Start simple — Begin with basic user and dataset creation
Use roles for teams — Create roles for different job functions
Grant tenant permissions — Use tenant-level permissions for organization-wide access
Regular audits — Periodically review and update permissions
Document access patterns — Keep clear records of who has access to what
Test permission changes — Verify permissions work as expected after changes