Exemplo n.º 1
1
func changeAccountPassword(ctx context.Context, w http.ResponseWriter, r *http.Request) {

	var acct account.Account

	var email string
	var newPassword string

	// read the account ID
	if emailBytes, err := base64.RawURLEncoding.DecodeString(rest.Param(ctx, "id")); err != nil {
		rest.WriteJSON(w, err)
		return
	} else {
		email = string(emailBytes)
	}

	if err := rest.ReadJSON(r, &newPassword); err != nil {
		rest.WriteJSON(w, err)
	} else if err := account.Get(ctx, email, &acct); err != nil {
		rest.WriteJSON(w, err)
	} else if err := acct.SetPassword(newPassword); err != nil {
		rest.WriteJSON(w, err)
	} else if err := account.Save(ctx, &acct); err != nil {
		rest.WriteJSON(w, err)
	} else {
		rest.WriteJSON(w, &rest.NoContent)
	}

}
Exemplo n.º 2
1
func changeAccount(ctx context.Context, w http.ResponseWriter, r *http.Request) {

	var acct account.Account
	var resp rest.Response
	resp.Body = &acct

	var email string
	var err error

	// read the account ID
	if emailBytes, err := base64.RawURLEncoding.DecodeString(rest.Param(ctx, "id")); err != nil {
		rest.WriteJSON(w, err)
		return
	} else {
		email = string(emailBytes)
	}

	// read the account and merge it with the request body
	if err = account.Get(ctx, email, &acct); err != nil {
		rest.WriteJSON(w, err)
		return
	} else if err = rest.ReadJSON(r, &acct); err != nil {
		rest.WriteJSON(w, err)
		return
	}

	// is the email address changing?
	if email != acct.Email {

		// run the change, then reread the account
		if err = account.ChangeEmail(ctx, email, acct.Email); err != nil {
			rest.WriteJSON(w, err)
			return
		}

		email = acct.Email

		if err = account.Get(ctx, email, &acct); err != nil {
			rest.WriteJSON(w, err)
			return
		}

		// point to the new URL on response
		newAccountURL, _ := r.URL.Parse("..")
		newAccountURL, _ = newAccountURL.Parse(base64.RawURLEncoding.EncodeToString([]byte(acct.Email)))
		w.Header().Set("Location", newAccountURL.String())
		resp.Code = http.StatusMovedPermanently

	}

	// save the remaining changes to the account
	if err = account.Save(ctx, &acct); err != nil {
		rest.WriteJSON(w, err)
		return
	}

	rest.WriteJSON(w, &resp)

}
Exemplo n.º 3
1
func newAccount(ctx context.Context, w http.ResponseWriter, r *http.Request) {

	var acctReq accountCreationRequest
	if err := rest.ReadJSON(r, &acctReq); err != nil {
		rest.WriteJSON(w, err)
	} else if newAcct, err := account.New(ctx, acctReq.Email, acctReq.Password); err != nil {
		rest.WriteJSON(w, err)
	} else {
		newAccountURL, _ := r.URL.Parse(newAcct.Key(ctx).StringID())
		w.Header().Set("Location", newAccountURL.String())
		rest.WriteJSON(w, rest.CreatedResponse(newAcct))
	}

}
Exemplo n.º 4
1
func changeConfig(ctx context.Context, w http.ResponseWriter, r *http.Request) {

	conf := config.Config{}
	if err := config.Get(ctx, &conf); err != nil {
		rest.WriteJSON(w, err)
	} else if err := rest.ReadJSON(r, &conf); err != nil {
		rest.WriteJSON(w, err)
	} else if err := config.Save(ctx, &conf); err != nil {
		rest.WriteJSON(w, err)
	} else {
		rest.WriteJSON(w, &conf)
	}

}