Menu
Introduction
Getting Started
Developer guide
Release
Architecture
CLI
Technical documentations
Version
Publication date

Mar 9, 2022

Confidentiality
Public
Reactions
1
Share

UserSyncService

Abstract base class for synchronizing users from gws_core database to custom databases.

This service extends EventListener to automatically sync users when user events occur. It provides both synchronization logic and event handling in a single all-in-one class.

IMPORTANT: When creating your implementation, you must decorate it with @event_listener to register it with the event system.

Type Parameters: TCustomUser: The type of the custom user model in the target database (must extend Model)

Example: from gws_core import User as GwsCoreUser from gws_core import event_listener

@event_listener
class ProjectUserSyncService(UserSyncService[ProjectUser]):
    def get_user_type(self) -> Type[ProjectUser]:
        return ProjectUser

    def update_custom_user_attributes(self, custom_user: ProjectUser, gws_core_user: User) -> ProjectUser:
        # Only update attributes, do not save
        custom_user.email = gws_core_user.email
        custom_user.first_name = gws_core_user.first_name
        # ... update other fields
        return custom_user
Functions
from_gws_core_user

Create a custom user object with data from a gws_core user.

This method should copy all relevant fields from the gws_core user to the custom user object and return the updated object.

IMPORTANT: This method should only update the object attributes. Do NOT save it to the database. The object will be saved by the sync_user method.

gws_core_user : User
The source User object from gws_core
Return type : TCustomUser
get_all_custom_users

Retrieve all custom users from the custom database.

This method uses the get_user_type() to retrieve the user model class and performs a select() query to get all users.

Return type : list
get_custom_user_by_id

Retrieve a custom user by ID from the custom database.

This method uses the get_user_type() to retrieve the user model class and calls get_by_id() on it.

user_id : str
The ID of the user to retrieve
Return type : Optional
get_or_import_from_space_user

Get a custom user by ID, or import from space service if not found.

This is a convenience method for lazy synchronization. It first tries to retrieve the user from the custom database. If not found, it retrieves the user from gws_core DB. If not found, it retrieves info from the space service and synchronizes it.

user_id : str
The ID of the user to retrieve
Return type : Optional
get_or_import_user

Get a custom user by ID, or import from gws_core if not found.

This is a convenience method for lazy synchronization. It first tries to retrieve the user from the custom database. If not found, it synchronizes the user from gws_core (if provided).

user_id : str
The ID of the user to retrieve
Return type : Optional
get_user_type

Return the custom user model class type.

This method should return the class (not an instance) of your custom user model. The returned class must extend gws_core.core.model.model.Model.

Return type : type
handle

Handle incoming events from the gws_core event system.

This method automatically syncs users when relevant events occur:

  • system.started: Syncs all users from gws_core when the system starts
  • user.created: Syncs the newly created user
  • user.updated: Syncs the updated user
  • user.activated: Syncs the user when activated/deactivated

Override this method if you need custom event handling logic.

event : Union
The event to handle
is_synchronous

Whether this listener runs synchronously in the caller's thread.

If True:

  • Executes in the caller's thread (same DB transaction)
  • Exceptions propagate to the caller (can rollback transactions)
  • Runs BEFORE async listeners

If False (default):

  • Executes in background worker thread
  • Exceptions are caught and logged
Return type : bool
sync_all_users

Synchronize multiple users from gws_core to the custom database.

This method iterates through all provided gws_core users and syncs each one. Errors are logged but don't stop the synchronization process.

sync_user

Synchronize a single user from gws_core to the custom database.

This method checks if the user exists in the custom database:

  • If the user exists, it updates the existing record
  • If the user doesn't exist, it creates a new record
gws_core_user : User
The User object from gws_core to synchronize
Return type : TCustomUser
Shine Logo
Technical bricks to reuse or customize

Have you developed a brick?

Share it to accelerate projects for the entire community.