Beispiel #1
0
func Register(w http.ResponseWriter, r *http.Request) {
	if utils.GetCurrentUser(r) != nil {
		http.Redirect(w, r, "/", http.StatusFound)
		return
	}

	if r.Method == "POST" {
		username := r.FormValue("username")
		password := r.FormValue("password")
		confirm := r.FormValue("password2")

		var error string
		if password != confirm {
			error = "Passwords don't match"
		}

		if error != "" {
			utils.RenderTemplate(w, r, "register.html", map[string]interface{}{
				"error": error,
			})
			return
		}

		// We're good, let's make it
		db_map := models.GetDbSession()
		user := models.NewUser(username, password)
		err := db_map.Insert(user)

		if err != nil {
			fmt.Printf("[error] Could not insert user (%s)\n", err.Error())
		} else {
			http.Redirect(w, r, "/login", http.StatusFound)
			return
		}
	}

	utils.RenderTemplate(w, r, "register.html", nil)
}
Beispiel #2
0
func Register(w http.ResponseWriter, r *http.Request) {
	if utils.GetCurrentUser(r) != nil {
		http.Redirect(w, r, "/", http.StatusFound)
		return
	}

	if r.Method == "POST" {
		username := r.FormValue("username")
		password := r.FormValue("password")
		confirm := r.FormValue("password2")

		var error string
		if password != confirm {
			error = "Passwords don't match"
		}

		// See if a user with this name already exists
		db := models.GetDbSession()
		count, err := db.SelectInt("SELECT COUNT(*) FROM users WHERE username=$1", username)
		if count > 0 || err != nil {
			error = "This username is already taken."
		}

        if len(username) < 3 {
            error = "Username must be greater than 3 characters."
        }

		if error != "" {
			utils.RenderTemplate(w, r, "register.html", map[string]interface{}{
				"error": error,
			}, nil)
			return
		}

		// We're good, let's make it
		user := models.NewUser(username, password)
		err = db.Insert(user)

		if err != nil {
			fmt.Printf("[error] Could not insert user (%s)\n", err.Error())
			return;
		}

		// Adminify the first user
		id, err := db.SelectInt("SELECT lastval()")
		if err == nil && id == 1 {
			user.GroupId = 2
			count, err = db.Update(user)

			if err != nil {
				fmt.Printf("[error] Could not adminify user (%s)\n", err.Error())
				return;
			}
		}

		http.Redirect(w, r, "/login", http.StatusFound)
		return
	}

	utils.RenderTemplate(w, r, "register.html", nil, nil)
}
Beispiel #3
0
func main() {
	// Get the config file
	var config_path string
	flag.StringVar(&config_path, "config", "gobb.conf", "Specifies the location of a config file")
	run_migrations := flag.Bool("migrate", false, "Runs database migrations")
	ign_migrations := flag.Bool("ignore-migrations", false, "Ignores an out of date database and runs the server anyways")
	serve := flag.Bool("serve", false, "run server")
	useradd := flag.Bool("add-user", false, "add a user")
	var name, password string
	flag.StringVar(&name, "name", "", "username to add")
	flag.StringVar(&password, "password", "", "password new user")
	group := flag.Int64("group", 0, "group of new user (<0 is special group, 0 is default, 1 is mod, 2 is admin)")
	flag.Parse()
	config.GetConfig(config_path)

	// Do we need to run migrations?
	latest_db_version, migrations, err := utils.GetMigrationInfo()
	if len(migrations) != 0 && *run_migrations {
		fmt.Println("[notice] Running database migrations:\n")
		err = utils.RunMigrations(latest_db_version)
		if err != nil {
			fmt.Printf("[error] Could not run migrations (%s)\n", err.Error())
			return
		}

		fmt.Println("\n[notice] Database migration successful!")
	} else if len(migrations) != 0 && !(*ign_migrations) {
		fmt.Println("Your database appears to be out of date. Please run migrations with --migrate or ignore this message with --ignore-migrations")
		return
	}
	db := models.GetDbSession()

	if *useradd {
		user := models.NewUser(name, password)
		user.GroupId = *group
		err = db.Insert(user)

	}

	if !*serve {
		return
	}
	// URL Routing!
	r := mux.NewRouter()
	r.StrictSlash(true)

	r.HandleFunc("/", controllers.Index)
	r.HandleFunc("/register", controllers.Register)
	r.HandleFunc("/login", controllers.Login)
	r.HandleFunc("/logout", controllers.Logout)
	r.HandleFunc("/admin", controllers.Admin)
	r.HandleFunc("/admin/boards", controllers.AdminBoards)
	r.HandleFunc("/admin/users/{id:[0-9]+}", controllers.AdminUser)
	r.HandleFunc("/admin/users", controllers.AdminUsers)
	r.HandleFunc("/action/stick", controllers.ActionStickThread)
	r.HandleFunc("/action/lock", controllers.ActionLockThread)
	r.HandleFunc("/action/delete", controllers.ActionDeleteThread)
	r.HandleFunc("/action/move", controllers.ActionMoveThread)
	r.HandleFunc("/action/mark_read", controllers.ActionMarkAllRead)
	r.HandleFunc("/action/edit", controllers.PostEditor)
	r.HandleFunc("/board/{id:[0-9]+}", controllers.Board)
	r.HandleFunc("/board/{board_id:[0-9]+}/new", controllers.PostEditor)
	r.HandleFunc("/board/{board_id:[0-9]+}/{post_id:[0-9]+}", controllers.Thread)
	r.HandleFunc("/user/{id:[0-9]+}", controllers.User)
	r.HandleFunc("/user/{id:[0-9]+}/settings", controllers.UserSettings)

	// Handle static files
	selected_template, _ := models.GetStringSetting("template")
	base_path, _ := config.Config.GetString("gobb", "base_path")
	if selected_template == "default" {
		pkg, _ := build.Import("github.com/stevenleeg/gobb/gobb", ".", build.FindOnly)
		static_path := filepath.Join(pkg.SrcRoot, pkg.ImportPath, "../templates")
		r.PathPrefix("/static/").Handler(http.FileServer(http.Dir(static_path)))
	} else {
		static_path := filepath.Join(base_path, "templates", selected_template)
		r.PathPrefix("/static/").Handler(http.FileServer(http.Dir(static_path)))
	}

	// User provided static files
	static_path, err := config.Config.GetString("gobb", "base_path")
	if err == nil {
		r.PathPrefix("/assets/").Handler(http.FileServer(http.Dir(static_path)))
	}

	http.Handle("/", r)

	port, err := config.Config.GetString("gobb", "port")
	fmt.Println("[notice] Starting server on port " + port)
	http.ListenAndServe(":"+port, nil)
}