Exemple #1
0
func (app *App) handleUsersCreate(w http.ResponseWriter, r *http.Request) {
	if r.Method != "POST" {
		http.Error(w, "I only respond to GETs", http.StatusNotImplemented)
		return
	}

	username := r.PostFormValue("username")
	pass := r.PostFormValue("password")
	email := r.PostFormValue("email")
	admin := r.PostFormValue("admin") == "true"

	if username == "" {
		http.Error(w, "No username provided", http.StatusBadRequest)
		return
	}

	if pass == "" {
		http.Error(w, "Empty password provided", http.StatusBadRequest)
		return
	}
	hashedPass := phash.Gen(pass)

	tx, err := app.db.Begin()
	if err != nil {
		app.dbError(w, r, err)
		return
	}
	defer tx.Rollback()

	if _, err := db.NewUser(tx, username, hashedPass, email, admin); err != nil {
		http.Error(w, "Could not save user to database: "+err.Error(),
			http.StatusInternalServerError)
		return
	}
	if err := tx.Commit(); err != nil {
		app.dbError(w, r, err)
		return
	}

	w.WriteHeader(http.StatusOK)
}
Exemple #2
0
// - [x] create N users
// - [x] create N apikeys & read-only apikeys
// - [x] create M models for each user
//     - [x] create J versions for model
//     - [x] create model status for each model
//     - [ ] associate model w/ MPS (?)
//     - [x] give (some) models JSON/HTML examples
//     - [x] share model with K users
func seedFunc(conn *sql.DB) error {
	tx, err := conn.Begin()
	if err != nil {
		return fmt.Errorf("cannot begin transaction %v", err)
	}
	defer tx.Rollback()

	log.Println("truncating tables in db")
	tx.Exec(`use scienceops;`)
	tx.Exec(`SET FOREIGN_KEY_CHECKS=0;`)
	rows, err := tx.Query(`show tables;`)
	if err != nil {
		return fmt.Errorf("could not truncate database: ", err)
	}
	var tables []string

	defer rows.Close()
	for rows.Next() {
		var t string
		rows.Scan(&t)
		tables = append(tables, t)
	}
	for _, t := range tables {
		q := fmt.Sprintf("TRUNCATE TABLE %s;", t)
		if _, err := tx.Exec(q); err != nil {
			fmt.Println(err)
			return fmt.Errorf("Could not truncate table %s: %v", t, err)
		}
	}
	tx.Exec(`SET FOREIGN_KEY_CHECKS=1;`)

	log.Println("seeding db")

	nUsers := 3
	nModels := 10
	nVersions := 15

	os.MkdirAll("/tmp/bundles/", 0777)

	hashedPass := "******"
	for _, user := range []string{"eric", "ryan", "greg", "sush", "colin", "brandon", "austin", "charlie"} {
		_, err := db.NewUser(tx, user, hashedPass, user+"@yhathq.com", true)
		if err != nil {
			return fmt.Errorf("could not create user: %v", err)
		}
	}
	for _, user := range []string{"bigdatabob"} {
		_, err := db.NewUser(tx, user, hashedPass, user+"@yhathq.com", false)
		if err != nil {
			return fmt.Errorf("could not create user: %v", err)
		}
	}

	for i := 0; i < nUsers; i++ {
		username := fmt.Sprintf("user-%d", i)
		email := fmt.Sprintf("*****@*****.**", username)
		user, err := db.NewUser(tx, username, hashedPass, email, true)
		if err != nil {
			return fmt.Errorf("could not create user: %v", err)
		}
		log.Printf("Created user %s", user.Name)

		for j := 0; j < nModels; j++ {
			name := randomdata.SillyName()

			params := &db.NewVersionParams{
				UserId:         user.Id,
				Model:          name,
				Lang:           db.LangPython2,
				SourceCode:     "print HI!",
				BundleFilename: "/foobar/bundle.json",
			}

			for v := 0; v < nVersions; v++ {
				if _, err := db.NewModelVersion(tx, params); err != nil {
					return fmt.Errorf("could not create version: %v", err)
				}
			}
			model, err := db.GetModel(tx, username, name)
			if err != nil {
				return fmt.Errorf("could not get model %s/%s: %v", username, name, err)
			}
			err = db.SetModelStatus(tx, model.Id, "online")
			if err != nil {
				fmt.Println(err)
				return fmt.Errorf("could not insert model status: %v", err)
			}

		}

	}
	log.Printf("added %d users to db\n", nUsers)
	return tx.Commit()
}
Exemple #3
0
func (app *App) handleRegister(w http.ResponseWriter, r *http.Request) {
	// Register is only displayed if there are no users on the system.
	// It is only for the inital login.
	tx, err := app.db.Begin()
	if err != nil {
		app.dbError(w, r, err)
		return
	}
	defer tx.Rollback()

	users, err := db.AllUsers(tx)
	if err != nil {
		app.dbError(w, r, err)
		return
	}
	if len(users) != 0 {
		if r.Method == "GET" {
			http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
		} else {
			http.Error(w, "Unauthorized", http.StatusUnauthorized)
		}
		return
	}

	if r.Method == "GET" {
		app.serveFile("register.html").ServeHTTP(w, r)
		return
	} else if r.Method != "POST" {
		http.Error(w, "I only respond to GET and POSTs", http.StatusNotImplemented)
		return
	}

	username := r.PostFormValue("username")
	pass := r.PostFormValue("password")
	email := r.PostFormValue("email")

	if username == "" {
		http.Error(w, "No username provided", http.StatusBadRequest)
		return
	}

	if pass == "" {
		http.Error(w, "Empty password provided", http.StatusBadRequest)
		return
	}
	hashedPass := phash.Gen(pass)

	user, err := db.NewUser(tx, username, hashedPass, email, true)
	if err != nil {
		http.Error(w, "Could not save user to database: "+err.Error(),
			http.StatusInternalServerError)
		return
	}
	if err := tx.Commit(); err != nil {
		app.dbError(w, r, err)
		return
	}

	u := &User{Id: user.Id, Name: user.Name}
	if err := app.setUser(r, w, u); err != nil {
		http.Error(w, "Failed to set session cookie: "+err.Error(),
			http.StatusInternalServerError)
		return
	}
	w.WriteHeader(http.StatusOK)
}