Esempio n. 1
0
func SaveProfile(p *profile.Profile) (err error) {
	if !Connected() {
		return ErrNotConnected
	}

	// Remove unselected communities
	cNew := make([]communities.Community, 0, len(p.Communities))
	for _, com := range p.Communities {
		if com.Selected {
			cNew = append(cNew, com)
		}
	}
	p.Communities = cNew

	// Lowercase username - makes searches easier
	p.Username = strings.ToLower(p.DisplayUsername)

	if p.Id == "" {
		err = c.Profile.Insert(p)
	} else {
		err = c.Profile.Update(p.Id, p)
	}
	return
}
Esempio n. 2
0
func HandleSetup(w http.ResponseWriter, r *http.Request) {
	// Grab Profile from cookie set in HandleLoginOAuth
	p := profile.Profile{}
	if cookie, err := r.Cookie("Profile"); err == nil {
		if err = sCookie.Decode(cookie.Name, cookie.Value, &p); err != nil {
			log.Print(err)
		}
	}
	// Make sure we got the profile out of the cookie
	if p.GoogleId == "" {
		http.Redirect(w, r, "/cannotGetCookie", http.StatusTemporaryRedirect)
		return
	}

	p.Communities = communities.All()

	e := newExtra()

	if r.Method == "POST" {
		if err := r.ParseForm(); err != nil {
			log.Println(err)
		}
		// Fill the profile communities with all, filter out falses later
		if err := setupDecoder.Decode(&p, r.PostForm); err != nil {
			log.Println(err)
		}
		if err := validateUsername(&p); err != "" {
			e.Errors["DisplayUsername"] = err
		}
		if err := validateCommunities(&p); err != "" {
			e.Errors["Communities"] = err
		}
		// Insert new profile
		if len(e.Errors) == 0 {
			if err := db.SaveProfile(&p); err != nil {
				e.Errors["Overall"] = err.Error()
			}
		}

		if len(e.Errors) == 0 {
			encoded, err := sCookie.Encode("Profile", p)
			if err != nil {
				log.Print(err)
				http.Redirect(w, r, "/cannotSetCookie", http.StatusTemporaryRedirect)
				return
			}
			http.SetCookie(w, &http.Cookie{
				Name:  "Profile",
				Value: encoded,
				Path:  "/",
			})
			http.Redirect(w, r, "/setupThanks", http.StatusTemporaryRedirect)
			return
		}
	}

	ctx := &parser.Context{
		Title:       "Setup Your Account",
		Description: "Establish your account details",
		Profile:     p,
		Extra:       e,
	}

	if err := parser.Render(w, ctx, "setup.html"); err != nil {
		log.Println(err)
	}
}