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 [email protected].
To unsubscribe, please send an email to:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]