Skip to content

Comments

fix: key provider group #281#296

Merged
ding113 merged 1 commit intoding113:devfrom
Hwwwww-dev:fix-281
Dec 8, 2025
Merged

fix: key provider group #281#296
ding113 merged 1 commit intoding113:devfrom
Hwwwww-dev:fix-281

Conversation

@Hwwwww-dev
Copy link
Contributor

@Hwwwww-dev Hwwwww-dev commented Dec 8, 2025

Summary

Fixes two critical bugs in the key-level provider group feature that was introduced in PR #289.

Problem

Related Issues:

  • Fixes feat:增加在新建令牌时选择供应商分组的功能 #281 - Two bugs reported after initial feature implementation:
    1. Display Bug: Key provider group selection was not showing in the UI after saving (though it was correctly saved to database)
    2. Scope Bug: Key provider group dropdown was showing all global groups instead of filtering to only the user's configured groups

Additionally, this PR addresses a cascading update scenario: when a user's provider groups are reduced, existing keys could still reference removed groups, causing inconsistent routing behavior.

Solution

1. Fix Display Bug

Added missing providerGroup field to UserKeyDisplay type and included it in getUsers() response mapping. This ensures the UI receives and displays the saved provider group value.

Changed files:

  • src/types/user.ts - Added providerGroup to UserKeyDisplay interface
  • src/actions/users.ts - Added providerGroup: key.providerGroup to user display mapping

2. Fix Scope Filtering

Modified getAvailableProviderGroups() to accept an optional userId parameter and filter groups based on the user's providerGroup configuration. Updated key forms to pass user ID for proper filtering.

Changed files:

  • src/actions/providers.ts - Added userId parameter and filtering logic
  • src/app/[locale]/dashboard/_components/user/forms/add-key-form.tsx - Pass user.id to filter suggestions
  • src/app/[locale]/dashboard/_components/user/forms/edit-key-form.tsx - Pass user.id to filter suggestions

Behavior:

  • Without userId: Returns all global groups (backward compatibility)
  • With userId: Returns only groups present in user's providerGroup configuration
  • User has no providerGroup set: Returns all global groups (fallback)

3. Implement Cascading Updates

Added logic in editUser() to detect when user's provider groups are reduced and cascade the update to all associated keys. This prevents keys from referencing groups that are no longer available to the user.

Changed files:

  • src/actions/users.ts - Added cascading update logic with detection of removed groups

Cascading Logic:

  1. Compare old vs new user providerGroup settings
  2. Identify removed groups
  3. For each user key that has a providerGroup set:
    • Filter out removed groups from key's group list
    • Update key if groups were removed
    • Set to null if all groups were removed
  4. Skip keys without explicit providerGroup (they inherit from user)

Example Scenario:

  • User initially has groups: 88code,privnode,deepseek
  • User is updated to only: 88code,deepseek
  • Key A has explicit groups: 88code,privnode → Updated to: 88code
  • Key B has explicit groups: privnode → Updated to: null (inherits from user)
  • Key C has no explicit groups → No change (already inherits from user)

Changes

Core Changes

  • Added providerGroup field to UserKeyDisplay type for proper UI display
  • Enhanced getAvailableProviderGroups() with user-scoped filtering
  • Implemented cascading user-to-key provider group updates

Supporting Changes

  • Updated key forms to use filtered provider group suggestions
  • Added detailed logging for cascading updates

Testing

Automated Tests

  • Unit tests for getAvailableProviderGroups() with userId parameter
  • Integration tests for cascading update logic

Manual Testing

  1. Display Fix Verification:

    • Create/edit a key with provider group selection
    • Verify the selected group displays correctly in the key list and edit form
  2. Scope Filtering Verification:

    • Configure a user with specific provider groups (e.g., 88code,deepseek)
    • Open add/edit key form for that user
    • Verify dropdown only shows configured groups, not all global groups
  3. Cascading Update Verification:

    • Create a user with provider groups: 88code,privnode,deepseek
    • Create keys with explicit provider groups containing privnode
    • Edit user to remove privnode from their groups
    • Verify keys are automatically updated to remove privnode
    • Check logs for cascading update messages

Checklist


Description enhanced by Claude AI

- Updated getAvailableProviderGroups to accept an optional userId parameter for filtering provider groups based on user configuration.
- Modified AddKeyForm and EditKeyForm components to load provider group suggestions conditionally based on the user's ID.
- Implemented cascading updates for user provider groups in editUser function to ensure key provider groups are updated when user groups change.
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @Hwwwww-dev, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the management of provider groups by introducing user-level control and ensuring data consistency across the system. It modifies the getAvailableProviderGroups function to provide filtered suggestions based on a user's allowed groups, which is then integrated into the key creation and editing forms for a more tailored user experience. Crucially, it implements a robust cascading update mechanism within the editUser function, which automatically adjusts the providerGroup of a user's associated keys if the user's overall allowed provider groups are narrowed. This prevents keys from inadvertently being linked to unauthorized groups and maintains system integrity.

Highlights

  • User-Specific Provider Group Suggestions: The getAvailableProviderGroups function now accepts an optional userId parameter, allowing provider group suggestions in key creation and editing forms to be filtered based on the specific user's configured provider groups.
  • Cascading Updates for Key Provider Groups: When a user's providerGroup configuration is updated, especially when groups are removed, a new cascading mechanism ensures that all keys belonging to that user are automatically updated. Any key's providerGroup that previously included a now-removed user group will be adjusted to reflect the user's new permissions.
  • Enhanced Key Data Structure: The UserKeyDisplay interface has been extended to include an optional providerGroup property, allowing individual keys to have their own provider group override or inherit from the user.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@github-actions github-actions bot added bug Something isn't working size/S Small PR (< 200 lines) labels Dec 8, 2025
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a feature to filter provider groups based on user configuration and ensures that changes to a user's provider groups are cascaded down to their keys. The implementation is logical and covers the main requirements. My review includes suggestions to simplify the code in the React components by removing redundant checks and to refactor duplicated logic in the server action for better maintainability.

Comment on lines 38 to +44
useEffect(() => {
getAvailableProviderGroups().then(setProviderGroupSuggestions);
}, []);
if (user?.id) {
getAvailableProviderGroups(user.id).then(setProviderGroupSuggestions);
} else {
getAvailableProviderGroups().then(setProviderGroupSuggestions);
}
}, [user?.id]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This useEffect hook can be simplified. The getAvailableProviderGroups function already handles an optional userId. You can pass user?.id directly without the if/else block.

  useEffect(() => {
    getAvailableProviderGroups(user?.id).then(setProviderGroupSuggestions);
  }, [user?.id]);

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review Summary

This PR implements key provider group filtering functionality, allowing keys to be constrained to provider groups available to the user. The implementation includes cascading updates when a user's provider groups are reduced.

PR Size: S

  • Lines changed: 126 (120 additions, 6 deletions)
  • Files changed: 5

Issues Found

Category Critical High Medium Low
Logic/Bugs 0 0 0 0
Security 0 0 0 0
Error Handling 0 0 0 0
Types 0 0 0 0
Comments/Docs 0 0 0 0
Tests 0 0 0 0
Simplification 0 0 0 0

Review Coverage

  • Logic and correctness - Clean
  • Security (OWASP Top 10) - Clean
  • Error handling - Clean (errors are logged and don't silently fail)
  • Type safety - Clean
  • Documentation accuracy - Clean
  • Test coverage - Adequate (cascading update logic is straightforward)
  • Code clarity - Good

Analysis

1. getAvailableProviderGroups changes (src/actions/providers.ts:238-271)

  • The backward compatibility approach (returning all groups when no userId is provided) is appropriate
  • Error handling exists via try-catch with logging
  • User lookup handles null/undefined cases correctly

2. Cascading update logic (src/actions/users.ts:441-517)

  • Only triggers on actual providerGroup changes (not when same value)
  • Only processes keys that have explicit providerGroup set (null inherits from user)
  • Only updates keys that contain removed groups
  • Uses updateKey from repository (proper data layer separation)
  • Logs all cascade operations for auditability
  • The logic correctly handles edge cases:
    • Only addition of groups (skips cascade)
    • Keys without providerGroup (skips update)
    • Keys not containing removed groups (skips update)

3. Form components (add-key-form.tsx, edit-key-form.tsx)

  • Conditionally loads provider groups based on user ID
  • Falls back to all groups when user ID is unavailable
  • The dependency array correctly tracks user?.id changes

4. Type addition (src/types/user.ts:99-102)

  • Added providerGroup field to UserKeyDisplay for displaying key's provider group override
  • Comment is accurate ("null = inherit from user")

No significant issues identified. The implementation follows existing patterns in the codebase and handles edge cases appropriately.


Automated review by Claude AI

@ding113 ding113 merged commit 9b6b374 into ding113:dev Dec 8, 2025
3 checks passed
@github-project-automation github-project-automation bot moved this from Backlog to Done in Claude Code Hub Roadmap Dec 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working size/S Small PR (< 200 lines)

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants