Beispiel #1
0
// usersAdd adds an user
func usersAdd(w http.ResponseWriter, r *http.Request) {
	if !authorized(w, r) {
		return
	}
	p := struct {
		Passwd       string `json: "passwd"`
		AuthRelay    bool   `json: "authRelay"`
		HaveMailbox  bool   `json: "haveMailbox"`
		IsCathall    bool   `json: "isCatchall"`
		MailboxQuota string `json: "mailboxQuota"`
	}{}

	// nil body
	if r.Body == nil {
		httpWriteErrorJson(w, 422, "empty body", "")
		return
	}

	if err := json.NewDecoder(r.Body).Decode(&p); err != nil {
		httpWriteErrorJson(w, 500, "unable to get JSON body", err.Error())
		return
	}

	if err := api.UserAdd(httpcontext.Get(r, "params").(httprouter.Params).ByName("user"), p.Passwd, p.MailboxQuota, p.HaveMailbox, p.AuthRelay, p.IsCathall); err != nil {
		httpWriteErrorJson(w, 422, "unable to create new user", err.Error())
		return
	}
	logInfo(r, "user added "+httpcontext.Get(r, "params").(httprouter.Params).ByName("user"))
	w.Header().Set("Location", httpGetScheme()+"://"+r.Host+"/users/"+httpcontext.Get(r, "params").(httprouter.Params).ByName("user"))
	w.WriteHeader(201)
	return
}
Beispiel #2
0
// usersUpdate used to update user proprieties
// for now tou can only change password
func usersUpdate(w http.ResponseWriter, r *http.Request) {
	if !authorized(w, r) {
		return
	}
	p := struct {
		Passwd string `json:"passwd"`
	}{}

	// body must not be empty
	if r.Body == nil {
		httpWriteErrorJson(w, 422, "empty body", "")
		return
	}

	if err := json.NewDecoder(r.Body).Decode(&p); err != nil {
		httpWriteErrorJson(w, 500, "unable to get JSON body", err.Error())
		return
	}

	if err := api.UserChangePassword(httpcontext.Get(r, "params").(httprouter.Params).ByName("user"), p.Passwd); err != nil {
		httpWriteErrorJson(w, 422, "unable to change user password", err.Error())
		return
	}
	logInfo(r, "password changed for user "+httpcontext.Get(r, "params").(httprouter.Params).ByName("user"))
	w.WriteHeader(204)
	return
}
Beispiel #3
0
// usersDel delete an user
func usersDel(w http.ResponseWriter, r *http.Request) {
	if !authorized(w, r) {
		return
	}
	err := api.UserDel(httpcontext.Get(r, "params").(httprouter.Params).ByName("user"))
	if err == gorm.RecordNotFound {
		httpWriteErrorJson(w, 404, "no such user "+httpcontext.Get(r, "params").(httprouter.Params).ByName("user"), err.Error())
		return
	}
	if err != nil {
		httpWriteErrorJson(w, 500, "unable to del user "+httpcontext.Get(r, "params").(httprouter.Params).ByName("user"), err.Error())
		return
	}

}
Beispiel #4
0
// queueGetMessage get a message by ID
func queueGetMessage(w http.ResponseWriter, r *http.Request) {
	if !authorized(w, r) {
		return
	}
	msgIdStr := httpcontext.Get(r, "params").(httprouter.Params).ByName("id")
	msgIdInt, err := strconv.ParseInt(msgIdStr, 10, 64)
	if err != nil {
		httpWriteErrorJson(w, 500, "unable to get message id", err.Error())
		return
	}

	m, err := api.QueueGetMessage(msgIdInt)
	if err == gorm.RecordNotFound {
		httpWriteErrorJson(w, 404, "no such message "+msgIdStr, "")
		return
	}
	if err != nil {
		httpWriteErrorJson(w, 500, "unable to get message "+msgIdStr, err.Error())
		return
	}
	js, err := json.Marshal(m)
	if err != nil {
		httpWriteErrorJson(w, 500, "unable to get message "+msgIdStr, err.Error())
		return
	}
	httpWriteJson(w, js)
}
Beispiel #5
0
// usersGetOne return one user
func usersGetOne(w http.ResponseWriter, r *http.Request) {
	if !authorized(w, r) {
		return
	}
	user, err := api.UserGetByLogin(httpcontext.Get(r, "params").(httprouter.Params).ByName("user"))
	if err == gorm.RecordNotFound {
		httpWriteErrorJson(w, 404, "no such user "+httpcontext.Get(r, "params").(httprouter.Params).ByName("user"), "")
		return
	}
	if err != nil {
		httpWriteErrorJson(w, 500, "unable to get user "+httpcontext.Get(r, "params").(httprouter.Params).ByName("user"), err.Error())
		return
	}
	js, err := json.Marshal(user)
	if err != nil {
		httpWriteErrorJson(w, 500, "unable to get user "+httpcontext.Get(r, "params").(httprouter.Params).ByName("user"), err.Error())
		return
	}
	httpWriteJson(w, js)
}
Beispiel #6
0
func Params(r *http.Request) map[string]string {
	if rv := httpcontext.Get(r, paramsKey); rv != nil {
		return rv.(map[string]string)
	}
	return nil
}