Beispiel #1
0
func (h *Handler) handleClientNew(w http.ResponseWriter, r *http.Request) {
	segments := strings.Split(strings.Trim(r.URL.Path, "/"), "/")
	username := segments[2]

	s, err := session.Parse(r)
	if err != nil {
		http.Redirect(w, r, fmt.Sprintf("%s/u/%s",
			config.Get("baseurl"), username), http.StatusFound)
		return
	}

	if s.User.Username != username {
		http.Redirect(w, r, fmt.Sprintf("%s/u/%s",
			config.Get("baseurl"), username), http.StatusFound)
		return
	}

	m := map[string]interface{}{
		"baseurl": config.Get("baseurl"),
		"session": s,
		"form": map[string]string{
			"email":    s.User.Email,
			"fullname": s.User.Fullname,
		},
	}

	h.Templates.ExecuteTemplate(w, "user_new_client.html", m)
}
Beispiel #2
0
func (h *Handler) handleUserPassword(w http.ResponseWriter, r *http.Request) {
	segments := strings.Split(strings.Trim(r.URL.Path, "/"), "/")
	username := segments[2]

	s, err := session.Parse(r)
	if err != nil {
		http.Redirect(w, r, fmt.Sprintf("%s/u/%s",
			config.Get("baseurl"), username), http.StatusFound)
		return
	}

	if s.User.Username != username {
		http.Redirect(w, r, fmt.Sprintf("%s/u/%s",
			config.Get("baseurl"), username), http.StatusFound)
		return
	}

	msg, _ := flashdata.Get(w, r)

	m := map[string]interface{}{
		"baseurl": config.Get("baseurl"),
		"session": s,
		"message": msg,
	}

	h.Templates.ExecuteTemplate(w, "user_update_password.html", m)
}
Beispiel #3
0
func (h *Handler) handleLogout(w http.ResponseWriter, r *http.Request) {
	s, _ := session.Parse(r)
	if s != nil {
		s.Save(w, false)
	}
	http.Redirect(w, r, config.Get("baseurl"), http.StatusFound)
}
Beispiel #4
0
func (h *Handler) serveServerError(w http.ResponseWriter, r *http.Request) {
	s, _ := session.Parse(r)
	m := map[string]interface{}{
		"baseurl": config.Get("baseurl"),
		"session": s,
	}
	w.WriteHeader(http.StatusInternalServerError)
	h.Templates.ExecuteTemplate(w, "error500.html", m)
}
Beispiel #5
0
func (h *Handler) handleUserUpdatePost(w http.ResponseWriter, r *http.Request) {
	segments := strings.Split(strings.Trim(r.URL.Path, "/"), "/")
	username := segments[2]

	s, err := session.Parse(r)
	if err != nil {
		http.Redirect(w, r, fmt.Sprintf("%s/u/%s",
			config.Get("baseurl"), username), http.StatusFound)
		return
	}

	if s.User.Username != username {
		http.Redirect(w, r, fmt.Sprintf("%s/u/%s",
			config.Get("baseurl"), username), http.StatusFound)
		return
	}

	email := strings.TrimSpace(r.FormValue("email"))
	fullname := strings.TrimSpace(r.FormValue("fullname"))

	// Validate
	v := validation.New()
	if v.Required("email", email, "email is required") {
		v.Email("email", email, "invalid email")
	}

	if len(v.Errors) == 0 {
		err = s.User.Update(email, fullname)
		if err != nil {
			logger.Error(w, err)
			h.serveServerError(w, r)
			return
		}
		flashdata.Set(w, "Profile updated")
		http.Redirect(w, r, fmt.Sprintf("%s/u/%s", config.Get("baseurl"),
			s.User.Username), http.StatusFound)
		return
	}

	m := map[string]interface{}{
		"baseurl": config.Get("baseurl"),
		"session": s,
		"form": map[string]string{
			"email":    email,
			"fullname": fullname,
		},
		"errors": v.Errors,
	}

	h.Templates.ExecuteTemplate(w, "user_update.html", m)
}
Beispiel #6
0
func (h *Handler) handleUserPasswordPost(w http.ResponseWriter, r *http.Request) {
	segments := strings.Split(strings.Trim(r.URL.Path, "/"), "/")
	username := segments[2]

	s, err := session.Parse(r)
	if err != nil {
		http.Redirect(w, r, fmt.Sprintf("%s/u/%s",
			config.Get("baseurl"), username), http.StatusFound)
		return
	}

	if s.User.Username != username {
		http.Redirect(w, r, fmt.Sprintf("%s/u/%s",
			config.Get("baseurl"), username), http.StatusFound)
		return
	}

	password := strings.TrimSpace(r.FormValue("password"))

	// Validate
	v := validation.New()
	v.Required("password", password, "new password is required")

	if len(v.Errors) == 0 {
		err := s.User.UpdatePassword(password)
		if err != nil {
			logger.Error(w, err)
			h.serveServerError(w, r)
			return
		}
		flashdata.Set(w, "Password updated")
		http.Redirect(w, r, fmt.Sprintf("%s/u/%s",
			config.Get("baseurl"), s.User.Username), http.StatusFound)
		return
	}

	m := map[string]interface{}{
		"baseurl": config.Get("baseurl"),
		"session": s,
		"errors":  v.Errors,
	}

	h.Templates.ExecuteTemplate(w, "user_update_password.html", m)
}
Beispiel #7
0
func (h *Handler) handleUser(w http.ResponseWriter, r *http.Request) {
	segments := strings.Split(strings.Trim(r.URL.Path, "/"), "/")
	username := segments[2]

	s, err := session.Parse(r)
	if err != nil {
		logger.Error(w, err)
	} else {
		err = s.Save(w, true)
		if err != nil {
			logger.Error(w, err)
		}
	}

	var owner bool
	var u *user.User
	if s != nil && s.User.Username == username {
		owner = true
		u = s.User
	} else {
		owner = false
		u, err = user.LoadByUsername(username)
		if err != nil {
			logger.Error(w, err)
			h.serveNotFound(w, r)
			return
		}
	}

	msg, _ := flashdata.Get(w, r)

	m := map[string]interface{}{
		"baseurl": config.Get("baseurl"),
		"session": s,
		"message": msg,
		"user":    u,
		"owner":   owner,
	}

	h.Templates.ExecuteTemplate(w, "user.html", m)
}
Beispiel #8
0
func (h *Handler) handleHome(w http.ResponseWriter, r *http.Request) {
	s, err := session.Parse(r)
	if err != nil {
		logger.Error(w, err)
	} else {
		err = s.Save(w, true)
		if err != nil {
			logger.Error(w, err)
		}
	}

	if s != nil {
		url := fmt.Sprintf("%s/u/%s", config.Get("baseurl"), s.User.Username)
		http.Redirect(w, r, url, http.StatusFound)
		return
	}

	m := map[string]interface{}{
		"baseurl": config.Get("baseurl"),
	}

	h.Templates.ExecuteTemplate(w, "home.html", m)
}