// Create a new user record func createUser(users *db.Col, user UserData) (int, string) { // Do some trivial checking on the email and password before processing if !strings.Contains(user.Email, "@") || !strings.Contains(user.Email, ".") { log.Println(user.Email, "has no @ or .") return 403, "Invalid Email Address" } if len(user.Password) < 1 { log.Println("Empty Password") return 403, "Empty Password" } _, err := users.Insert(map[string]interface{}{ "Email": user.Email, "Password": user.Password, "Valid": false, "LoggedIn": false, "Location": user.Location, "Credit": 0, "Scale": user.Scale, "Armies": user.Armies, "GameTime": user.GameTime, }) if err != nil { panic(err) } log.Println("Registering User:"******"Added User" }
// Lookup the user record for the given email address func lookupUser(users *db.Col, email string) dbRow { var query interface{} queryStr := `{"eq": "` + email + `", "in": ["Email"]}` json.Unmarshal([]byte(queryStr), &query) queryResult := make(map[int]struct{}) if err := db.EvalQuery(query, users, &queryResult); err != nil { panic(err) } for id := range queryResult { results, err := users.Read(id) if err != nil { panic(err) } return results } return nil }
// Create a new user record func validateUser(users *db.Col, params martini.Params) (int, string) { var user UserData retval := 404 resultStr := "Not Found" theId := 0 // Check each user for a match users.ForEachDoc(func(id int, userContent []byte) (willMoveOn bool) { json.Unmarshal(userContent, &user) if !user.Valid { checksum := fmt.Sprintf("%x", md5.Sum([]byte(user.Email+user.Password))) if checksum == params["key"] { retval = 202 resultStr = user.Email theId = id } return false // dont process any more records, as we found the right one } return true // process the next record }) // Update the record here, to avoid deadlock waiting for lock mutex on table if retval == 202 { // Now update the user record var newRow dbRow user.Valid = true jsonUser, err := json.Marshal(user) json.Unmarshal(jsonUser, &newRow) err = users.Update(theId, newRow) if err != nil { panic(err) } } return retval, resultStr }