func HandleAuthCallback(w http.ResponseWriter, r *http.Request) { ctx := GetContext(r) if ctx.Account != nil { http.Redirect(w, r, "/organizations", http.StatusSeeOther) return } user, err := gothic.CompleteUserAuth(w, r) if err != nil { http.Redirect(w, r, "/login", http.StatusSeeOther) return } acc, err := data.GetAccountEmail(user.Email) catch(r, err) if acc == nil { accEmail, err := data.NewAccountEmail(user.Email) catch(r, err) accEmail.Primary = true accEmail.Verified = true accEmail.VerifiedAt = time.Now() nAcc := data.Account{} nAcc.Emails = append(nAcc.Emails, accEmail) err = nAcc.Put() acc = &nAcc } ctx.Session.Values["accountID"] = acc.ID.Hex() ctx.Session.Save(r, w) http.Redirect(w, r, "/organizations", http.StatusSeeOther) }
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) }