mirror of
https://github.com/open-webui/open-webui.git
synced 2026-09-21 05:28:05 -05:00
feat: emit group events on OAuth group sync (#27657)
With ENABLE_OAUTH_GROUP_MANAGEMENT enabled, every SSO login reconciles the user's group membership against the IdP claims, adding and removing them from groups and, with ENABLE_OAUTH_GROUP_CREATION, creating groups that do not exist yet. None of it emitted an event, so the same membership change was observable when an admin made it through the UI or when it arrived over SCIM, but invisible when the IdP drove it. That is the path that changes membership most often. Emits group.member_added and group.member_removed per membership transition and group.created for each auto-created group, using the same payload keys as the groups router. The member events are published only when the write returned a group, so a failed or no-op write emits nothing, and both loops already run only on an actual transition. update_user_groups takes the request so the events can be published; it has a single caller.
This commit is contained in:
@@ -1585,7 +1585,7 @@ class OAuthManager:
|
||||
|
||||
return role
|
||||
|
||||
async def update_user_groups(self, user, user_data, default_permissions, db=None):
|
||||
async def update_user_groups(self, request, user, user_data, default_permissions, db=None):
|
||||
auth_config = await get_oauth_runtime_config()
|
||||
log.debug('Running OAUTH Group management')
|
||||
oauth_claim = auth_config.OAUTH_GROUPS_CLAIM
|
||||
@@ -1650,6 +1650,13 @@ class OAuthManager:
|
||||
groups_created = True
|
||||
# Add to local set to prevent duplicate creation attempts in this run
|
||||
all_group_names.add(group_name)
|
||||
await publish_event(
|
||||
request,
|
||||
EVENTS.GROUP_CREATED,
|
||||
subject_id=created_group.id,
|
||||
source='oauth',
|
||||
data={'name': created_group.name},
|
||||
)
|
||||
else:
|
||||
log.error(f"Failed to create group '{group_name}' via OAuth.")
|
||||
except Exception as e:
|
||||
@@ -1674,7 +1681,15 @@ class OAuthManager:
|
||||
):
|
||||
# Remove group from user
|
||||
log.debug('Removing user from group %s as it is no longer in their oauth groups', group_model.name)
|
||||
await Groups.remove_users_from_group(group_model.id, [user.id], db=db)
|
||||
if await Groups.remove_users_from_group(group_model.id, [user.id], db=db):
|
||||
await publish_event(
|
||||
request,
|
||||
EVENTS.GROUP_MEMBER_REMOVED,
|
||||
actor=user,
|
||||
subject_id=group_model.id,
|
||||
source='oauth',
|
||||
data={'user_ids': [user.id]},
|
||||
)
|
||||
|
||||
# In case a group is created, but perms are never assigned to the group by hitting "save"
|
||||
group_permissions = group_model.permissions
|
||||
@@ -1703,7 +1718,15 @@ class OAuthManager:
|
||||
# Add user to group
|
||||
log.debug('Adding user to group %s as it was found in their oauth groups', group_model.name)
|
||||
|
||||
await Groups.add_users_to_group(group_model.id, [user.id], db=db)
|
||||
if await Groups.add_users_to_group(group_model.id, [user.id], db=db):
|
||||
await publish_event(
|
||||
request,
|
||||
EVENTS.GROUP_MEMBER_ADDED,
|
||||
actor=user,
|
||||
subject_id=group_model.id,
|
||||
source='oauth',
|
||||
data={'user_ids': [user.id]},
|
||||
)
|
||||
|
||||
# In case a group is created, but perms are never assigned to the group by hitting "save"
|
||||
group_permissions = group_model.permissions
|
||||
@@ -2060,6 +2083,7 @@ class OAuthManager:
|
||||
)
|
||||
if auth_config.ENABLE_OAUTH_GROUP_MANAGEMENT:
|
||||
await self.update_user_groups(
|
||||
request=request,
|
||||
user=user,
|
||||
user_data=user_data,
|
||||
default_permissions=await Config.get('user.permissions'),
|
||||
|
||||
Reference in New Issue
Block a user