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
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.
UserTCustomUserRetrieve 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.
listRetrieve 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.
strOptionalGet 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.
strOptionalGet 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).
strOptionalReturn 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.
typeHandle 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.
UnionWhether 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
boolSynchronize 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.
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
UserTCustomUser