dosubot[bot] commented on issue #35594:
URL: https://github.com/apache/superset/issues/35594#issuecomment-3388620317

   <!-- Answer -->
   You can integrate your custom user management action logging directly into 
Superset’s Action Log by using your custom `DbEventLogger` to write entries to 
the logs table. The logs table supports fields like `action`, `user_id`, `json` 
(for extra details), and `referrer` (which you can use for the IP address) 
[reference](https://github.com/apache/superset/blob/master/superset/models/core.py).
   
   Here’s a detailed example using SQLAlchemy events and Flask’s request 
context to capture user CRUD actions and log the user’s IP address:
   
   ```python
   # In superset_config.py or your custom extension
   from sqlalchemy import event
   from flask import request
   from flask_appbuilder.security.sqla.models import User
   
   def log_user_event(mapper, connection, target):
       # Safely get the user's IP address from the request context
       ip_addr = None
       if request:
           # Prefer X-Forwarded-For if behind a proxy, else use remote_addr
           ip_addr = request.headers.get('X-Forwarded-For', request.remote_addr)
       # Choose the action type based on the event
       action = "user_update"  # or "user_create"/"user_delete" depending on 
the event
       # Build extra details for auditing
       details = {
           "username": target.username,
           "email": getattr(target, "email", None),
           "ip_address": ip_addr,
       }
       # Log to Superset's Action Log via your custom event logger
       EVENT_LOGGER.log(
           user_id=target.id,
           action=action,
           json=details,  # This will be serialized and stored in the logs table
           referrer=ip_addr,  # Optionally store IP in the referrer field
       )
   
   event.listen(User, 'after_update', log_user_event)
   event.listen(User, 'after_insert', log_user_event)
   event.listen(User, 'after_delete', log_user_event)
   ```
   
   - The `json` field in the logs table can store any extra metadata you want 
(like username, email, IP address, etc.) 
[reference](https://github.com/apache/superset/blob/master/superset/models/core.py).
   - The `referrer` field can be used to store the IP address for easy 
filtering in the Action Log UI 
[reference](https://github.com/apache/superset-frontend/src/pages/ActionLog/index.tsx).
   - You can filter and view these logs in the Action Log UI by action, user, 
or any custom details you include 
[reference](https://github.com/apache/superset-frontend/src/pages/ActionLog/index.tsx).
   
   This pattern is upgrade-safe since it uses SQLAlchemy events and the 
standard logging infrastructure. If you patch view logic instead, you can use 
the `@EVENT_LOGGER.log_this_with_context(action="user_create")` decorator on 
your custom methods to automatically capture request context and log entries 
[reference](https://github.com/apache/superset/utils/log.py).
   
   Let me know if you want an example for patching view logic or have questions 
about customizing the log format!
   
   
   <!-- Dosu Comment Footer -->
   *To reply, just mention [@dosu](https://go.dosu.dev/dosubot).*
   
   ---
   <sup>How did I do? 
[Good](https://app.dosu.dev/response-feedback/c048fa61-3f55-4c57-9878-3db3b9f15304?feedback_type=great_response)
 | 
[Irrelevant](https://app.dosu.dev/response-feedback/c048fa61-3f55-4c57-9878-3db3b9f15304?feedback_type=irrelevant_answer)
 | 
[Incorrect](https://app.dosu.dev/response-feedback/c048fa61-3f55-4c57-9878-3db3b9f15304?feedback_type=incorrect_sources)
 | 
[Verbose](https://app.dosu.dev/response-feedback/c048fa61-3f55-4c57-9878-3db3b9f15304?feedback_type=too_verbose)
 | 
[Hallucination](https://app.dosu.dev/response-feedback/c048fa61-3f55-4c57-9878-3db3b9f15304?feedback_type=hallucination)
 | [Report 
🐛](https://app.dosu.dev/response-feedback/c048fa61-3f55-4c57-9878-3db3b9f15304?feedback_type=bug_report)
 | 
[Other](https://app.dosu.dev/response-feedback/c048fa61-3f55-4c57-9878-3db3b9f15304?feedback_type=other)</sup>&nbsp;&nbsp;[![Chat
 with 
Dosu](https://dosu.dev/dosu-chat-badge.svg)](https://app.dosu.dev/a28d3c7e-a9d3-459e-9fb6-3a6f9ff4f357/ask?utm_source=githu
 b)&nbsp;[![Join 
Discord](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&label=)](https://go.dosu.dev/discord-bot)&nbsp;[![Share
 on 
X](https://img.shields.io/badge/X-share-black)](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/apache/superset/issues/35594)


-- 
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