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

aichy1263a12d46c373d
diff --git a/internal/entity/captcha_entity.go b/internal/entity/captcha_entity.go
new file mode 100644
index 00000000..d8520925
--- /dev/null
+++ b/internal/entity/captcha_entity.go
@@ -0,0 +1,21 @@
+package entity
+
+const (
+ CaptchaActionEmail = "email"
+ CaptchaActionPassword = "password"
+ CaptchaActionEditUserinfo = "edit_userinfo"
+ CaptchaActionQuestion = "question"
+ CaptchaActionAnswer = "answer"
+ CaptchaActionComment = "comment"
+ CaptchaActionEdit = "edit"
+ CaptchaActionInvitationAnswer = "invitation_answer"
+ CaptchaActionSearch = "search"
+ CaptchaActionReport = "report"
+ CaptchaActionDelete = "delete"
+ CaptchaActionVote = "vote"
+)
+
+type ActionRecordInfo struct {
+ LastTime int `json:"last_time"`
+ Num int `json:"num"`
+}
diff --git a/internal/repo/captcha/captcha.go b/internal/repo/captcha/captcha.go
index 382e45e7..c7ea9d07 100644
--- a/internal/repo/captcha/captcha.go
+++ b/internal/repo/captcha/captcha.go
@@ -2,12 +2,15 @@ package captcha
import (
"context"
+ "encoding/json"
"fmt"
"time"
"github.com/answerdev/answer/internal/base/data"
"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"
)
@@ -24,26 +27,42 @@ func NewCaptchaRepo(data *data.Data) action.CaptchaRepo {
}
}
-func (cr *captchaRepo) SetActionType(ctx context.Context, ip, actionType string, amount int) (err error) {
- cacheKey := fmt.Sprintf("ActionRecord:%s@", ip)
- err = cr.data.Cache.SetInt64(ctx, cacheKey, int64(amount), 6*time.Minute)
+func (cr *captchaRepo) SetActionType(ctx context.Context, unit, actionType string, amount int) (err error) {
+ 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.Num = amount
+ spew.Dump("====SetActionType====", cacheKey, value)
+ valueStr, err := json.Marshal(value)
+ if err != nil {
+ return nil
+ }
+ err = cr.data.Cache.SetString(ctx, cacheKey, string(valueStr), 6*time.Minute)
if err != nil {
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
}
return
}
-func (cr *captchaRepo) GetActionType(ctx context.Context, ip, actionType string) (amount int, err error) {
- cacheKey := fmt.Sprintf("ActionRecord:%s@", ip)
- res, err := cr.data.Cache.GetInt64(ctx, cacheKey)
+func (cr *captchaRepo) GetActionType(ctx context.Context, unit, actionType string) (actioninfo *entity.ActionRecordInfo, err error) {
+ now := time.Now()
+ cacheKey := fmt.Sprintf("ActionRecord:%s@%s@%s", unit, actionType, now.Format("2006-1-02"))
+ actioninfo = &entity.ActionRecordInfo{}
+ res, err := cr.data.Cache.GetString(ctx, cacheKey)
if err != nil {
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
}
- return int(res), nil
+ err = json.Unmarshal([]byte(res), actioninfo)
+ if err != nil {
+ return actioninfo, nil
+ }
+ return actioninfo, nil
}
-func (cr *captchaRepo) DelActionType(ctx context.Context, ip, actionType string) (err error) {
- cacheKey := fmt.Sprintf("ActionRecord:%s@", ip)
+func (cr *captchaRepo) DelActionType(ctx context.Context, unit, actionType string) (err error) {
+ now := time.Now()
+ cacheKey := fmt.Sprintf("ActionRecord:%s@%s@%s", unit, actionType, now.Format("2006-1-02"))
err = cr.data.Cache.Del(ctx, cacheKey)
if err != nil {
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
diff --git a/internal/repo/repo_test/reason_repo_test.go b/internal/repo/repo_test/reason_repo_test.go
index 4f93b376..6ce371d3 100644
--- a/internal/repo/repo_test/reason_repo_test.go
+++ b/internal/repo/repo_test/reason_repo_test.go
@@ -2,9 +2,10 @@ package repo_test
import (
"context"
+ "testing"
+
"github.com/answerdev/answer/internal/repo/config"
serviceconfig "github.com/answerdev/answer/internal/service/config"
- "testing"
"github.com/answerdev/answer/internal/repo/reason"
"github.com/stretchr/testify/assert"
diff --git a/internal/repo/repo_test/tag_rel_repo_test.go b/internal/repo/repo_test/tag_rel_repo_test.go
index ca3371e8..41b6c611 100644
--- a/internal/repo/repo_test/tag_rel_repo_test.go
+++ b/internal/repo/repo_test/tag_rel_repo_test.go
@@ -2,11 +2,12 @@ package repo_test
import (
"context"
- "github.com/answerdev/answer/internal/repo/unique"
"log"
"sync"
"testing"
+ "github.com/answerdev/answer/internal/repo/unique"
+
"github.com/answerdev/answer/internal/entity"
"github.com/answerdev/answer/internal/repo/tag"
"github.com/stretchr/testify/assert"
diff --git a/internal/service/action/captcha_service.go b/internal/service/action/captcha_service.go
index 046d35ce..edc81667 100644
--- a/internal/service/action/captcha_service.go
+++ b/internal/service/action/captcha_service.go
@@ -6,6 +6,7 @@ import (
"strings"
"github.com/answerdev/answer/internal/base/reason"
+ "github.com/answerdev/answer/internal/entity"
"github.com/answerdev/answer/internal/schema"
"github.com/mojocn/base64Captcha"
"github.com/segmentfault/pacman/errors"
@@ -17,9 +18,9 @@ type CaptchaRepo interface {
SetCaptcha(ctx context.Context, key, captcha string) (err error)
GetCaptcha(ctx context.Context, key string) (captcha string, err error)
DelCaptcha(ctx context.Context, key string) (err error)
- SetActionType(ctx context.Context, ip, actionType string, amount int) (err error)
- GetActionType(ctx context.Context, ip, actionType string) (amount int, err error)
- DelActionType(ctx context.Context, ip, actionType string) (err error)
+ SetActionType(ctx context.Context, unit, actionType string, amount int) (err error)
+ GetActionType(ctx context.Context, unit, actionType string) (actioninfo *entity.ActionRecordInfo, err error)
+ DelActionType(ctx context.Context, unit, actionType string) (err error)
}
// CaptchaService kit service
@@ -37,12 +38,8 @@ func NewCaptchaService(captchaRepo CaptchaRepo) *CaptchaService {
// ActionRecord action record
func (cs *CaptchaService) ActionRecord(ctx context.Context, req *schema.ActionRecordReq) (resp *schema.ActionRecordResp, err error) {
resp = &schema.ActionRecordResp{}
- num, err := cs.captchaRepo.GetActionType(ctx, req.IP, req.Action)
- if err != nil {
- num = 0
- }
- // TODO config num to config file
- if num >= 3 {
+ verificationResult := cs.ValidationStrategy(ctx, req.IP, req.Action)
+ if !verificationResult {
resp.CaptchaID, resp.CaptchaImg, err = cs.GenerateCaptcha(ctx)
resp.Verify = true
}
@@ -74,11 +71,8 @@ func (cs *CaptchaService) UserRegisterVerifyCaptcha(
func (cs *CaptchaService) ActionRecordVerifyCaptcha(
ctx context.Context, actionType string, ip string, id string, VerifyValue string,
) bool {
- num, cahceErr := cs.captchaRepo.GetActionType(ctx, ip, actionType)
- if cahceErr != nil {
- return true
- }
- if num >= 3 {
+ verificationResult := cs.ValidationStrategy(ctx, ip, actionType)
+ if !verificationResult {
if id == "" || VerifyValue == "" {
return false
}
@@ -93,16 +87,16 @@ func (cs *CaptchaService) ActionRecordVerifyCaptcha(
func (cs *CaptchaService) ActionRecordAdd(ctx context.Context, actionType string, ip string) (int, error) {
var err error
- num, cahceErr := cs.captchaRepo.GetActionType(ctx, ip, actionType)
+ info, cahceErr := cs.captchaRepo.GetActionType(ctx, ip, actionType)
if cahceErr != nil {
log.Error(err)
}
- num++
- err = cs.captchaRepo.SetActionType(ctx, ip, actionType, num)
+ info.Num++
+ err = cs.captchaRepo.SetActionType(ctx, ip, actionType, info.Num)
if err != nil {
return 0, err
}
- return num, nil
+ return info.Num, nil
}
func (cs *CaptchaService) ActionRecordDel(ctx context.Context, actionType string, ip string) {
diff --git a/internal/service/action/captcha_strategy.go b/internal/service/action/captcha_strategy.go
new file mode 100644
index 00000000..27cb9f21
--- /dev/null
+++ b/internal/service/action/captcha_strategy.go
@@ -0,0 +1,122 @@
+package action
+
+import (
+ "context"
+
+ "github.com/answerdev/answer/internal/entity"
+ "github.com/davecgh/go-spew/spew"
+)
+
+// ValidationStrategy
+// true pass
+// false need captcha
+func (cs *CaptchaService) ValidationStrategy(ctx context.Context, unit, actionType string) bool {
+ info, err := cs.captchaRepo.GetActionType(ctx, unit, actionType)
+ spew.Dump("[ValidationStrategy=验证策略]", unit, actionType, info, err)
+ if err != nil {
+ //No record, no processing
+ //
+ }
+ switch actionType {
+ case entity.CaptchaActionEmail:
+ return cs.CaptchaActionEmail(ctx, info)
+ case entity.CaptchaActionPassword:
+ return cs.CaptchaActionPassword(ctx, info)
+ case entity.CaptchaActionEditUserinfo:
+ return cs.CaptchaActionEditUserinfo(ctx, info)
+ case entity.CaptchaActionQuestion:
+ return cs.CaptchaActionQuestion(ctx, info)
+ case entity.CaptchaActionAnswer:
+ return cs.CaptchaActionAnswer(ctx, info)
+ case entity.CaptchaActionComment:
+ return cs.CaptchaActionComment(ctx, info)
+ case entity.CaptchaActionEdit:
+ return cs.CaptchaActionEdit(ctx, info)
+ case entity.CaptchaActionInvitationAnswer:
+ return cs.CaptchaActionInvitationAnswer(ctx, info)
+ case entity.CaptchaActionSearch:
+ return cs.CaptchaActionSearch(ctx, info)
+ case entity.CaptchaActionReport:
+ return cs.CaptchaActionReport(ctx, info)
+ case entity.CaptchaActionDelete:
+ return cs.CaptchaActionDelete(ctx, info)
+ case entity.CaptchaActionVote:
+ return cs.CaptchaActionVote(ctx, info)
+
+ }
+ //actionType not found
+ return false
+}
+
+func (cs *CaptchaService) CaptchaActionEmail(ctx context.Context, actioninfo *entity.ActionRecordInfo) bool {
+ // setNum := 0
+ // setTime := 0 //seconds
+ // You need a verification code every time
+ return false
+}
+
+func (cs *CaptchaService) CaptchaActionPassword(ctx context.Context, actioninfo *entity.ActionRecordInfo) bool {
+ // setNum := 0
+ // setTime := 0 //seconds
+ return false
+}
+
+func (cs *CaptchaService) CaptchaActionEditUserinfo(ctx context.Context, actioninfo *entity.ActionRecordInfo) bool {
+ // setNum := 0
+ // setTime := 0 //seconds
+ return false
+}
+
+func (cs *CaptchaService) CaptchaActionQuestion(ctx context.Context, actioninfo *entity.ActionRecordInfo) bool {
+ // setNum := 0
+ // setTime := 0 //seconds
+ return false
+}
+
+func (cs *CaptchaService) CaptchaActionAnswer(ctx context.Context, actioninfo *entity.ActionRecordInfo) bool {
+ // setNum := 0
+ // setTime := 0 //seconds
+ return false
+}
+
+func (cs *CaptchaService) CaptchaActionComment(ctx context.Context, actioninfo *entity.ActionRecordInfo) bool {
+ // setNum := 0
+ // setTime := 0 //seconds
+ return false
+}
+
+func (cs *CaptchaService) CaptchaActionEdit(ctx context.Context, actioninfo *entity.ActionRecordInfo) bool {
+ // setNum := 0
+ // setTime := 0 //seconds
+ return false
+}
+
+func (cs *CaptchaService) CaptchaActionInvitationAnswer(ctx context.Context, actioninfo *entity.ActionRecordInfo) bool {
+ // setNum := 0
+ // setTime := 0 //seconds
+ return false
+}
+
+func (cs *CaptchaService) CaptchaActionSearch(ctx context.Context, actioninfo *entity.ActionRecordInfo) bool {
+ // setNum := 0
+ // setTime := 0 //seconds
+ return false
+}
+
+func (cs *CaptchaService) CaptchaActionReport(ctx context.Context, actioninfo *entity.ActionRecordInfo) bool {
+ // setNum := 0
+ // setTime := 0 //seconds
+ return false
+}
+
+func (cs *CaptchaService) CaptchaActionDelete(ctx context.Context, actioninfo *entity.ActionRecordInfo) bool {
+ // setNum := 0
+ // setTime := 0 //seconds
+ return false
+}
+
+func (cs *CaptchaService) CaptchaActionVote(ctx context.Context, actioninfo *entity.ActionRecordInfo) bool {
+ // setNum := 0
+ // setTime := 0 //seconds
+ return false
+}