Skip to content

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.

update captcha

aichy12629c253baa374
diff --git a/cmd/wire_gen.go b/cmd/wire_gen.go
index e482d45a..c6473ce0 100644
--- a/cmd/wire_gen.go
+++ b/cmd/wire_gen.go
@@ -180,7 +180,7 @@ func initApplication(debug bool, serverConf *conf.Server, dbConf *data.Database,
searchParser := search_parser.NewSearchParser(tagCommonService, userCommon)
searchRepo := search_common.NewSearchRepo(dataData, uniqueIDRepo, userCommon)
searchService := service.NewSearchService(searchParser, searchRepo)
- searchController := controller.NewSearchController(searchService)
+ searchController := controller.NewSearchController(searchService, captchaService)
serviceRevisionService := service.NewRevisionService(revisionRepo, userCommon, questionCommon, answerService, objService, questionRepo, answerRepo, tagRepo, tagCommonService, notificationQueueService, activityQueueService)
revisionController := controller.NewRevisionController(serviceRevisionService, rankService)
rankController := controller.NewRankController(rankService)
diff --git a/internal/controller/search_controller.go b/internal/controller/search_controller.go
index 5b658351..fb4c19e7 100644
--- a/internal/controller/search_controller.go
+++ b/internal/controller/search_controller.go
@@ -3,19 +3,32 @@ package controller
import (
"github.com/answerdev/answer/internal/base/handler"
"github.com/answerdev/answer/internal/base/middleware"
+ "github.com/answerdev/answer/internal/base/reason"
+ "github.com/answerdev/answer/internal/base/translator"
+ "github.com/answerdev/answer/internal/base/validator"
+ "github.com/answerdev/answer/internal/entity"
"github.com/answerdev/answer/internal/schema"
"github.com/answerdev/answer/internal/service"
+ "github.com/answerdev/answer/internal/service/action"
"github.com/gin-gonic/gin"
+ "github.com/segmentfault/pacman/errors"
)
// SearchController tag controller
type SearchController struct {
searchService *service.SearchService
+ actionService *action.CaptchaService
}
// NewSearchController new controller
-func NewSearchController(searchService *service.SearchService) *SearchController {
- return &SearchController{searchService: searchService}
+func NewSearchController(
+ searchService *service.SearchService,
+ actionService *action.CaptchaService,
+) *SearchController {
+ return &SearchController{
+ searchService: searchService,
+ actionService: actionService,
+ }
}
// Search godoc
@@ -35,9 +48,22 @@ func (sc *SearchController) Search(ctx *gin.Context) {
return
}
dto.UserID = middleware.GetLoginUserIDFromContext(ctx)
+ unit := ctx.ClientIP()
+ if dto.UserID != "" {
+ unit = dto.UserID
+ }
+ captchaPass := sc.actionService.ActionRecordVerifyCaptcha(ctx, entity.CaptchaActionSearch, unit, dto.CaptchaID, dto.CaptchaCode)
+ if !captchaPass {
+ errFields := append([]*validator.FormErrorField{}, &validator.FormErrorField{
+ ErrorField: "captcha_code",
+ ErrorMsg: translator.Tr(handler.GetLang(ctx), reason.CaptchaVerificationFailed),
+ })
+ handler.HandleResponse(ctx, errors.BadRequest(reason.CaptchaVerificationFailed), errFields)
+ return
+ }
resp, total, extra, err := sc.searchService.Search(ctx, &dto)
-
+ sc.actionService.ActionRecordAdd(ctx, entity.CaptchaActionSearch, unit)
handler.HandleResponse(ctx, err, schema.SearchListResp{
Total: total,
SearchResp: resp,
diff --git a/internal/entity/captcha_entity.go b/internal/entity/captcha_entity.go
index bb7c39ce..1de095c4 100644
--- a/internal/entity/captcha_entity.go
+++ b/internal/entity/captcha_entity.go
@@ -16,7 +16,7 @@ const (
)
type ActionRecordInfo struct {
- LastTime int `json:"last_time"`
+ LastTime int64 `json:"last_time"`
Num int `json:"num"`
Config string `json:"config"`
}
diff --git a/internal/repo/captcha/captcha.go b/internal/repo/captcha/captcha.go
index 1f504e33..a332523a 100644
--- a/internal/repo/captcha/captcha.go
+++ b/internal/repo/captcha/captcha.go
@@ -10,7 +10,6 @@ import (
"github.com/answerdev/answer/internal/base/reason"
"github.com/answerdev/answer/internal/entity"
"github.com/answerdev/answer/internal/service/action"
- "github.com/davecgh/go-spew/spew"
"github.com/segmentfault/pacman/errors"
"github.com/segmentfault/pacman/log"
)
@@ -31,9 +30,8 @@ func (cr *captchaRepo) SetActionType(ctx context.Context, unit, actionType, conf
now := time.Now()
cacheKey := fmt.Sprintf("ActionRecord:%s@%s@%s", unit, actionType, now.Format("2006-1-02"))
value := &entity.ActionRecordInfo{}
- value.LastTime = int(now.Unix())
+ value.LastTime = now.Unix()
value.Num = amount
- spew.Dump("====SetActionType====", cacheKey, value)
valueStr, err := json.Marshal(value)
if err != nil {
return nil
diff --git a/internal/schema/search_schema.go b/internal/schema/search_schema.go
index 51ec0f6a..7f3d1867 100644
--- a/internal/schema/search_schema.go
+++ b/internal/schema/search_schema.go
@@ -1,11 +1,13 @@
package schema
type SearchDTO struct {
- UserID string // UserID current login user ID
- Query string `validate:"required,gte=1,lte=60" json:"q" form:"q"` // Query the query string
- Page int `validate:"omitempty,min=1" form:"page,default=1" json:"page"` //Query number of pages
- Size int `validate:"omitempty,min=1,max=50" form:"size,default=30" json:"size"` //Search page size
- Order string `validate:"required,oneof=newest active score relevance" form:"order,default=relevance" json:"order" enums:"newest,active,score,relevance"`
+ UserID string // UserID current login user ID
+ Query string `validate:"required,gte=1,lte=60" json:"q" form:"q"` // Query the query string
+ Page int `validate:"omitempty,min=1" form:"page,default=1" json:"page"` //Query number of pages
+ Size int `validate:"omitempty,min=1,max=50" form:"size,default=30" json:"size"` //Search page size
+ Order string `validate:"required,oneof=newest active score relevance" form:"order,default=relevance" json:"order" enums:"newest,active,score,relevance"`
+ CaptchaID string `json:"captcha_id"` // captcha_id
+ CaptchaCode string `json:"captcha_code"`
}
type SearchObject struct {
diff --git a/internal/service/action/captcha_strategy.go b/internal/service/action/captcha_strategy.go
index 402c6c8b..4a88b50d 100644
--- a/internal/service/action/captcha_strategy.go
+++ b/internal/service/action/captcha_strategy.go
@@ -2,6 +2,7 @@ package action
import (
"context"
+ "time"
"github.com/answerdev/answer/internal/entity"
"github.com/davecgh/go-spew/spew"
@@ -19,116 +20,167 @@ func (cs *CaptchaService) ValidationStrategy(ctx context.Context, unit, actionTy
}
switch actionType {
case entity.CaptchaActionEmail:
- return cs.CaptchaActionEmail(ctx, info)
+ return cs.CaptchaActionEmail(ctx, unit, info)
case entity.CaptchaActionPassword:
- return cs.CaptchaActionPassword(ctx, info)
+ return cs.CaptchaActionPassword(ctx, unit, info)
case entity.CaptchaActionEditUserinfo:
- return cs.CaptchaActionEditUserinfo(ctx, info)
+ return cs.CaptchaActionEditUserinfo(ctx, unit, info)
case entity.CaptchaActionQuestion:
- return cs.CaptchaActionQuestion(ctx, info)
+ return cs.CaptchaActionQuestion(ctx, unit, info)
case entity.CaptchaActionAnswer:
- return cs.CaptchaActionAnswer(ctx, info)
+ return cs.CaptchaActionAnswer(ctx, unit, info)
case entity.CaptchaActionComment:
- return cs.CaptchaActionComment(ctx, info)
+ return cs.CaptchaActionComment(ctx, unit, info)
case entity.CaptchaActionEdit:
- return cs.CaptchaActionEdit(ctx, info)
+ return cs.CaptchaActionEdit(ctx, unit, info)
case entity.CaptchaActionInvitationAnswer:
- return cs.CaptchaActionInvitationAnswer(ctx, info)
+ return cs.CaptchaActionInvitationAnswer(ctx, unit, info)
case entity.CaptchaActionSearch:
- return cs.CaptchaActionSearch(ctx, info)
+ return cs.CaptchaActionSearch(ctx, unit, info)
case entity.CaptchaActionReport:
- return cs.CaptchaActionReport(ctx, info)
+ return cs.CaptchaActionReport(ctx, unit, info)
case entity.CaptchaActionDelete:
- return cs.CaptchaActionDelete(ctx, info)
+ return cs.CaptchaActionDelete(ctx, unit, info)
case entity.CaptchaActionVote:
- return cs.CaptchaActionVote(ctx, info)
+ return cs.CaptchaActionVote(ctx, unit, info)
}
//actionType not found
return false
}
-func (cs *CaptchaService) CaptchaActionEmail(ctx context.Context, actioninfo *entity.ActionRecordInfo) bool {
- // setNum := 0
- // setTime := 0 //seconds
+func (cs *CaptchaService) CaptchaActionEmail(ctx context.Context, unit string, actioninfo *entity.ActionRecordInfo) bool {
// You need a verification code every time
spew.Dump("[CaptchaActionEmail]", actioninfo)
return false
}
-func (cs *CaptchaService) CaptchaActionPassword(ctx context.Context, actioninfo *entity.ActionRecordInfo) bool {
+func (cs *CaptchaService) CaptchaActionPassword(ctx context.Context, unit string, actioninfo *entity.ActionRecordInfo) bool {
spew.Dump("[CaptchaActionPassword]", actioninfo)
- // setNum := 0
- // setTime := 0 //seconds
- return false
+ setNum := 3
+ setTime := int64(60 * 30) //seconds
+ now := time.Now().Unix()
+ if now-actioninfo.LastTime <= setTime || actioninfo.Num >= setNum {
+ return false
+ }
+ if now-actioninfo.LastTime > setTime {
+ cs.captchaRepo.SetActionType(ctx, unit, entity.CaptchaActionPassword, "", 0)
+ }
+ return true
}
-func (cs *CaptchaService) CaptchaActionEditUserinfo(ctx context.Context, actioninfo *entity.ActionRecordInfo) bool {
+func (cs *CaptchaService) CaptchaActionEditUserinfo(ctx context.Context, unit string, actioninfo *entity.ActionRecordInfo) bool {
spew.Dump("[CaptchaActionEditUserinfo]", actioninfo)
- // setNum := 0
- // setTime := 0 //seconds
- return false
+ setNum := 3
+ setTime := int64(60 * 30) //seconds
+ now := time.Now().Unix()
+ if now-actioninfo.LastTime <= setTime || actioninfo.Num >= setNum {
+ return false
+ }
+ if now-actioninfo.LastTime > setTime {
+ cs.captchaRepo.SetActionType(ctx, unit, entity.CaptchaActionEditUserinfo, "", 0)
+ }
+ return true
}
-func (cs *CaptchaService) CaptchaActionQuestion(ctx context.Context, actioninfo *entity.ActionRecordInfo) bool {
+func (cs *CaptchaService) CaptchaActionQuestion(ctx context.Context, unit string, actioninfo *entity.ActionRecordInfo) bool {
spew.Dump("[CaptchaActionQuestion]", actioninfo)
- // setNum := 0
- // setTime := 0 //seconds
- return false
+ setNum := 3
+ setTime := int64(5) //seconds
+ now := time.Now().Unix()
+ if now-actioninfo.LastTime <= setTime || actioninfo.Num >= setNum {
+ return false
+ }
+ return true
}
-func (cs *CaptchaService) CaptchaActionAnswer(ctx context.Context, actioninfo *entity.ActionRecordInfo) bool {
+func (cs *CaptchaService) CaptchaActionAnswer(ctx context.Context, unit string, actioninfo *entity.ActionRecordInfo) bool {
spew.Dump("[CaptchaActionAnswer]", actioninfo)
- // setNum := 0
- // setTime := 0 //seconds
- return false
+ setNum := 10
+ setTime := int64(5) //seconds
+ now := time.Now().Unix()
+ if now-actioninfo.LastTime <= setTime || actioninfo.Num >= setNum {
+ return false
+ }
+ return true
}
-func (cs *CaptchaService) CaptchaActionComment(ctx context.Context, actioninfo *entity.ActionRecordInfo) bool {
+func (cs *CaptchaService) CaptchaActionComment(ctx context.Context, unit string, actioninfo *entity.ActionRecordInfo) bool {
spew.Dump("[CaptchaActionComment]", actioninfo)
- // setNum := 0
- // setTime := 0 //seconds
- return false
+ setNum := 30
+ setTime := int64(1) //seconds
+ now := time.Now().Unix()
+ if now-actioninfo.LastTime <= setTime || actioninfo.Num >= setNum {
+ return false
+ }
+ return true
}
-func (cs *CaptchaService) CaptchaActionEdit(ctx context.Context, actioninfo *entity.ActionRecordInfo) bool {
+func (cs *CaptchaService) CaptchaActionEdit(ctx context.Context, unit string, actioninfo *entity.ActionRecordInfo) bool {
spew.Dump("[CaptchaActionEdit]", actioninfo)
- // setNum := 0
- // setTime := 0 //seconds
- return false
+ setNum := 10
+ // setTime := int64(5) //seconds
+ // now := time.Now().Unix()
+ if actioninfo.Num >= setNum {
+ return false
+ }
+ return true
}
-func (cs *CaptchaService) CaptchaActionInvitationAnswer(ctx context.Context, actioninfo *entity.ActionRecordInfo) bool {
+func (cs *CaptchaService) CaptchaActionInvitationAnswer(ctx context.Context, unit string, actioninfo *entity.ActionRecordInfo) bool {
spew.Dump("[CaptchaActionInvitationAnswer]", actioninfo)
- // setNum := 0
- // setTime := 0 //seconds
- return false
+ setNum := 30
+ // setTime := int64(5) //seconds
+ // now := time.Now().Unix()
+ if actioninfo.Num >= setNum {
+ return false
+ }
+ return true
}
-func (cs *CaptchaService) CaptchaActionSearch(ctx context.Context, actioninfo *entity.ActionRecordInfo) bool {
- spew.Dump("[CaptchaActionSearch]", actioninfo)
- // setNum := 0
- // setTime := 0 //seconds
- return false
+func (cs *CaptchaService) CaptchaActionSearch(ctx context.Context, unit string, actioninfo *entity.ActionRecordInfo) bool {
+ now := time.Now().Unix()
+ setNum := 20
+ setTime := int64(60) //seconds
+ spew.Dump("[CaptchaActionSearch]", unit, actioninfo, now-int64(actioninfo.LastTime))
+ if now-int64(actioninfo.LastTime) <= setTime && actioninfo.Num >= setNum {
+ spew.Dump("in1")
+ return false
+ }
+ if now-actioninfo.LastTime > setTime {
+ cs.captchaRepo.SetActionType(ctx, unit, entity.CaptchaActionSearch, "", 0)
+ }
+ return true
}
-func (cs *CaptchaService) CaptchaActionReport(ctx context.Context, actioninfo *entity.ActionRecordInfo) bool {
- spew.Dump("[CaptchaActionReport]", actioninfo)
- // setNum := 0
- // setTime := 0 //seconds
- return false
+func (cs *CaptchaService) CaptchaActionReport(ctx context.Context, unit string, actioninfo *entity.ActionRecordInfo) bool {
+ setNum := 30
+ setTime := int64(1) //seconds
+ now := time.Now().Unix()
+ spew.Dump("[CaptchaActionReport]", actioninfo, now-int64(actioninfo.LastTime))
+ if now-actioninfo.LastTime <= setTime || actioninfo.Num >= setNum {
+ return false
+ }
+ return true
}
-func (cs *CaptchaService) CaptchaActionDelete(ctx context.Context, actioninfo *entity.ActionRecordInfo) bool {
+func (cs *CaptchaService) CaptchaActionDelete(ctx context.Context, unit string, actioninfo *entity.ActionRecordInfo) bool {
spew.Dump("[CaptchaActionDelete]", actioninfo)
- // setNum := 0
- // setTime := 0 //seconds
- return false
+ setNum := 5
+ setTime := int64(5) //seconds
+ now := time.Now().Unix()
+ if now-actioninfo.LastTime <= setTime || actioninfo.Num >= setNum {
+ return false
+ }
+ return true
}
-func (cs *CaptchaService) CaptchaActionVote(ctx context.Context, actioninfo *entity.ActionRecordInfo) bool {
+func (cs *CaptchaService) CaptchaActionVote(ctx context.Context, unit string, actioninfo *entity.ActionRecordInfo) bool {
spew.Dump("[CaptchaActionVote]", actioninfo)
- // setNum := 0
- // setTime := 0 //seconds
- return false
+ setNum := 40
+ // setTime := int64(5) //seconds
+ if actioninfo.Num >= setNum {
+ return false
+ }
+ return true
}