user_service.py


Overview

The user_service.py file provides service-layer classes that manage database operations related to users, tenants, and their relationships within the InfiniFlow application. It abstracts common CRUD and business logic for user authentication, tenant management, and user-tenant role assignments by building upon a base CommonService class and using the Peewee ORM for database interaction.

This file contains three main service classes:

Each class uses database models (User, Tenant, UserTenant) and integrates with the database connection context managed by Peewee.


Classes and Methods

Class: UserService

Service class focused on operations related to the User model.

Attributes:

Methods:

query(cls, cols=None, reverse=None, order_by=None, **kwargs)
filter_by_id(cls, user_id)
query_user(cls, email, password)
save(cls, **kwargs)
delete_user(cls, user_ids, update_user_dict)
update_user(cls, user_id, user_dict)
is_admin(cls, user_id)

Class: TenantService

Service class managing tenant-related operations.

Attributes:

Methods:

get_info_by(cls, user_id)
get_joined_tenants_by_user_id(cls, user_id)
decrease(cls, user_id, num)
user_gateway(cls, tenant_id)

Class: UserTenantService

Service class for managing user-tenant relationships.

Attributes:

Methods:

filter_by_id(cls, user_tenant_id)
save(cls, **kwargs)
get_by_tenant_id(cls, tenant_id)
get_tenants_by_user_id(cls, user_id)
get_num_members(cls, user_id)
filter_by_tenant_and_user_id(cls, tenant_id, user_id)

Important Implementation Details


Interaction with Other Parts of the System

This file acts as the service layer between API endpoints/controllers and the database, encapsulating domain logic for user and tenant management.


Visual Diagram

classDiagram
    class UserService {
        +model: User
        +query(cols, reverse, order_by, **kwargs)
        +filter_by_id(user_id)
        +query_user(email, password)
        +save(**kwargs)
        +delete_user(user_ids, update_user_dict)
        +update_user(user_id, user_dict)
        +is_admin(user_id)
    }

    class TenantService {
        +model: Tenant
        +get_info_by(user_id)
        +get_joined_tenants_by_user_id(user_id)
        +decrease(user_id, num)
        +user_gateway(tenant_id)
    }

    class UserTenantService {
        +model: UserTenant
        +filter_by_id(user_tenant_id)
        +save(**kwargs)
        +get_by_tenant_id(tenant_id)
        +get_tenants_by_user_id(user_id)
        +get_num_members(user_id)
        +filter_by_tenant_and_user_id(tenant_id, user_id)
    }

    UserService --|> CommonService
    TenantService --|> CommonService
    UserTenantService --|> CommonService

Summary

The user_service.py file is a critical service layer module in InfiniFlow that encapsulates user and tenant management logic, including authentication, role management, tenant credit operations, and user-tenant associations. It leverages Peewee ORM with transactional safety and integrates security best practices for password handling and user session validation. This modular design supports scalable and maintainable code for core identity and access management functionalities.


End of Documentation for user_service.py