[PR #20176] feat: Implement multi-tenancy, multi-user, and quota management #29330

Closed
opened 2026-02-21 20:45:20 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/langgenius/dify/pull/20176

State: closed
Merged: No


This commit introduces a comprehensive set of features for multi-tenancy, multi-user management, and resource quota enforcement.

Key changes include:

  • Quota System Definition:

    • Added a tenant_quotas table and corresponding TenantQuota model to store quota limits for each tenant.
    • Defined a preliminary structure for quota plans (e.g., Free, Basic, Premium) although the plan definitions themselves are expected to be managed by an external billing system.
  • Billing System Integration (Mocked):

    • Extended BillingService with a get_tenant_quota method to fetch quota information (currently mocked).
    • The Tenant model now has a quota property to access this information.
  • Quota Enforcement:

    • Introduced a QuotaService to manage quota checks and handle overages.
    • Integrated quota checks into the creation/upload processes for:
      • Users (TenantService.create_tenant_member)
      • Documents (DatasetService.save_document_with_dataset_id, DocumentService.check_documents_upload_quota) - Apps (AppService.create_app) - Datasets (DatasetService.create_empty_dataset)
    • Added a QuotaExceededError custom exception.
  • Multi-User Management Enhancements:

    • Reviewed and confirmed that existing Role-Based Access Control (RBAC) is compatible with the new quota features.
    • Ensured that quota-related actions (like viewing usage) are restricted by user roles (owner/admin).
  • API Endpoint Updates:

    • Created a new API endpoint /billing/quota/usage for tenant owners/admins to view their current resource usage against their quotas.
    • Existing resource creation API endpoints now implicitly enforce quotas through service-level checks.
  • User Interface Updates:

    • Modified the billing page (web/app/components/billing/plan/index.tsx) to use a new UsageInfoContainer component.
    • The UsageInfoContainer (web/app/components/billing/usage-info/index.tsx) fetches and displays detailed quota usage (limits and current consumption) for various resources from the new API endpoint.
    • Updated relevant TypeScript types for UI components.
  • Testing:

    • Added unit tests for QuotaService covering various scenarios like quota checking, overage handling, and usage summary generation.
    • Added integration tests for the /billing/quota/usage API endpoint, verifying success cases, authentication, and authorization.
  • Documentation:

    • Prepared a comprehensive summary of changes required for the User Guide, API Documentation, and Developer Documentation to reflect the new features.

These changes provide a solid foundation for managing tenants, users, and resource quotas within the application, enhancing its capabilities for different subscription tiers and usage levels.

Summary

Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. List any dependencies that are required for this change.

Tip

Close issue syntax: Fixes #<issue number> or Resolves #<issue number>, see documentation for more details.

Screenshots

Before After
... ...

Checklist

Important

Please review the checklist below before submitting your pull request.

  • This change requires a documentation update, included: Dify Document
  • I understand that this PR may be closed in case there was no previous discussion or issues. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly.
  • I ran dev/reformat(backend) and cd web && npx lint-staged(frontend) to appease the lint gods
**Original Pull Request:** https://github.com/langgenius/dify/pull/20176 **State:** closed **Merged:** No --- This commit introduces a comprehensive set of features for multi-tenancy, multi-user management, and resource quota enforcement. Key changes include: - **Quota System Definition:** - Added a `tenant_quotas` table and corresponding `TenantQuota` model to store quota limits for each tenant. - Defined a preliminary structure for quota plans (e.g., Free, Basic, Premium) although the plan definitions themselves are expected to be managed by an external billing system. - **Billing System Integration (Mocked):** - Extended `BillingService` with a `get_tenant_quota` method to fetch quota information (currently mocked). - The `Tenant` model now has a `quota` property to access this information. - **Quota Enforcement:** - Introduced a `QuotaService` to manage quota checks and handle overages. - Integrated quota checks into the creation/upload processes for: - Users (`TenantService.create_tenant_member`) - Documents (`DatasetService.save_document_with_dataset_id`, `DocumentService.check_documents_upload_quota`) - Apps (`AppService.create_app`) - Datasets (`DatasetService.create_empty_dataset`) - Added a `QuotaExceededError` custom exception. - **Multi-User Management Enhancements:** - Reviewed and confirmed that existing Role-Based Access Control (RBAC) is compatible with the new quota features. - Ensured that quota-related actions (like viewing usage) are restricted by user roles (owner/admin). - **API Endpoint Updates:** - Created a new API endpoint `/billing/quota/usage` for tenant owners/admins to view their current resource usage against their quotas. - Existing resource creation API endpoints now implicitly enforce quotas through service-level checks. - **User Interface Updates:** - Modified the billing page (`web/app/components/billing/plan/index.tsx`) to use a new `UsageInfoContainer` component. - The `UsageInfoContainer` (`web/app/components/billing/usage-info/index.tsx`) fetches and displays detailed quota usage (limits and current consumption) for various resources from the new API endpoint. - Updated relevant TypeScript types for UI components. - **Testing:** - Added unit tests for `QuotaService` covering various scenarios like quota checking, overage handling, and usage summary generation. - Added integration tests for the `/billing/quota/usage` API endpoint, verifying success cases, authentication, and authorization. - **Documentation:** - Prepared a comprehensive summary of changes required for the User Guide, API Documentation, and Developer Documentation to reflect the new features. These changes provide a solid foundation for managing tenants, users, and resource quotas within the application, enhancing its capabilities for different subscription tiers and usage levels. # Summary Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. List any dependencies that are required for this change. > [!Tip] > Close issue syntax: `Fixes #<issue number>` or `Resolves #<issue number>`, see [documentation](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword) for more details. # Screenshots | Before | After | |--------|-------| | ... | ... | # Checklist > [!IMPORTANT] > Please review the checklist below before submitting your pull request. - [ ] This change requires a documentation update, included: [Dify Document](https://github.com/langgenius/dify-docs) - [x] I understand that this PR may be closed in case there was no previous discussion or issues. (This doesn't apply to typos!) - [x] I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change. - [x] I've updated the documentation accordingly. - [x] I ran `dev/reformat`(backend) and `cd web && npx lint-staged`(frontend) to appease the lint gods
yindo added the pull-request label 2026-02-21 20:45:20 -05:00
yindo closed this issue 2026-02-21 20:45:20 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#29330