From 31384e9b4d57e2fdfd1e925271bb0c974f04dccf Mon Sep 17 00:00:00 2001 From: stremovsky Date: Wed, 1 Jan 2020 10:06:27 +0200 Subject: [PATCH] on profile chnage send old and new values --- src/notify.go | 6 +++--- src/users_api.go | 5 +++-- src/users_db.go | 22 +++++++++++----------- 3 files changed, 17 insertions(+), 16 deletions(-) diff --git a/src/notify.go b/src/notify.go index 02391c0..00dbbbd 100644 --- a/src/notify.go +++ b/src/notify.go @@ -9,12 +9,12 @@ import ( "net/http" ) -func notifyProfileChange(notifyUrl string, profile []byte, mode string, address string) { +func notifyProfileChange(notifyUrl string, old []byte, profile []byte, mode string, address string) { if len(notifyUrl) == 0 { return } - requestBody := fmt.Sprintf(`{"action":"%s","address":"%s","mode":"%s","profile":%s}`, - "profilechange", address, mode, profile) + requestBody := fmt.Sprintf(`{"action":"%s","address":"%s","mode":"%s","old":%s,"profile":%s}`, + "profilechange", address, mode, old, profile) go notify(notifyUrl, []byte(requestBody)) } diff --git a/src/users_api.go b/src/users_api.go index 571f5c5..a55796c 100644 --- a/src/users_api.go +++ b/src/users_api.go @@ -174,14 +174,15 @@ func (e mainEnv) userChange(w http.ResponseWriter, r *http.Request, ps httproute userTOKEN = userBson["token"].(string) event.Record = userTOKEN } - newJSON, err := e.db.updateUserRecord(parsedData, userTOKEN, event, e.conf) + oldJSON, newJSON, err := e.db.updateUserRecord(parsedData, userTOKEN, event, e.conf) if err != nil { returnError(w, r, "internal error", 405, err, event) return } returnUUID(w, userTOKEN) notifyUrl := e.conf.Notification.Profile_notification_url - notifyForgetMe(notifyUrl, newJSON, "token", userTOKEN) + + notifyProfileChange(notifyUrl, oldJSON, newJSON, "token", userTOKEN) } // user forgetme request comes here diff --git a/src/users_db.go b/src/users_db.go index a93bcf8..8154832 100644 --- a/src/users_db.go +++ b/src/users_db.go @@ -128,31 +128,31 @@ func (dbobj dbcon) validateIndexChange(indexName string, idxOldValue string, raw return -1, nil } -func (dbobj dbcon) updateUserRecord(parsedData userJSON, userTOKEN string, event *auditEvent, conf Config) ([]byte, error) { +func (dbobj dbcon) updateUserRecord(parsedData userJSON, userTOKEN string, event *auditEvent, conf Config) ([]byte, []byte, error) { var err error for x := 0; x < 10; x++ { - newJSON, err := dbobj.updateUserRecordDo(parsedData, userTOKEN, event, conf) + oldJSON, newJSON, err := dbobj.updateUserRecordDo(parsedData, userTOKEN, event, conf) if err == nil { - return newJSON, nil + return oldJSON, newJSON, nil } fmt.Printf("Trying to update user again: %s\n", userTOKEN) } - return nil, err + return nil, nil, err } -func (dbobj dbcon) updateUserRecordDo(parsedData userJSON, userTOKEN string, event *auditEvent, conf Config) ([]byte, error) { +func (dbobj dbcon) updateUserRecordDo(parsedData userJSON, userTOKEN string, event *auditEvent, conf Config) ([]byte, []byte, error) { //_, err = collection.InsertOne(context.TODO(), bson.M{"name": "The Go Language2", "genre": "Coding", "authorId": "4"}) oldUserBson, err := dbobj.lookupUserRecord(userTOKEN) if oldUserBson == nil || err != nil { // not found - return nil, err + return nil, nil, err } // get user key userKey := oldUserBson["key"].(string) recordKey, err := base64.StdEncoding.DecodeString(userKey) if err != nil { - return nil, err + return nil, nil, err } encData0 := oldUserBson["data"].(string) encData, err := base64.StdEncoding.DecodeString(encData0) @@ -179,7 +179,7 @@ func (dbobj dbcon) updateUserRecordDo(parsedData userJSON, userTOKEN string, eve if idxOldValue, ok := oldUserBson[idx+"idx"]; ok { loginCode, err = dbobj.validateIndexChange(idx, idxOldValue.(string), raw, conf) if err != nil { - return nil, err + return nil, nil, err } if loginCode == -1 { bdel[idx+"idx"] = "" @@ -191,7 +191,7 @@ func (dbobj dbcon) updateUserRecordDo(parsedData userJSON, userTOKEN string, eve otherUserBson, _ := dbobj.lookupUserRecordByIndex(idx, newIdxValue.(string), conf) if otherUserBson != nil { // already exist user with same index value - return nil, errors.New(fmt.Sprintf("duplicate %s index", idx)) + return nil, nil, errors.New(fmt.Sprintf("duplicate %s index", idx)) } //fmt.Printf("adding index2? %s\n", raw[idx]) // create login index @@ -220,7 +220,7 @@ func (dbobj dbcon) updateUserRecordDo(parsedData userJSON, userTOKEN string, eve //fmt.Printf("op json: %s\n", update) result, err := dbobj.updateRecord2(TblName.Users, "token", userTOKEN, "md5", sig, &bdoc, &bdel) if err != nil { - return nil, err + return nil, nil, err } if event != nil { event.Before = encData0 @@ -232,7 +232,7 @@ func (dbobj dbcon) updateUserRecordDo(parsedData userJSON, userTOKEN string, eve event.Msg = "failed to update" } } - return newJSON, nil + return decrypted, newJSON, nil } func (dbobj dbcon) lookupUserRecord(userTOKEN string) (bson.M, error) {