@@ -201,9 +201,37 @@ func (h *Handler) DeleteAccountGroup(c *gin.Context) {
201201 h .store .ApplyAccountGroups (acc .DBID , groups )
202202 }
203203 }
204+ if err := h .removeDeletedGroupFromAPIKeyScopes (ctx , id ); err != nil {
205+ writeInternalError (c , err )
206+ return
207+ }
204208 writeMessage (c , http .StatusOK , "分组已删除" )
205209}
206210
211+ func (h * Handler ) removeDeletedGroupFromAPIKeyScopes (ctx context.Context , groupID int64 ) error {
212+ if h == nil || h .db == nil || groupID <= 0 {
213+ return nil
214+ }
215+ keys , err := h .db .ListAPIKeys (ctx )
216+ if err != nil {
217+ return err
218+ }
219+ for _ , key := range keys {
220+ if key == nil || ! containsInt64 (key .AllowedGroupIDs , groupID ) {
221+ continue
222+ }
223+ next := removeInt64 (key .AllowedGroupIDs , groupID )
224+ if err := h .db .UpdateAPIKeyAllowedGroupIDs (ctx , key .ID , next ); err != nil {
225+ return err
226+ }
227+ if h .store != nil {
228+ h .store .SetAPIKeyAllowedGroups (key .ID , next )
229+ }
230+ h .invalidateAPIKeyRuntimeCaches (ctx , key .Key )
231+ }
232+ return nil
233+ }
234+
207235func sanitizeAccountGroupName (raw string ) (string , error ) {
208236 name := strings .TrimSpace (raw )
209237 if name == "" {
@@ -230,6 +258,15 @@ func removeInt64(slice []int64, target int64) []int64 {
230258 return out
231259}
232260
261+ func containsInt64 (slice []int64 , target int64 ) bool {
262+ for _ , v := range slice {
263+ if v == target {
264+ return true
265+ }
266+ }
267+ return false
268+ }
269+
233270func dedupeInt64 (ids []int64 ) []int64 {
234271 if len (ids ) == 0 {
235272 return nil
0 commit comments