예제 #1
0
// SaveHandler saves a item
func SaveHandler(w http.ResponseWriter, r *http.Request) {
	u, _ := auth.GetAuthenticatedUser(r)

	hash := r.FormValue("hash")
	name := r.FormValue("name")
	kind := Open
	folder := r.FormValue("folder")
	created := time.Now()

	if hash == "" {
		hash = crypto.UniqueHash(name)
	}

	room, err := repo.Load(hash)
	if err == nil {
		created = room.Created
	}

	room = &Room{Hash: hash, Name: name, Kind: kind, Folder: folder, Created: created}
	err = repo.Save(room)
	render.Check(err, w)

	// Add members to room
	members := r.Form["members"]
	members = append(members, u.Key)
	for _, user := range members {
		hash = crypto.Hash(room.Hash + user)
		rm := &RoomMember{Hash: hash, User: user, Room: room.Hash}
		err = roomMemberRepo.Save(rm)
		render.Check(err, w)
	}

	http.Redirect(w, r, "/r/"+room.Hash, http.StatusFound)
}
예제 #2
0
파일: main.go 프로젝트: nathanborror/spaces
func messageSaveHandler(w http.ResponseWriter, r *http.Request) {
	au, err := auth.GetAuthenticatedUser(r)
	if err != nil {
		http.Redirect(w, r, "/login", http.StatusFound)
		return
	}

	hash := r.FormValue("hash")
	room := r.FormValue("room")
	text := r.FormValue("text")

	if hash == "" {
		hash = crypto.UniqueHash(text)
	}

	m := &messages.Message{Hash: hash, Room: room, User: au.Key, Text: text}
	err = messageRepo.Save(m)
	render.Check(err, w)

	// Check for any resources in message
	go dropbox.HandleDropboxFilesPut("DMX/Test.gdoc", text, r)

	// Push members
	go push.PushMembers(room, m.Text)

	// Redirect to message (this is kind of a hack so we return the right JSON
	// to the clients connected over websockets).
	http.Redirect(w, r, "/m/"+hash, http.StatusFound)
}
예제 #3
0
// SavePathHandler saves a path for a board
func SavePathHandler(w http.ResponseWriter, r *http.Request) {
	u, _ := auth.GetAuthenticatedUser(r)

	hash := crypto.UniqueHash("")
	board := r.FormValue("board")
	data := r.FormValue("data")

	path := &Path{Hash: hash, Board: board, Data: data, User: u.Key}
	err := pathRepo.Save(path)
	render.Check(err, w)

	http.Redirect(w, r, "/b/"+board, http.StatusFound)
}
예제 #4
0
// SaveHandler saves a board
func SaveHandler(w http.ResponseWriter, r *http.Request) {
	hash := r.FormValue("hash")
	room := r.FormValue("room")
	name := r.FormValue("name")
	created := time.Now()

	if hash == "" {
		hash = crypto.UniqueHash(name)
	}

	board, err := repo.Load(hash)
	if err == nil {
		created = board.Created
	}

	board = &Board{Hash: hash, Room: room, Name: name, Created: created}
	err = repo.Save(board)
	render.Check(err, w)

	http.Redirect(w, r, "/b/"+board.Hash, http.StatusFound)
}
예제 #5
0
// RegisterHandler registers a new user
func RegisterHandler(w http.ResponseWriter, r *http.Request) {
	if r.Method == "POST" {
		name := strings.TrimSpace(r.FormValue("name"))
		email := strings.TrimSpace(r.FormValue("email"))
		password := r.FormValue("password")

		// If email or password are blank then redirect to register page
		// TODO: provide a sensible error to people so they understand what
		// they did wrong.
		if email == "" || password == "" {
			render.Redirect(w, r, "/register")
			return
		}

		// Check to see if person already exists by attempting to log them in.
		passwordHash := crypto.PasswordHash(password)
		user, err := Authenticate(email, password, w, r)

		// If they do exist, redirect them home else create a new user and
		// log them into the site.
		if user != nil {
			render.Redirect(w, r, "/")
			return
		}

		key := crypto.UniqueHash(name)
		user = &User{Key: key, Name: name, Email: email, Password: passwordHash}
		err = repo.Insert(user)
		render.Check(err, w)

		// Auth user and redirect them
		user, _ = Authenticate(email, password, w, r)
		render.Redirect(w, r, "/")
		return
	}

	render.Render(w, r, "auth_register", nil)
}