Ejemplo n.º 1
0
func handleUserCreate(d db.DB) htr.Handle {
	return func(w http.ResponseWriter, r *http.Request, ps htr.Params) {
		if err := r.ParseForm(); err != nil {
			WriteResponse(w, newApiError(err.Error(), err))
			log.Printf("bad admin request: %#v", r)
			return
		}

		key := util.Key(r.Form.Get("key"))
		if err := admin.IsAdmin(d, key); err != nil {
			WriteResponse(w, newApiError(err.Error(), err))
			log.Printf("bad admin request: %#v", r)
			return
		}

		email := r.Form.Get("email")
		pwhash := r.Form.Get("pwhash")

		if err := user.Create(d, email, pwhash); err != nil {
			WriteResponse(w, newApiError(err.Error(), err))
			log.Printf("error creating user %s: %s", email, err.Error())
			return
		}

		log.Printf("user %s created", email)
		WriteResponse(w, &user.User{
			Email: email,
			Hash:  util.Hash(pwhash),
		})
	}
}
Ejemplo n.º 2
0
func (s *UserSuite) createTests(u t.TestUser, c *gc.C) error {
	err := user.Create(s.d, u.Email, u.Pwhash)
	if err != nil {
		return err
	}

	userBytes, err := db.GetByKey(s.d, user.Users, []byte(u.Email))
	if err != nil {
		return err
	}
	c.Assert(len(userBytes), gc.Not(gc.Equals), 0)

	var tmpUser user.User
	err = json.Unmarshal(userBytes, &tmpUser)
	if err != nil {
		return err
	}

	c.Check(tmpUser.Email, gc.Equals, u.Email)
	return nil
}