A collection in R2R is a logical grouping of users and documents that allows for efficient access control and organization. Collections enable you to manage permissions and access to documents at a group level, rather than individually.R2R provides robust document collection management, allowing developers to implement efficient access control and organization of users and documents. This cookbook will guide you through the collection capabilities in R2R.For user authentication, please refer to the User Auth Cookbook.
Collection permissioning in R2R is still under development and as a result the is likely to API continue evolving in future releases.
A diagram showing user and collection management across r2r
Collections currently follow a flat hierarchy wherein superusers are responsible for management operations. This functionality will expand as development on R2R continues.
from r2r import R2RClientclient = R2RClient("http://localhost:7272") # Replace with your R2R deployment URL# Assuming you're logged in as an admin or a user with appropriate permissions# For testing, the default R2R implementation will grant superuser privileges to anon api callscollection_result = client.create_collection("Marketing Team", "Collection for marketing department")print(f"Collection creation result: {collection_result}")# {'results': {'collection_id': '123e4567-e89b-12d3-a456-426614174000', 'name': 'Marketing Team', 'description': 'Collection for marketing department', 'created_at': '2024-07-16T22:53:47.524794Z', 'updated_at': '2024-07-16T22:53:47.524794Z'}}
To add a user to a collection, you need both the user’s ID and the collections’s ID:
Copy
Ask AI
user_id = '456e789f-g01h-34i5-j678-901234567890' # This should be a valid user IDadd_user_result = client.add_user_to_collection(user_id, collection_id)print(f"Add user to collection result: {add_user_result}")# {'results': {'message': 'User successfully added to the collection'}}
document_id = '789g012j-k34l-56m7-n890-123456789012' # This should be a valid document IDassign_doc_result = client.assign_document_to_collection(document_id, collection_id)print(f"Assign document to collection result: {assign_doc_result}")# {'results': {'message': 'Document successfully assigned to the collection'}}
Many of the collection-related methods support pagination and filtering. Here are some examples:
Copy
Ask AI
# List collections with paginationpaginated_collection = client.list_collections(offset=10, limit=20)# Get users in a collection with paginationpaginated_users = client.get_users_in_collection(collection_id, offset=5, limit=10)# Get documents in a collection with paginationpaginated_docs = client.documents_in_collection(collection_id, offset=0, limit=50)# Get collections overview with specific collection IDsspecific_collections_overview = client.collections_overview(collection_ids=['id1', 'id2', 'id3'])
R2R’s collection permissioning system provides a foundation for implementing sophisticated access control in your applications. As the feature set evolves, more advanced capabilities will become available. Stay tuned to the R2R documentation for updates and new features related to collection permissions.For user authentication and individual user management, refer to the User Auth Cookbook. For more advanced use cases or custom implementations, consult the R2R documentation or reach out to the community for support.