From 82f93c0011ee1a97fce9f01c94fdfcea885cb2f4 Mon Sep 17 00:00:00 2001 From: Patrick Peng Sun Date: Sun, 9 Jul 2017 03:50:28 +1000 Subject: [PATCH] when check cookie we do not retrieve lead record. --- crmpixel.go | 34 ++++++++++++++++++++++------------ crmpixel_test.go | 2 +- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/crmpixel.go b/crmpixel.go index 42670da..cfba8b9 100644 --- a/crmpixel.go +++ b/crmpixel.go @@ -5,9 +5,9 @@ import ( "encoding/json" "errors" "fmt" - "log" "math/rand" "net/http" + "strconv" "strings" "time" ) @@ -20,8 +20,7 @@ func crmpixelImage() (pixel []byte, err error) { func crmpixel(w http.ResponseWriter, r *http.Request) { - cookie, lead := crmpixelCookie(r) - log.Println(lead) + cookie := crmpixelCookie(r) http.SetCookie(w, &cookie) //send out pixel 1x1 transparent png file @@ -33,17 +32,17 @@ func crmpixel(w http.ResponseWriter, r *http.Request) { } } -func crmpixelCookie(r *http.Request) (ret http.Cookie, info crmdLead) { +func crmpixelCookie(r *http.Request) (ret http.Cookie) { cookie, leadok := r.Cookie("biukop_cl") //crm lead if leadok != nil { - ret, info = createNewCookie(r) + ret, _ = createNewCookie(r) return } - valid, info := isValidCookieBiukopCL(cookie.Value) + valid := isValidCookieBiukopCL(cookie.Value) if !valid { - ret, info = createNewCookie(r) + ret, _ = createNewCookie(r) return } @@ -64,10 +63,10 @@ func createNewCookie(r *http.Request) (ret http.Cookie, info crmdLead) { func crmCreateNewAnonymousLeadByHTTPRequest(r *http.Request) (info crmdLead) { info.FirstName = "Anonymous" - info.LastName = "User" + info.LastName = "User " + r.RemoteAddr info.Password = "Password" info.Status = "Anonymous" - info.Description = "Anonymous from " + r.RemoteAddr + info.Description = "Anonymous user from " + r.RemoteAddr info.ForceDuplicate = true b, err := json.Marshal(info) if err != nil { @@ -80,7 +79,7 @@ func crmCreateNewAnonymousLeadByHTTPRequest(r *http.Request) (info crmdLead) { return } -func isValidCookieBiukopCL(cookieValue string) (valid bool, info crmdLead) { +func isValidCookieBiukopCL(cookieValue string) (valid bool) { valid = false //correctly split string @@ -97,11 +96,18 @@ func isValidCookieBiukopCL(cookieValue string) (valid bool, info crmdLead) { return } - //find lead data - info, err := crmpixelLead(id) + ts, err := strconv.Atoi(timestamp) if err != nil { return } + + if timestampOldThan(int32(ts), 86400) { //older than 1 day + //find lead data + _, err := crmpixelLead(id) + if err != nil { + return + } + } valid = true return } @@ -124,6 +130,10 @@ func buildBiukopCLValue(id string) (ret string) { func crmpixelLead(id string) (info crmdLead, err error) { entity, err := crmGetEntity("Lead", id) + if err != nil { + return + } + info, ok := entity.(crmdLead) if !ok { err = errors.New("search lead, return a bad entity type") diff --git a/crmpixel_test.go b/crmpixel_test.go index 4a24dad..e2ae171 100644 --- a/crmpixel_test.go +++ b/crmpixel_test.go @@ -32,7 +32,7 @@ func checkRecorderCookieBiukopCL(v []string) bool { // Extract the dropped cookie from the request. cookie, err := request.Cookie("biukop_cl") - valid, _ := isValidCookieBiukopCL(cookie.Value) + valid := isValidCookieBiukopCL(cookie.Value) return err == nil && valid }