|
15 | 15 |
|
16 | 16 | from open_webui.models.auths import Auths
|
17 | 17 | from open_webui.models.users import Users
|
| 18 | +from open_webui.models.roles import Roles |
18 | 19 | from open_webui.models.groups import Groups, GroupModel, GroupUpdateForm
|
19 | 20 | from open_webui.config import (
|
20 | 21 | DEFAULT_USER_ROLE,
|
@@ -79,6 +80,17 @@ def __init__(self, app):
|
79 | 80 | def get_client(self, provider_name):
|
80 | 81 | return self.oauth.create_client(provider_name)
|
81 | 82 |
|
| 83 | + def find_first_role_match(self, oauth_roles, allowed_roles): |
| 84 | + # Convert to sets for more efficient lookup if lists are large |
| 85 | + oauth_roles_set = set(oauth_roles) |
| 86 | + allowed_roles_set = set(allowed_roles) |
| 87 | + |
| 88 | + # Find the intersection of the two sets |
| 89 | + matching_roles = oauth_roles_set.intersection(allowed_roles_set) |
| 90 | + |
| 91 | + # Return the first matching role if any matches found |
| 92 | + return next(iter(matching_roles), None) |
| 93 | + |
82 | 94 | def get_user_role(self, user, user_data):
|
83 | 95 | if user and Users.get_num_users() == 1:
|
84 | 96 | # If the user is the only user, assign the role "admin" - actually repairs role for single user on login
|
@@ -117,8 +129,15 @@ def get_user_role(self, user, user_data):
|
117 | 129 | for allowed_role in oauth_allowed_roles:
|
118 | 130 | # If the user has any of the allowed roles, assign the role "user"
|
119 | 131 | if allowed_role in oauth_roles:
|
120 |
| - log.debug("Assigned user the user role") |
121 |
| - role = "user" |
| 132 | + first_match = self.find_first_role_match(oauth_roles, oauth_allowed_roles) |
| 133 | + if first_match: |
| 134 | + Roles.add_role_if_role_do_not_exists(first_match) |
| 135 | + role = first_match |
| 136 | + else: |
| 137 | + # Fallback to role user. |
| 138 | + role = "user" |
| 139 | + |
| 140 | + log.debug(f"Assigned user the {role} role") |
122 | 141 | break
|
123 | 142 | for admin_role in oauth_admin_roles:
|
124 | 143 | # If the user has any of the admin roles, assign the role "admin"
|
|
0 commit comments