A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.
fix: enhance user admin service to manage API keys and filter answer visibility
LinkinStars9d9f5d06a8a3
diff --git a/cmd/wire_gen.go b/cmd/wire_gen.goindex d020e3e5..446f6cc0 100644--- a/cmd/wire_gen.go+++ b/cmd/wire_gen.go@@ -244,7 +244,7 @@ func initApplication(debug bool, serverConf *conf.Server, dbConf *data.Database,notificationRepo := notification2.NewNotificationRepo(dataData)pluginUserConfigRepo := plugin_config.NewPluginUserConfigRepo(dataData)badgeAwardRepo := badge_award.NewBadgeAwardRepo(dataData, uniqueIDRepo)- userAdminService := user_admin.NewUserAdminService(userAdminRepo, userRoleRelService, authService, userCommon, userActiveActivityRepo, siteInfoCommonService, emailService, questionRepo, answerRepo, commentCommonRepo, userExternalLoginRepo, notificationRepo, pluginUserConfigRepo, badgeAwardRepo)+ userAdminService := user_admin.NewUserAdminService(userAdminRepo, userRoleRelService, authService, userCommon, userActiveActivityRepo, siteInfoCommonService, emailService, questionRepo, answerRepo, commentCommonRepo, userExternalLoginRepo, notificationRepo, pluginUserConfigRepo, badgeAwardRepo, apiKeyRepo)userAdminController := controller_admin.NewUserAdminController(userAdminService)reasonRepo := reason.NewReasonRepo(configService)reasonService := reason2.NewReasonService(reasonRepo)diff --git a/internal/controller/mcp_controller.go b/internal/controller/mcp_controller.goindex b40c58cf..e24c1a54 100644--- a/internal/controller/mcp_controller.go+++ b/internal/controller/mcp_controller.go@@ -176,6 +176,9 @@ func (c *MCPController) MCPAnswersHandler() func(ctx context.Context, request mc}resp := make([]*schema.MCPSearchAnswerInfoResp, 0)for _, answer := range answerList {+ if answer.Status != entity.AnswerStatusAvailable {+ continue+ }t := &schema.MCPSearchAnswerInfoResp{QuestionID: answer.QuestionID,AnswerID: answer.ID,@@ -195,6 +198,9 @@ func (c *MCPController) MCPAnswersHandler() func(ctx context.Context, request mc}resp := make([]*schema.MCPSearchAnswerInfoResp, 0)for _, answer := range answerList {+ if answer.Status != entity.AnswerStatusAvailable {+ continue+ }t := &schema.MCPSearchAnswerInfoResp{QuestionID: answer.QuestionID,AnswerID: answer.ID,diff --git a/internal/repo/answer/answer_repo.go b/internal/repo/answer/answer_repo.goindex 52963c44..42e3494a 100644--- a/internal/repo/answer/answer_repo.go+++ b/internal/repo/answer/answer_repo.go@@ -196,8 +196,12 @@ func (ar *answerRepo) GetAnswerCount(ctx context.Context) (count int64, err erro// GetAnswerList get answer list allfunc (ar *answerRepo) GetAnswerList(ctx context.Context, answer *entity.Answer) (answerList []*entity.Answer, err error) {answerList = make([]*entity.Answer, 0)- answer.ID = uid.DeShortID(answer.ID)- answer.QuestionID = uid.DeShortID(answer.QuestionID)+ if len(answer.ID) > 0 {+ answer.ID = uid.DeShortID(answer.ID)+ }+ if len(answer.QuestionID) > 0 {+ answer.QuestionID = uid.DeShortID(answer.QuestionID)+ }err = ar.data.DB.Context(ctx).Find(&answerList, answer)if err != nil {err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()diff --git a/internal/repo/api_key/api_key_repo.go b/internal/repo/api_key/api_key_repo.goindex 2309384a..8fd9ed46 100644--- a/internal/repo/api_key/api_key_repo.go+++ b/internal/repo/api_key/api_key_repo.go@@ -81,3 +81,11 @@ func (ar *apiKeyRepo) DeleteAPIKey(ctx context.Context, id int) (err error) {}return}++func (ar *apiKeyRepo) DeleteAPIKeysByUserID(ctx context.Context, userID string) (err error) {+ _, err = ar.data.DB.Context(ctx).Where("user_id = ?", userID).Delete(&entity.APIKey{})+ if err != nil {+ err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()+ }+ return+}diff --git a/internal/service/apikey/apikey_service.go b/internal/service/apikey/apikey_service.goindex 43c1294c..2d154ebd 100644--- a/internal/service/apikey/apikey_service.go+++ b/internal/service/apikey/apikey_service.go@@ -35,6 +35,7 @@ type APIKeyRepo interface {UpdateAPIKey(ctx context.Context, apiKey entity.APIKey) (err error)AddAPIKey(ctx context.Context, apiKey entity.APIKey) (err error)DeleteAPIKey(ctx context.Context, id int) (err error)+ DeleteAPIKeysByUserID(ctx context.Context, userID string) (err error)}type APIKeyService struct {@@ -114,3 +115,7 @@ func (s *APIKeyService) DeleteAPIKey(ctx context.Context, req *schema.DeleteAPIK}return nil}++func (s *APIKeyService) DeleteUserAPIKeys(ctx context.Context, userID string) error {+ return s.apiKeyRepo.DeleteAPIKeysByUserID(ctx, userID)+}diff --git a/internal/service/user_admin/user_backyard.go b/internal/service/user_admin/user_backyard.goindex fcced1c8..29e33804 100644--- a/internal/service/user_admin/user_backyard.go+++ b/internal/service/user_admin/user_backyard.go@@ -45,6 +45,7 @@ import ("github.com/apache/answer/internal/entity""github.com/apache/answer/internal/schema""github.com/apache/answer/internal/service/activity"+ "github.com/apache/answer/internal/service/apikey""github.com/apache/answer/internal/service/auth""github.com/apache/answer/internal/service/role""github.com/apache/answer/internal/service/siteinfo_common"@@ -87,6 +88,7 @@ type UserAdminService struct {notificationRepo notificationcommon.NotificationRepopluginUserConfigRepo plugin_common.PluginUserConfigRepobadgeAwardRepo badge.BadgeAwardRepo+ apiKeyRepo apikey.APIKeyRepo}// NewUserAdminService new user admin service@@ -105,6 +107,7 @@ func NewUserAdminService(notificationRepo notificationcommon.NotificationRepo,pluginUserConfigRepo plugin_common.PluginUserConfigRepo,badgeAwardRepo badge.BadgeAwardRepo,+ apiKeyRepo apikey.APIKeyRepo,) *UserAdminService {return &UserAdminService{userRepo: userRepo,@@ -121,6 +124,7 @@ func NewUserAdminService(notificationRepo: notificationRepo,pluginUserConfigRepo: pluginUserConfigRepo,badgeAwardRepo: badgeAwardRepo,+ apiKeyRepo: apiKeyRepo,}}@@ -162,6 +166,11 @@ func (us *UserAdminService) UpdateUserStatus(ctx context.Context, req *schema.Upif err != nil {return err}+ if req.IsInactive() || req.IsSuspended() || req.IsDeleted() {+ if err := us.revokeUserAPIKeys(ctx, userInfo.ID); err != nil {+ return err+ }+ }// remove all content that user created, such as question, answer, comment, etc.if req.RemoveAllContent {@@ -227,11 +236,18 @@ func (us *UserAdminService) UpdateUserRole(ctx context.Context, req *schema.Updaif err != nil {return err}+ if err := us.revokeUserAPIKeys(ctx, req.UserID); err != nil {+ return err+ }us.authService.RemoveUserAllTokens(ctx, req.UserID)return}+func (us *UserAdminService) revokeUserAPIKeys(ctx context.Context, userID string) error {+ return us.apiKeyRepo.DeleteAPIKeysByUserID(ctx, userID)+}+// AddUser add userfunc (us *UserAdminService) AddUser(ctx context.Context, req *schema.AddUserReq) (err error) {_, has, err := us.userRepo.GetUserInfoByEmail(ctx, req.Email)