Skip to content

Commit db84943

Browse files
committed
Merge remote-tracking branch 'origin/fix/2.0.2/user' into test
2 parents 74c7cf7 + 15d2d3f commit db84943

7 files changed

Lines changed: 48 additions & 3 deletions

File tree

cmd/wire_gen.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/controller/mcp_controller.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,9 @@ func (c *MCPController) MCPAnswersHandler() func(ctx context.Context, request mc
176176
}
177177
resp := make([]*schema.MCPSearchAnswerInfoResp, 0)
178178
for _, answer := range answerList {
179+
if answer.Status != entity.AnswerStatusAvailable {
180+
continue
181+
}
179182
t := &schema.MCPSearchAnswerInfoResp{
180183
QuestionID: answer.QuestionID,
181184
AnswerID: answer.ID,
@@ -195,6 +198,9 @@ func (c *MCPController) MCPAnswersHandler() func(ctx context.Context, request mc
195198
}
196199
resp := make([]*schema.MCPSearchAnswerInfoResp, 0)
197200
for _, answer := range answerList {
201+
if answer.Status != entity.AnswerStatusAvailable {
202+
continue
203+
}
198204
t := &schema.MCPSearchAnswerInfoResp{
199205
QuestionID: answer.QuestionID,
200206
AnswerID: answer.ID,

internal/repo/answer/answer_repo.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,12 @@ func (ar *answerRepo) GetAnswerCount(ctx context.Context) (count int64, err erro
196196
// GetAnswerList get answer list all
197197
func (ar *answerRepo) GetAnswerList(ctx context.Context, answer *entity.Answer) (answerList []*entity.Answer, err error) {
198198
answerList = make([]*entity.Answer, 0)
199-
answer.ID = uid.DeShortID(answer.ID)
200-
answer.QuestionID = uid.DeShortID(answer.QuestionID)
199+
if len(answer.ID) > 0 {
200+
answer.ID = uid.DeShortID(answer.ID)
201+
}
202+
if len(answer.QuestionID) > 0 {
203+
answer.QuestionID = uid.DeShortID(answer.QuestionID)
204+
}
201205
err = ar.data.DB.Context(ctx).Find(&answerList, answer)
202206
if err != nil {
203207
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()

internal/repo/api_key/api_key_repo.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,11 @@ func (ar *apiKeyRepo) DeleteAPIKey(ctx context.Context, id int) (err error) {
8181
}
8282
return
8383
}
84+
85+
func (ar *apiKeyRepo) DeleteAPIKeysByUserID(ctx context.Context, userID string) (err error) {
86+
_, err = ar.data.DB.Context(ctx).Where("user_id = ?", userID).Delete(&entity.APIKey{})
87+
if err != nil {
88+
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
89+
}
90+
return
91+
}

internal/service/apikey/apikey_service.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ type APIKeyRepo interface {
3535
UpdateAPIKey(ctx context.Context, apiKey entity.APIKey) (err error)
3636
AddAPIKey(ctx context.Context, apiKey entity.APIKey) (err error)
3737
DeleteAPIKey(ctx context.Context, id int) (err error)
38+
DeleteAPIKeysByUserID(ctx context.Context, userID string) (err error)
3839
}
3940

4041
type APIKeyService struct {
@@ -114,3 +115,7 @@ func (s *APIKeyService) DeleteAPIKey(ctx context.Context, req *schema.DeleteAPIK
114115
}
115116
return nil
116117
}
118+
119+
func (s *APIKeyService) DeleteUserAPIKeys(ctx context.Context, userID string) error {
120+
return s.apiKeyRepo.DeleteAPIKeysByUserID(ctx, userID)
121+
}

internal/service/content/answer_service.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,12 +554,18 @@ func (as *AnswerService) Get(ctx context.Context, answerID, loginUserID string,
554554
if !exist {
555555
return nil, nil, false, errors.NotFound(reason.AnswerNotFound)
556556
}
557+
557558
if (question.Status == entity.QuestionStatusDeleted ||
558559
question.Status == entity.QuestionStatusPending ||
559560
question.Show == entity.QuestionHide) &&
560561
!isAdminModerator && question.UserID != loginUserID {
561562
return nil, nil, false, errors.NotFound(reason.AnswerNotFound)
562563
}
564+
if (answerInfo.Status == entity.AnswerStatusDeleted ||
565+
answerInfo.Status == entity.AnswerStatusPending) &&
566+
!isAdminModerator && answerInfo.UserID != loginUserID {
567+
return nil, nil, false, errors.NotFound(reason.AnswerNotFound)
568+
}
563569
info := as.ShowFormat(ctx, answerInfo)
564570
// todo questionFunc
565571
questionInfo, err := as.questionCommon.Info(ctx, answerInfo.QuestionID, loginUserID)

internal/service/user_admin/user_backyard.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
236252
func (us *UserAdminService) AddUser(ctx context.Context, req *schema.AddUserReq) (err error) {
237253
_, has, err := us.userRepo.GetUserInfoByEmail(ctx, req.Email)

0 commit comments

Comments
 (0)