mirror of
https://github.com/outbackdingo/databunker.git
synced 2026-01-27 18:18:43 +00:00
preserve use records after delete
This commit is contained in:
@@ -132,18 +132,17 @@ func validateUserRecordChange(oldRecord []byte, newRecord []byte, authResult str
|
||||
return adminRecordChanged, nil
|
||||
}
|
||||
|
||||
func cleanupRecord(record []byte) []byte {
|
||||
empty := []byte("{}")
|
||||
func cleanupRecord(record []byte) ([]byte, map[string]interface{}) {
|
||||
if userSchema == nil {
|
||||
return empty
|
||||
return nil, nil
|
||||
}
|
||||
var doc interface{}
|
||||
if err := json.Unmarshal(record, &doc); err != nil {
|
||||
return empty
|
||||
return nil, nil
|
||||
}
|
||||
result := userSchema.Validate(nil, doc)
|
||||
if result.ExtendedResults == nil {
|
||||
return empty
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
doc1 := make(map[string]interface{})
|
||||
@@ -213,20 +212,24 @@ func cleanupRecord(record []byte) []byte {
|
||||
}
|
||||
}
|
||||
|
||||
found := false
|
||||
for _, r := range *result.ExtendedResults {
|
||||
fmt.Printf("path: %s key: %s data: %v\n", r.PropertyPath, r.Key, r.Value)
|
||||
if r.Key == "preserve" {
|
||||
//pointer, _ := jptr.Parse(r.PropertyPath)
|
||||
//data1, _ := pointer.Eval(oldDoc)
|
||||
nested(r.PropertyPath, r.Value)
|
||||
fmt.Printf("current doc1 %v\n", doc1)
|
||||
found = true
|
||||
}
|
||||
}
|
||||
fmt.Printf("final doc1 %v\n", doc1)
|
||||
dataBinary, err := json.Marshal(doc1)
|
||||
fmt.Println(err)
|
||||
if found == false {
|
||||
return nil, nil
|
||||
}
|
||||
//fmt.Printf("final doc1 %v\n", doc1)
|
||||
dataBinary, _ := json.Marshal(doc1)
|
||||
//fmt.Println(err)
|
||||
fmt.Printf("data bin %s\n", dataBinary)
|
||||
return dataBinary
|
||||
return dataBinary, doc1
|
||||
}
|
||||
|
||||
/*******************************************************************/
|
||||
@@ -252,7 +255,7 @@ func (f *IsAdmin) Resolve(pointer jptr.Pointer, uri string) *jsonschema.Schema {
|
||||
}
|
||||
|
||||
func (f *IsAdmin) ValidateKeyword(ctx context.Context, currentState *jsonschema.ValidationState, data interface{}) {
|
||||
fmt.Printf("ValidateKeyword admin %s => %v\n", currentState.InstanceLocation.String(), data)
|
||||
//fmt.Printf("ValidateKeyword admin %s => %v\n", currentState.InstanceLocation.String(), data)
|
||||
currentState.AddExtendedResult("admin", data)
|
||||
}
|
||||
|
||||
@@ -279,7 +282,7 @@ func (f *IsLocked) Resolve(pointer jptr.Pointer, uri string) *jsonschema.Schema
|
||||
}
|
||||
|
||||
func (f *IsLocked) ValidateKeyword(ctx context.Context, currentState *jsonschema.ValidationState, data interface{}) {
|
||||
fmt.Printf("ValidateKeyword locked %s => %v\n", currentState.InstanceLocation.String(), data)
|
||||
//fmt.Printf("ValidateKeyword locked %s => %v\n", currentState.InstanceLocation.String(), data)
|
||||
currentState.AddExtendedResult("locked", data)
|
||||
}
|
||||
|
||||
@@ -306,6 +309,6 @@ func (f *IsPreserve) Resolve(pointer jptr.Pointer, uri string) *jsonschema.Schem
|
||||
}
|
||||
|
||||
func (f *IsPreserve) ValidateKeyword(ctx context.Context, currentState *jsonschema.ValidationState, data interface{}) {
|
||||
fmt.Printf("ValidateKeyword preserve %s => %v\n", currentState.InstanceLocation.String(), data)
|
||||
//fmt.Printf("ValidateKeyword preserve %s => %v\n", currentState.InstanceLocation.String(), data)
|
||||
currentState.AddExtendedResult("preserve", data)
|
||||
}
|
||||
@@ -323,13 +323,62 @@ func (dbobj dbcon) deleteUserRecord(userJSON []byte, userTOKEN string) (bool, er
|
||||
dbobj.store.DeleteRecord(storage.TblName.Audit, "record", userTOKEN)
|
||||
dbobj.store.DeleteRecord(storage.TblName.Sessions, "token", userTOKEN)
|
||||
|
||||
// cleanup user record
|
||||
dataJSON, record := cleanupRecord(userJSON)
|
||||
bdel := bson.M{}
|
||||
bdel["data"] = ""
|
||||
bdel["key"] = ""
|
||||
bdel["loginidx"] = ""
|
||||
bdel["emailidx"] = ""
|
||||
bdel["phoneidx"] = ""
|
||||
if dataJSON != nil {
|
||||
oldUserBson, err := dbobj.lookupUserRecord(userTOKEN)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
userKey := oldUserBson["key"].(string)
|
||||
recordKey, err := base64.StdEncoding.DecodeString(userKey)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
sig := oldUserBson["md5"].(string)
|
||||
bdoc := bson.M{}
|
||||
|
||||
if _, ok := record["email"]; ok {
|
||||
fmt.Printf("Preservice email idx\n")
|
||||
bdoc["emailidx"] = oldUserBson["emailidx"].(string)
|
||||
} else {
|
||||
bdel["emailidx"] = ""
|
||||
}
|
||||
if _, ok := record["phone"]; ok {
|
||||
fmt.Printf("Preservice phone idx\n")
|
||||
bdoc["phoneidx"] = oldUserBson["phoneidx"].(string)
|
||||
} else {
|
||||
bdel["phoneidx"] = ""
|
||||
}
|
||||
if _, ok := record["login"]; ok {
|
||||
fmt.Printf("Preservice login idx\n")
|
||||
bdoc["loginidx"] = oldUserBson["loginidx"].(string)
|
||||
} else {
|
||||
bdel["loginidx"] = ""
|
||||
}
|
||||
encoded, _ := encrypt(dbobj.masterKey, recordKey, dataJSON)
|
||||
encodedStr := base64.StdEncoding.EncodeToString(encoded)
|
||||
bdoc["key"] = userKey
|
||||
bdoc["data"] = encodedStr
|
||||
md5Hash := md5.Sum([]byte(encodedStr))
|
||||
bdoc["md5"] = base64.StdEncoding.EncodeToString(md5Hash[:])
|
||||
bdoc["token"] = userTOKEN
|
||||
result, err := dbobj.store.UpdateRecord2(storage.TblName.Users, "token", userTOKEN, "md5", sig, &bdoc, &bdel)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if result > 0 {
|
||||
return true, nil
|
||||
}
|
||||
return false, nil
|
||||
} else {
|
||||
// cleanup user record
|
||||
bdel["data"] = ""
|
||||
bdel["key"] = ""
|
||||
bdel["loginidx"] = ""
|
||||
bdel["emailidx"] = ""
|
||||
bdel["phoneidx"] = ""
|
||||
}
|
||||
result, err := dbobj.store.CleanupRecord(storage.TblName.Users, "token", userTOKEN, bdel)
|
||||
if err != nil {
|
||||
return false, err
|
||||
|
||||
Reference in New Issue
Block a user