cohere-toolkit
cohere-toolkit copied to clipboard
BE: Organization filters
- Filtering all entities with a foreign key to Organization by organization_id if the Organization-Id request header is set.
AI Description
This PR introduces changes to the backend, enhancing the system's ability to handle organizations and user groups.
Summary of Changes:
- Added a new function,
validate_organization_header, to validate requests containing theOrganization-Idheader. - Modified the
RouterNameclasses to includeDepends(validate_organization_header)in their dependencies. - Introduced a new class,
FilterFields, with an attributeORGANIZATION_ID, and a custom query class,CustomFilterQuery, to enable filtering by theorganization_idfield. - Updated the
get_organization_usersfunction to retrieve a list of users within a specified organization. - Adjustments to the
ContextMiddlewareclass to handle theOrganization-Idheader and set the global context. - Various test updates and additions to validate the functionality of organizations, user groups, and agents within organizations.
Detailed Code Changes:
New Code:
validate_organization_headerfunction inrequest_validators.pyto validate the presence of theOrganization-Idheader.FilterFieldsclass andCustomFilterQueryclass inbase.pyfor custom filtering capabilities.get_organization_usersfunction inorganization.pyto retrieve users within an organization.- Added
organization_idand related methods to theContextclass incontext.py. - Added handling for the
Organization-Idheader and global context management in theContextMiddlewareclass in `context.py. - Introduced organization-related factories and tests in
test_list_organization_agents,test_list_organization_agents_query_param, andtest_list_organization_agents_nonexistent_organizationintest_agent.py. - Added organization-related tests in
test_get_organization_conversation_listintest_conversation.py. - Implemented tests for listing organization users in
test_list_organization_usersintest_organization.py.
Modified Code:
- Modified
RouterNameclasses inrouters.pyto includeDepends(validate_organization_header)in their dependencies. - Updated the
get_sessionfunction indatabase.pyto useCustomFilterQuery. - Adjusted the
list_agentsfunction inagent.pyto handle theorganization_idfor filtering agents. - Modified the
get_organizationanddelete_organizationfunctions inorganization.pyto raise an exception if the "Organization not found" instead of "Model not found". - Changed the return type of
get_organization_usersfunction inorganization.pyto return a list ofUserinstead ofManagedTool. - Updated the
Contextclass incontext.pyto includeorganization_id,organization, and global filtering methods. - Modified the
sessionandsession_chatfunctions inconftest.pyto use `CustomFilterQuery. - Adjusted tests in
test_chat.pyto includetoolswhen creating an agent. - Updated tests in
test_conversation.pyto includeorganization_idwhen creating conversations. - Modified factories in
agent.pyandconversation.pyto includeorganization_id.
Removed Code:
- None
Codecov Report
Attention: Patch coverage is 85.23490% with 22 lines in your changes missing coverage. Please review.
Please upload report for BASE (
main@dbc5b7b). Learn more about missing BASE report.
Additional details and impacted files
@@ Coverage Diff @@
## main #706 +/- ##
=======================================
Coverage ? 75.00%
=======================================
Files ? 262
Lines ? 11656
Branches ? 0
=======================================
Hits ? 8743
Misses ? 2913
Partials ? 0
:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.
Can you give a bit more context about this change? Is security relevant or will all users be able to access every organisation? Because if not I can easily change the organisation id in the header? Wouldn't it be better to just fetch it from the authenticated user?
Can you give a bit more context about this change? Is security relevant or will all users be able to access every organisation? Because if not I can easily change the organisation id in the header? Wouldn't it be better to just fetch it from the authenticated user?
For now, we do not have any restrictions related to organizations. However, I agree that we need to implement them in the future.