示例#1
0
文件: rego.go 项目: steveoc64/ksm
// 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
}