update captcha

This commit is contained in:
aichy126 2023-07-18 18:11:18 +08:00
parent b6a85b06ab
commit 29c253baa3
6 changed files with 152 additions and 74 deletions

View File

@ -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)

View File

@ -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,

View File

@ -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"`
}

View File

@ -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

View File

@ -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 {

View File

@ -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
}