on profile chnage send old and new values

This commit is contained in:
stremovsky
2020-01-01 10:06:27 +02:00
parent e35a7dc7f1
commit 31384e9b4d
3 changed files with 17 additions and 16 deletions

View File

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

View File

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

View File

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