GitHub user baptisteaden added a comment to the discussion: Set database 
transaction params

@dosu SQL_QUERY_MUTATOR was causing an error (maybe Superset version related? 
I'm on v5), I tried with DB_ENGINE_MUTATOR instead, something like that:

```
from superset.sql_parse import ParsedQuery
from flask_jwt_extended import get_jwt
from flask import has_request_context

class CustomSecurityManager(SupersetSecurityManager):
    def get_guest_user_from_token(self, token):
        user = super().get_guest_user_from_token(token)

        custom = token.get("custom_claims", {})

        g.app_person_id = custom.get("person_id")
        g.app_application_id = custom.get("application_id")
        g.app_role = custom.get("role")

        print("🔥 Captured guest claims", custom, flush=True)

        return user


CUSTOM_SECURITY_MANAGER = CustomSecurityManager


def db_engine_mutator(engine):
    @event.listens_for(engine, "begin")
    def set_rls_context(conn):
        print("🔥 RLS SET FROM G", flush=True)

        if not has_request_context():
            return

        person_id = getattr(g, "app_person_id", None)
        application_id = getattr(g, "app_application_id", None)
        role = getattr(g, "app_role", None)

        if not person_id:
            return

        conn.exec_driver_sql(
            """
            SET LOCAL jwt.claims.person_id = %s;
            SET LOCAL jwt.claims.application_id = %s;
            SET LOCAL jwt.claims.role = %s;
            """,
            (person_id, application_id, role),
        )

    return engine


DB_ENGINE_MUTATOR = db_engine_mutator
```

But it seems `set_rls_context` was never called as the log was never shown.

GitHub link: 
https://github.com/apache/superset/discussions/37498#discussioncomment-15641656

----
This is an automatically sent email for [email protected].
To unsubscribe, please send an email to: 
[email protected]


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

Reply via email to