Exemple #1
0
func ServeProject(w http.ResponseWriter, r *http.Request) {
	ctx := GetContext(r)

	if ctx.Account == nil {
		http.Redirect(w, r, "/login", http.StatusSeeOther)
	}

	vars := mux.Vars(r)
	idStr := vars["id"]
	if !bson.IsObjectIdHex(idStr) {
		ServeNotFound(w, r)
		return
	}
	id := bson.ObjectIdHex(idStr)
	prj, err := data.GetProject(id)
	catch(r, err)
	if prj == nil {
		ServeNotFound(w, r)
		return
	}

	mems, err := prj.Members()
	catch(r, err)

	found := false

	for _, mem := range mems {
		if mem.AccountID == ctx.Account.ID {
			found = true
			break
		}
	}

	if !found {
		ServeForbidden(w, r)
		return
	}

	org, err := prj.Organization()
	catch(r, err)

	docs, err := data.ListDocumentsProject(prj.ID, 0, math.MaxInt32)
	catch(r, err)

	w.Header().Set("Content-Type", mime.TypeByExtension(".html"))
	ServeHTMLTemplate(w, r, tplProjectView, struct {
		Context      *Context
		Organization *data.Organization
		Project      *data.Project
		Members      []data.Member
		Documents    []data.Document
	}{
		Context:      ctx,
		Organization: org,
		Project:      prj,
		Members:      mems,
		Documents:    docs,
	})
}
Exemple #2
0
func HandleDocumentCreate(w http.ResponseWriter, r *http.Request) {
	ctx := GetContext(r)

	if ctx.Account == nil {
		http.Redirect(w, r, "/login", http.StatusSeeOther)
		return
	}
	acc := ctx.Account

	vars := mux.Vars(r)
	idStr := vars["id"]
	if !bson.IsObjectIdHex(idStr) {
		ServeNotFound(w, r)
		return
	}
	id := bson.ObjectIdHex(idStr)
	prj, err := data.GetProject(id)
	catch(r, err)
	if prj == nil {
		ServeNotFound(w, r)
		return
	}
	if acc.ID != prj.OwnerID {
		ServeForbidden(w, r)
		return
	}

	err = r.ParseForm()
	catch(r, err)

	body := struct {
		Title string `schema:"title"`
	}{}

	err = schema.NewDecoder().Decode(&body, r.PostForm)
	catch(r, err)

	switch {
	case body.Title == "":
		RedirectBack(w, r)
		return
	}
	doc := data.Document{
		Title:     body.Title,
		ProjectID: prj.ID,
		Published: false,
	}
	err = doc.Put()
	catch(r, err)

	http.Redirect(w, r, "/projects/"+prj.ID.Hex(), http.StatusSeeOther)
}
Exemple #3
0
func ServeNewDocument(w http.ResponseWriter, r *http.Request) {
	ctx := GetContext(r)

	if ctx.Account == nil {
		http.Redirect(w, r, "/login", http.StatusSeeOther)
		return
	}
	acc := ctx.Account

	vars := mux.Vars(r)
	idStr := vars["id"]
	if !bson.IsObjectIdHex(idStr) {
		ServeNotFound(w, r)
		return
	}
	id := bson.ObjectIdHex(idStr)
	prj, err := data.GetProject(id)
	catch(r, err)
	if prj == nil {
		ServeNotFound(w, r)
		return
	}
	if acc.ID != prj.OwnerID {
		ServeForbidden(w, r)
		return
	}

	org, err := prj.Organization()
	catch(r, err)

	w.Header().Set("Content-Type", mime.TypeByExtension(".html"))
	ServeHTMLTemplate(w, r, tplDocumentNew, struct {
		Context      *Context
		Organization *data.Organization
		Project      *data.Project
	}{
		Context:      ctx,
		Organization: org,
		Project:      prj,
	})
}
Exemple #4
0
func HandleMemberAdd(w http.ResponseWriter, r *http.Request) {
	ctx := GetContext(r)

	if ctx.Account == nil {
		http.Redirect(w, r, "/login", http.StatusSeeOther)
		return
	}

	err := r.ParseForm()
	catch(r, err)

	vars := mux.Vars(r)
	idStr := vars["id"]
	if !bson.IsObjectIdHex(idStr) {
		ServeNotFound(w, r)
		return
	}
	id := bson.ObjectIdHex(idStr)
	prj, err := data.GetProject(id)
	catch(r, err)
	if prj == nil {
		ServeNotFound(w, r)
		return
	}

	if prj.OwnerID != ctx.Account.ID {
		ServeForbidden(w, r)
		return
	}

	body := struct {
		Email string `schema:"email"`
	}{}

	err = schema.NewDecoder().Decode(&body, r.PostForm)
	catch(r, err)

	acc, err := data.GetAccountEmail(body.Email)
	catch(r, err)

	if acc == nil {
		RedirectBack(w, r)
		return
	}

	mem, err := data.GetMemberProjectAccount(prj.ID, acc.ID)
	catch(r, err)

	if mem != nil {
		RedirectBack(w, r)
		return
	}

	nM := data.Member{
		OrganizationID: prj.OrganizationID,
		ProjectID:      prj.ID,
		AccountID:      acc.ID,
		InviterID:      ctx.Account.ID,
		InvitedAt:      time.Now(),
	}
	err = nM.Put()
	catch(r, err)

	mems, err := data.ListMembersProject(prj.ID, 0, math.MaxInt32)
	catch(r, err)

	prj.MemberIDs = []bson.ObjectId{}
	for _, mem := range mems {
		prj.MemberIDs = append(prj.MemberIDs, mem.ID)
	}
	err = prj.Put()
	catch(r, err)

	http.Redirect(w, r, "/projects/"+prj.ID.Hex(), http.StatusSeeOther)
}