Example #1
0
func (s *UserSuite) deleteTests(u t.TestUser, c *gc.C) error {
	if err := user.Delete(s.d, u.Email); err != nil {
		return err
	}

	err := user.ValidLogin(s.d, u.Email, util.Key(u.LoginKey))
	c.Assert(err, gc.ErrorMatches, fmt.Sprintf("could not get login for email %q not valid", u.Email))
	return nil
}
Example #2
0
func handleUserDelete(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"))
		email := r.Form.Get("email")
		pwhash := r.Form.Get("pwhash")

		switch {
		case key != "":
			// An admin can delete any user.
			if err := admin.IsAdmin(d, key); err != nil {
				WriteResponse(w, newApiError(err.Error(), err))
				log.Printf("bad admin request: %#v", r)
				return
			}

		case email != "", pwhash != "":
			if err := user.CheckUser(d, email, pwhash); err != nil {
				WriteResponse(w, newApiError(err.Error(), err))
				log.Printf("invalid user %s: %s", email, err.Error())
				return
			}
		default:
			// No key, no email, no pwhash -- no delete.
			WriteResponse(w, newApiError("must pass pwhash and email, or API key", nil))
			log.Printf("invalid user delete request: no values")
			return
		}

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

		log.Printf("user %q deleted", email)
		WriteResponse(w, &user.User{
			Email: email,
		})
	}
}