mirror of
				https://github.com/optim-enterprises-bv/databunker.git
				synced 2025-11-04 03:47:53 +00:00 
			
		
		
		
	adding custom index support
This commit is contained in:
		@@ -113,6 +113,7 @@ type userJSON struct {
 | 
				
			|||||||
	loginIdx string
 | 
						loginIdx string
 | 
				
			||||||
	emailIdx string
 | 
						emailIdx string
 | 
				
			||||||
	phoneIdx string
 | 
						phoneIdx string
 | 
				
			||||||
 | 
						customIdx string
 | 
				
			||||||
	token    string
 | 
						token    string
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -43,6 +43,8 @@ func (e mainEnv) createSession(w http.ResponseWriter, r *http.Request, ps httpro
 | 
				
			|||||||
		userBson, err = e.db.lookupUserRecordByIndex("email", parsedData.emailIdx, e.conf)
 | 
							userBson, err = e.db.lookupUserRecordByIndex("email", parsedData.emailIdx, e.conf)
 | 
				
			||||||
	} else if len(parsedData.phoneIdx) > 0 {
 | 
						} else if len(parsedData.phoneIdx) > 0 {
 | 
				
			||||||
		userBson, err = e.db.lookupUserRecordByIndex("phone", parsedData.phoneIdx, e.conf)
 | 
							userBson, err = e.db.lookupUserRecordByIndex("phone", parsedData.phoneIdx, e.conf)
 | 
				
			||||||
 | 
						} else if len(parsedData.customIdx) > 0 {
 | 
				
			||||||
 | 
							userBson, err = e.db.lookupUserRecordByIndex("custom", parsedData.customIdx, e.conf)
 | 
				
			||||||
	} else if len(parsedData.token) > 0 {
 | 
						} else if len(parsedData.token) > 0 {
 | 
				
			||||||
		userBson, err = e.db.lookupUserRecord(parsedData.token)
 | 
							userBson, err = e.db.lookupUserRecord(parsedData.token)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -939,13 +939,15 @@ func (dbobj MySQLDB) initUsers() error {
 | 
				
			|||||||
		`loginidx TINYTEXT,`+
 | 
							`loginidx TINYTEXT,`+
 | 
				
			||||||
		`emailidx TINYTEXT,`+
 | 
							`emailidx TINYTEXT,`+
 | 
				
			||||||
		`phoneidx TINYTEXT,`+
 | 
							`phoneidx TINYTEXT,`+
 | 
				
			||||||
 | 
							`customidx TINYTEXT,`+
 | 
				
			||||||
		`tempcodeexp int,`+
 | 
							`tempcodeexp int,`+
 | 
				
			||||||
		`tempcode int,`+
 | 
							`tempcode int,`+
 | 
				
			||||||
		`data TEXT);`,
 | 
							`data TEXT);`,
 | 
				
			||||||
		`CREATE UNIQUE INDEX users_token ON users (token(36));`,
 | 
							`CREATE UNIQUE INDEX users_token ON users (token(36));`,
 | 
				
			||||||
		`CREATE INDEX users_login ON users (loginidx(36));`,
 | 
							`CREATE INDEX users_login ON users (loginidx(36));`,
 | 
				
			||||||
		`CREATE INDEX users_email ON users (emailidx(36));`,
 | 
							`CREATE INDEX users_email ON users (emailidx(36));`,
 | 
				
			||||||
		`CREATE INDEX users_phone ON users (phoneidx(36));`}
 | 
							`CREATE INDEX users_phone ON users (phoneidx(36));`,
 | 
				
			||||||
 | 
							`CREATE INDEX users_custom ON users (customidx(36));`}
 | 
				
			||||||
	return dbobj.execQueries(queries)
 | 
						return dbobj.execQueries(queries)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -929,7 +929,7 @@ func (dbobj SQLiteDB) initUsers() error {
 | 
				
			|||||||
			  loginidx STRING,
 | 
								  loginidx STRING,
 | 
				
			||||||
			  emailidx STRING,
 | 
								  emailidx STRING,
 | 
				
			||||||
			  phoneidx STRING,
 | 
								  phoneidx STRING,
 | 
				
			||||||
			  rofields STRING,
 | 
								  customidx STRING,
 | 
				
			||||||
			  tempcodeexp int,
 | 
								  tempcodeexp int,
 | 
				
			||||||
			  tempcode int,
 | 
								  tempcode int,
 | 
				
			||||||
			  data TEXT
 | 
								  data TEXT
 | 
				
			||||||
@@ -937,7 +937,8 @@ func (dbobj SQLiteDB) initUsers() error {
 | 
				
			|||||||
		`CREATE INDEX users_token ON users (token);`,
 | 
							`CREATE INDEX users_token ON users (token);`,
 | 
				
			||||||
		`CREATE INDEX users_login ON users (loginidx);`,
 | 
							`CREATE INDEX users_login ON users (loginidx);`,
 | 
				
			||||||
		`CREATE INDEX users_email ON users (emailidx);`,
 | 
							`CREATE INDEX users_email ON users (emailidx);`,
 | 
				
			||||||
		`CREATE INDEX users_phone ON users (phoneidx);`}
 | 
							`CREATE INDEX users_phone ON users (phoneidx);`,
 | 
				
			||||||
 | 
							`CREATE INDEX users_custom ON users (customidx);`}
 | 
				
			||||||
	return dbobj.execQueries(queries)
 | 
						return dbobj.execQueries(queries)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -70,6 +70,17 @@ func (e mainEnv) userNew(w http.ResponseWriter, r *http.Request, ps httprouter.P
 | 
				
			|||||||
			return
 | 
								return
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						if len(parsedData.customIdx) > 0 {
 | 
				
			||||||
 | 
							otherUserBson, err := e.db.lookupUserRecordByIndex("custom", parsedData.customIdx, e.conf)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								returnError(w, r, "internal error", 405, err, event)
 | 
				
			||||||
 | 
								return
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							if otherUserBson != nil {
 | 
				
			||||||
 | 
								returnError(w, r, "duplicate index: custom", 405, nil, event)
 | 
				
			||||||
 | 
								return
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
	userTOKEN, err := e.db.createUserRecord(parsedData, event)
 | 
						userTOKEN, err := e.db.createUserRecord(parsedData, event)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		returnError(w, r, "internal error", 405, err, event)
 | 
							returnError(w, r, "internal error", 405, err, event)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -51,6 +51,9 @@ func (dbobj dbcon) createUserRecord(parsedData userJSON, event *auditEvent) (str
 | 
				
			|||||||
	if len(parsedData.phoneIdx) > 0 {
 | 
						if len(parsedData.phoneIdx) > 0 {
 | 
				
			||||||
		bdoc["phoneidx"] = hashString(dbobj.hash, parsedData.phoneIdx)
 | 
							bdoc["phoneidx"] = hashString(dbobj.hash, parsedData.phoneIdx)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						if len(parsedData.customIdx) > 0 {
 | 
				
			||||||
 | 
							bdoc["customidx"] = hashString(dbobj.hash, parsedData.customIdx)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
	if event != nil {
 | 
						if event != nil {
 | 
				
			||||||
		event.After = encodedStr
 | 
							event.After = encodedStr
 | 
				
			||||||
		event.Record = userTOKEN
 | 
							event.Record = userTOKEN
 | 
				
			||||||
@@ -167,7 +170,7 @@ func (dbobj dbcon) updateUserRecordDo(jsonDataPatch []byte, userTOKEN string, ol
 | 
				
			|||||||
	sig := oldUserBson["md5"].(string)
 | 
						sig := oldUserBson["md5"].(string)
 | 
				
			||||||
	// create new user record
 | 
						// create new user record
 | 
				
			||||||
	bdoc := bson.M{}
 | 
						bdoc := bson.M{}
 | 
				
			||||||
	keys := []string{"login", "email", "phone"}
 | 
						keys := []string{"login", "email", "phone", "custom"}
 | 
				
			||||||
	newEmail := ""
 | 
						newEmail := ""
 | 
				
			||||||
	for _, idx := range keys {
 | 
						for _, idx := range keys {
 | 
				
			||||||
		//fmt.Printf("Checking %s\n", idx)
 | 
							//fmt.Printf("Checking %s\n", idx)
 | 
				
			||||||
@@ -453,6 +456,7 @@ func (dbobj dbcon) deleteUserRecord(userJSON []byte, userTOKEN string) (bool, er
 | 
				
			|||||||
		bdel["loginidx"] = ""
 | 
							bdel["loginidx"] = ""
 | 
				
			||||||
		bdel["emailidx"] = ""
 | 
							bdel["emailidx"] = ""
 | 
				
			||||||
		bdel["phoneidx"] = ""
 | 
							bdel["phoneidx"] = ""
 | 
				
			||||||
 | 
							bdel["customidx"] = ""
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	result, err := dbobj.store.CleanupRecord(storage.TblName.Users, "token", userTOKEN, bdel)
 | 
						result, err := dbobj.store.CleanupRecord(storage.TblName.Users, "token", userTOKEN, bdel)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										47
									
								
								src/utils.go
									
									
									
									
									
								
							
							
						
						
									
										47
									
								
								src/utils.go
									
									
									
									
									
								
							@@ -12,7 +12,6 @@ import (
 | 
				
			|||||||
	"mime"
 | 
						"mime"
 | 
				
			||||||
	"net/http"
 | 
						"net/http"
 | 
				
			||||||
	"net/url"
 | 
						"net/url"
 | 
				
			||||||
	"reflect"
 | 
					 | 
				
			||||||
	"regexp"
 | 
						"regexp"
 | 
				
			||||||
	"strconv"
 | 
						"strconv"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
@@ -153,6 +152,9 @@ func validateMode(index string) bool {
 | 
				
			|||||||
	if index == "login" {
 | 
						if index == "login" {
 | 
				
			||||||
		return true
 | 
							return true
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						if index == "custom" {
 | 
				
			||||||
 | 
							return true
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
	return false
 | 
						return false
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -475,34 +477,22 @@ func getJSONPostData(r *http.Request) (map[string]interface{}, error) {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func getIndexString(val interface{}) string {
 | 
					func getIndexString(val interface{}) string {
 | 
				
			||||||
	if reflect.TypeOf(val) == reflect.TypeOf(nil) {
 | 
						switch val.(type) {
 | 
				
			||||||
 | 
							case nil:
 | 
				
			||||||
 | 
								return ""
 | 
				
			||||||
 | 
							case string:
 | 
				
			||||||
 | 
								return strings.TrimSpace(val.(string))
 | 
				
			||||||
 | 
							case []uint8:
 | 
				
			||||||
 | 
								return strings.TrimSpace(string(val.([]uint8)))
 | 
				
			||||||
 | 
							case int:
 | 
				
			||||||
 | 
								return strconv.Itoa(val.(int))
 | 
				
			||||||
 | 
							case int64:
 | 
				
			||||||
 | 
								return fmt.Sprintf("%v", val.(int64))
 | 
				
			||||||
 | 
							case float64:
 | 
				
			||||||
 | 
								return strconv.Itoa(int(val.(float64)))
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
	return ""
 | 
						return ""
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
	myType := reflect.TypeOf(val).Kind()
 | 
					 | 
				
			||||||
	newIdxValue := ""
 | 
					 | 
				
			||||||
	if myType == reflect.String {
 | 
					 | 
				
			||||||
		newIdxValue = val.(string)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	if myType == reflect.Int {
 | 
					 | 
				
			||||||
		newIdxValue = strconv.Itoa(val.(int))
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	if myType == reflect.Float64 {
 | 
					 | 
				
			||||||
		newIdxValue = strconv.Itoa(int(val.(float64)))
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	return strings.TrimSpace(newIdxValue)
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
/*
 | 
					 | 
				
			||||||
func getIndexValue(indexName string, val interface{}) (string) {
 | 
					 | 
				
			||||||
	indexValue = getIndexString(val)
 | 
					 | 
				
			||||||
        if indexName == "email" {
 | 
					 | 
				
			||||||
                indexValue = normalizeEmail(indexValue)
 | 
					 | 
				
			||||||
        } else if indexName == "phone" {
 | 
					 | 
				
			||||||
                indexValue = normalizePhone(indexValue, conf.Sms.DefaultCountry)
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
	return indexValue
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
*/
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
func getJSONPost(r *http.Request, defaultCountry string) (userJSON, error) {
 | 
					func getJSONPost(r *http.Request, defaultCountry string) (userJSON, error) {
 | 
				
			||||||
	var result userJSON
 | 
						var result userJSON
 | 
				
			||||||
@@ -523,6 +513,9 @@ func getJSONPost(r *http.Request, defaultCountry string) (userJSON, error) {
 | 
				
			|||||||
	if value, ok := records["phone"]; ok {
 | 
						if value, ok := records["phone"]; ok {
 | 
				
			||||||
		result.phoneIdx = normalizePhone(getIndexString(value), defaultCountry)
 | 
							result.phoneIdx = normalizePhone(getIndexString(value), defaultCountry)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						if value, ok := records["custom"]; ok {
 | 
				
			||||||
 | 
							result.customIdx = getIndexString(value)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
	if value, ok := records["token"]; ok {
 | 
						if value, ok := records["token"]; ok {
 | 
				
			||||||
		result.token = value.(string)
 | 
							result.token = value.(string)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user