adding custom index support

This commit is contained in:
root
2021-04-24 19:49:42 +00:00
parent 28ec488552
commit 74e55eda4e
7 changed files with 45 additions and 31 deletions

View File

@@ -113,6 +113,7 @@ type userJSON struct {
loginIdx string
emailIdx string
phoneIdx string
customIdx string
token string
}

View File

@@ -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)
} else if len(parsedData.phoneIdx) > 0 {
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 {
userBson, err = e.db.lookupUserRecord(parsedData.token)
}

View File

@@ -939,13 +939,15 @@ func (dbobj MySQLDB) initUsers() error {
`loginidx TINYTEXT,`+
`emailidx TINYTEXT,`+
`phoneidx TINYTEXT,`+
`customidx TINYTEXT,`+
`tempcodeexp int,`+
`tempcode int,`+
`data TEXT);`,
`CREATE UNIQUE INDEX users_token ON users (token(36));`,
`CREATE INDEX users_login ON users (loginidx(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)
}

View File

@@ -929,7 +929,7 @@ func (dbobj SQLiteDB) initUsers() error {
loginidx STRING,
emailidx STRING,
phoneidx STRING,
rofields STRING,
customidx STRING,
tempcodeexp int,
tempcode int,
data TEXT
@@ -937,7 +937,8 @@ func (dbobj SQLiteDB) initUsers() error {
`CREATE INDEX users_token ON users (token);`,
`CREATE INDEX users_login ON users (loginidx);`,
`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)
}

View File

@@ -70,6 +70,17 @@ func (e mainEnv) userNew(w http.ResponseWriter, r *http.Request, ps httprouter.P
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)
if err != nil {
returnError(w, r, "internal error", 405, err, event)

View File

@@ -51,6 +51,9 @@ func (dbobj dbcon) createUserRecord(parsedData userJSON, event *auditEvent) (str
if len(parsedData.phoneIdx) > 0 {
bdoc["phoneidx"] = hashString(dbobj.hash, parsedData.phoneIdx)
}
if len(parsedData.customIdx) > 0 {
bdoc["customidx"] = hashString(dbobj.hash, parsedData.customIdx)
}
if event != nil {
event.After = encodedStr
event.Record = userTOKEN
@@ -167,7 +170,7 @@ func (dbobj dbcon) updateUserRecordDo(jsonDataPatch []byte, userTOKEN string, ol
sig := oldUserBson["md5"].(string)
// create new user record
bdoc := bson.M{}
keys := []string{"login", "email", "phone"}
keys := []string{"login", "email", "phone", "custom"}
newEmail := ""
for _, idx := range keys {
//fmt.Printf("Checking %s\n", idx)
@@ -453,6 +456,7 @@ func (dbobj dbcon) deleteUserRecord(userJSON []byte, userTOKEN string) (bool, er
bdel["loginidx"] = ""
bdel["emailidx"] = ""
bdel["phoneidx"] = ""
bdel["customidx"] = ""
}
result, err := dbobj.store.CleanupRecord(storage.TblName.Users, "token", userTOKEN, bdel)
if err != nil {

View File

@@ -12,7 +12,6 @@ import (
"mime"
"net/http"
"net/url"
"reflect"
"regexp"
"strconv"
"strings"
@@ -153,6 +152,9 @@ func validateMode(index string) bool {
if index == "login" {
return true
}
if index == "custom" {
return true
}
return false
}
@@ -475,34 +477,22 @@ func getJSONPostData(r *http.Request) (map[string]interface{}, error) {
}
func getIndexString(val interface{}) string {
if reflect.TypeOf(val) == reflect.TypeOf(nil) {
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)
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 indexValue
return ""
}
*/
func getJSONPost(r *http.Request, defaultCountry string) (userJSON, error) {
var result userJSON
@@ -523,6 +513,9 @@ func getJSONPost(r *http.Request, defaultCountry string) (userJSON, error) {
if value, ok := records["phone"]; ok {
result.phoneIdx = normalizePhone(getIndexString(value), defaultCountry)
}
if value, ok := records["custom"]; ok {
result.customIdx = getIndexString(value)
}
if value, ok := records["token"]; ok {
result.token = value.(string)
}