LeandroMartinMacato commented on issue #35594:
URL: https://github.com/apache/superset/issues/35594#issuecomment-3395597243

   @dosu
   I created a seperate file for my custom DbEventLogger Class and refer to it 
in `superset_config.py`, This is now able to trigger the specific event , 
however getting this error in the user management page `This transaction is 
closed` when logging into superset's logging table
   
   EVENT_LOGGER = CustomEventLogger
   
   `custom_db_event_logger.py`
   ```python
   from superset.utils.log import DBEventLogger
   
   from sqlalchemy import event
   from flask_appbuilder.security.sqla.models import User
   from custom_sso_security_manager import CustomUser
   
   
   from flask import has_request_context, request, g
   
   
   class CustomEventLogger(DBEventLogger):
       def __init__(self):
           print(">>>>> Initializing CustomUserEvent Logger <<<<<")
   
           #  Listen to Meta Database Update then audit movement
           event.listen(CustomUser, 'after_update', self.log_user_edit)
           event.listen(CustomUser, 'after_insert', self.log_user_insert)
           event.listen(CustomUser, 'after_delete', self.log_user_delete)
   
       def log_user_insert(self, mapper, connection, target):
           print("\n\n>>>>>>> USER MANAGEMENT INSERT USER TRIGGER 
<<<<<<<<<<<<<<<<\n\n")
           self.log(actor.id, "user_insert", dashboard_id=None,
                    duration_ms=None, slice_id=None, referrer=None)
   
       def log_user_delete(self, mapper, connection, target):
           print("\n\n>>>>>>> USER MANAGEMENT DELETE USER TRIGGER 
<<<<<<<<<<<<<<<<\n\n")
           self.log(actor.id, "user_delete", dashboard_id=None,
                    duration_ms=None, slice_id=None, referrer=None)
   
       def log_user_edit(self, mapper, connection, target):
           # Replace with your custom logging logic
           print("\n\n>>>>>>> USER MANAGEMENT EDIT USER TRIGGER 
<<<<<<<<<<<<<<<<\n\n")
           print(type(target))
           print(target)
   
           actor = getattr(g, "user", None)
           username = getattr(actor, "username", "system")
           print(f'ACTOR: {actor} \n Actor Username: {username}')
   
           ip_address = None
           if has_request_context():
               ip_address = request.headers.get(
                   "X-Forwarded-For", request.remote_addr)
   
           print(f'IP ADDRESS: {ip_address}')
           # self.log(target.id, "user_update", details={"username": 
target.username})
   
           details = {
               "username": username,
               "ip_address": ip_address,
               "actor_id": actor.id if actor else None,
           }
           # self.log(actor.id, "user_edit", dashboard_id=None, 
duration_ms=None,
           #          slice_id=None, referrer=None, **{"records": [details]})
           # self.log(actor.id, "user_edit", dashboard_id=None, 
duration_ms=None,
           #          slice_id=None, referrer=None)
           self.log(actor.id, "user_edit", *args, **kwargs)
   
       def log(self, user_id, action, *args, **kwargs):
           # print(f"USER: {user_id} >>>>>>>> PERFORMED : {action} 
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<")
           # # You can add custom logic here to filter for user management 
actions
           # if action in ["user_create", "user_update", "user_delete"]:
           #     # Add your custom logging logic here
           #     print(f"User management action: {action}, user_id: {user_id}, 
details: {kwargs}")
           # # Call the parent logger to keep default behavior
           super().log(user_id, action, *args, **kwargs)
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to