You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@superset.apache.org by "rluech (via GitHub)" <gi...@apache.org> on 2025/03/24 10:36:34 UTC

[D] How to dynamically switch database connection based on role instead of username? [superset]

GitHub user rluech created a discussion: How to dynamically switch database connection based on role instead of username?

How can I get the roles of the current user instead of the username in DB_CONNECTION_MUTATOR function?

The example in the config.py file uses the username to manipulate the uri:
```
def DB_CONNECTION_MUTATOR(uri, params, username, security_manager, source):
     user = security_manager.find_user(username=username)
     if user and user.email:
         uri.username = user.email
     return uri, params
```

I would like to manipulate the uri if the user has a specific role.

GitHub link: https://github.com/apache/superset/discussions/32818

----
This is an automatically sent email for notifications@superset.apache.org.
To unsubscribe, please send an email to: notifications-unsubscribe@superset.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org


Re: [D] How to dynamically switch database connection based on role instead of username? [superset]

Posted by "dosubot[bot] (via GitHub)" <gi...@apache.org>.
GitHub user dosubot[bot] added a comment to the discussion: How to dynamically switch database connection based on role instead of username?

<!-- Answer -->
To dynamically switch the database connection based on a user's role in the `DB_CONNECTION_MUTATOR` function, you can retrieve the user's roles using the `security_manager.get_user_roles()` method. Here's how you can modify the function to check for a specific role:

```python
def DB_CONNECTION_MUTATOR(uri, params, username, security_manager, source):
    user = security_manager.find_user(username=username)
    if user:
        roles = security_manager.get_user_roles(user)
        # Check if the user has a specific role
        if any(role.name == "specific_role_name" for role in roles):
            # Manipulate the URI based on the role
            uri.username = "role_based_username"
            uri.password = "role_based_password"
    return uri, params
```

In this example, replace `"specific_role_name"` with the name of the role you want to check for, and adjust the URI manipulation as needed for your use case. This approach allows you to customize the database connection based on the user's roles instead of their username <sup>[[1]](https://github.com/apache/superset/blob/master/superset/security/manager.py)</sup>.


<!-- Dosu Comment Footer -->
*To continue talking to [Dosu](https://dosu.dev), mention @dosu.*

---
<sup>Help Dosu learn! Give it feedback: [Great Response](https://app.dosu.dev/response-feedback/b0f877c3-fb13-462a-b3d9-282a7f16ad8c?feedback_type=great_response) | [Irrelevant Answer](https://app.dosu.dev/response-feedback/b0f877c3-fb13-462a-b3d9-282a7f16ad8c?feedback_type=irrelevant_answer) | [Incorrect Sources](https://app.dosu.dev/response-feedback/b0f877c3-fb13-462a-b3d9-282a7f16ad8c?feedback_type=incorrect_sources) | [Too Verbose](https://app.dosu.dev/response-feedback/b0f877c3-fb13-462a-b3d9-282a7f16ad8c?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/b0f877c3-fb13-462a-b3d9-282a7f16ad8c?feedback_type=hallucination) | [Bug Report](https://app.dosu.dev/response-feedback/b0f877c3-fb13-462a-b3d9-282a7f16ad8c?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/b0f877c3-fb13-462a-b3d9-282a7f16ad8c?feedback_type=other)</sup>


GitHub link: https://github.com/apache/superset/discussions/32818#discussioncomment-12600582

----
This is an automatically sent email for notifications@superset.apache.org.
To unsubscribe, please send an email to: notifications-unsubscribe@superset.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org


Re: [D] How to dynamically switch database connection based on role instead of username? [superset]

Posted by "rluech (via GitHub)" <gi...@apache.org>.
GitHub user rluech added a comment to the discussion: How to dynamically switch database connection based on role instead of username?

Thank you! Thats exactly what I was looking for.

GitHub link: https://github.com/apache/superset/discussions/32818#discussioncomment-12601363

----
This is an automatically sent email for notifications@superset.apache.org.
To unsubscribe, please send an email to: notifications-unsubscribe@superset.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org