@@ -45,6 +45,7 @@ import (
4545 "github.com/apache/answer/internal/entity"
4646 "github.com/apache/answer/internal/schema"
4747 "github.com/apache/answer/internal/service/activity"
48+ "github.com/apache/answer/internal/service/apikey"
4849 "github.com/apache/answer/internal/service/auth"
4950 "github.com/apache/answer/internal/service/role"
5051 "github.com/apache/answer/internal/service/siteinfo_common"
@@ -87,6 +88,7 @@ type UserAdminService struct {
8788 notificationRepo notificationcommon.NotificationRepo
8889 pluginUserConfigRepo plugin_common.PluginUserConfigRepo
8990 badgeAwardRepo badge.BadgeAwardRepo
91+ apiKeyRepo apikey.APIKeyRepo
9092}
9193
9294// NewUserAdminService new user admin service
@@ -105,6 +107,7 @@ func NewUserAdminService(
105107 notificationRepo notificationcommon.NotificationRepo ,
106108 pluginUserConfigRepo plugin_common.PluginUserConfigRepo ,
107109 badgeAwardRepo badge.BadgeAwardRepo ,
110+ apiKeyRepo apikey.APIKeyRepo ,
108111) * UserAdminService {
109112 return & UserAdminService {
110113 userRepo : userRepo ,
@@ -121,6 +124,7 @@ func NewUserAdminService(
121124 notificationRepo : notificationRepo ,
122125 pluginUserConfigRepo : pluginUserConfigRepo ,
123126 badgeAwardRepo : badgeAwardRepo ,
127+ apiKeyRepo : apiKeyRepo ,
124128 }
125129}
126130
@@ -162,6 +166,11 @@ func (us *UserAdminService) UpdateUserStatus(ctx context.Context, req *schema.Up
162166 if err != nil {
163167 return err
164168 }
169+ if req .IsInactive () || req .IsSuspended () || req .IsDeleted () {
170+ if err := us .revokeUserAPIKeys (ctx , userInfo .ID ); err != nil {
171+ return err
172+ }
173+ }
165174
166175 // remove all content that user created, such as question, answer, comment, etc.
167176 if req .RemoveAllContent {
@@ -227,11 +236,18 @@ func (us *UserAdminService) UpdateUserRole(ctx context.Context, req *schema.Upda
227236 if err != nil {
228237 return err
229238 }
239+ if err := us .revokeUserAPIKeys (ctx , req .UserID ); err != nil {
240+ return err
241+ }
230242
231243 us .authService .RemoveUserAllTokens (ctx , req .UserID )
232244 return
233245}
234246
247+ func (us * UserAdminService ) revokeUserAPIKeys (ctx context.Context , userID string ) error {
248+ return us .apiKeyRepo .DeleteAPIKeysByUserID (ctx , userID )
249+ }
250+
235251// AddUser add user
236252func (us * UserAdminService ) AddUser (ctx context.Context , req * schema.AddUserReq ) (err error ) {
237253 _ , has , err := us .userRepo .GetUserInfoByEmail (ctx , req .Email )
0 commit comments