<!-- .github/pull_request_template.md --> ## Description Add return value of creating role and tenant, add detailed permissions example to Cognee ## Type of Change <!-- Please check the relevant option --> - [ ] Bug fix (non-breaking change that fixes an issue) - [x] New feature (non-breaking change that adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to change) - [ ] Documentation update - [ ] Code refactoring - [ ] Performance improvement - [ ] Other (please specify): ## Pre-submission Checklist <!-- Please check all boxes that apply before submitting your PR --> - [x] **I have tested my changes thoroughly before submitting this PR** - [x] **This PR contains minimal changes necessary to address the issue/feature** - [x] My code follows the project's coding standards and style guidelines - [x] I have added tests that prove my fix is effective or that my feature works - [x] I have added necessary documentation (if applicable) - [x] All new and existing tests pass - [x] I have searched existing PRs to ensure this change hasn't been submitted already - [x] I have linked any relevant issues in the description - [x] My commits have clear and descriptive messages ## DCO Affirmation I affirm that all code in every commit of this pull request conforms to the terms of the Topoteretes Developer Certificate of Origin. --------- Co-authored-by: Boris <boris@topoteretes.com> Co-authored-by: Hande <159312713+hande-k@users.noreply.github.com> Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
39 lines
1.3 KiB
Python
39 lines
1.3 KiB
Python
from uuid import UUID
|
|
from sqlalchemy.exc import IntegrityError
|
|
|
|
from cognee.infrastructure.databases.exceptions import EntityAlreadyExistsError
|
|
from cognee.infrastructure.databases.relational import get_relational_engine
|
|
from cognee.modules.users.models import Tenant
|
|
from cognee.modules.users.methods import get_user
|
|
|
|
|
|
async def create_tenant(tenant_name: str, user_id: UUID) -> UUID:
|
|
"""
|
|
Create a new tenant with the given name, for the user with the given id.
|
|
This user is the owner of the tenant.
|
|
Args:
|
|
tenant_name: Name of the new tenant.
|
|
user_id: Id of the user.
|
|
|
|
Returns:
|
|
None
|
|
"""
|
|
db_engine = get_relational_engine()
|
|
async with db_engine.get_async_session() as session:
|
|
try:
|
|
user = await get_user(user_id)
|
|
if user.tenant_id:
|
|
raise EntityAlreadyExistsError(
|
|
message="User already has a tenant. New tenant cannot be created."
|
|
)
|
|
|
|
tenant = Tenant(name=tenant_name, owner_id=user_id)
|
|
session.add(tenant)
|
|
await session.flush()
|
|
|
|
user.tenant_id = tenant.id
|
|
await session.merge(user)
|
|
await session.commit()
|
|
return tenant.id
|
|
except IntegrityError:
|
|
raise EntityAlreadyExistsError(message="Tenant already exists.")
|