Example #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
}
Example #2
0
File: user.go Project: ro78/tmail
				cgCli.BoolFlag{
					Name:  "catchall",
					Usage: "Set this user as catchall for domain",
				},
				cgCli.StringFlag{
					Name:  "quota, q",
					Value: "",
					Usage: "Mailbox quota in bytes (not bits). You can use K,M,G as unit. Eg: 10G mean a quota of 10GB",
				},
			},
			Action: func(c *cgCli.Context) {
				var err error
				if len(c.Args()) < 2 {
					cliDieBadArgs(c)
				}
				err = api.UserAdd(c.Args()[0], c.Args()[1], c.String("q"), c.Bool("m"), c.Bool("r"), c.Bool("catchall"))
				cliHandleErr(err)
				cliDieOk()
			},
		},
		{
			Name:        "del",
			Usage:       "Delete an user",
			Description: "tmail user del USER",
			Action: func(c *cgCli.Context) {
				var err error
				if len(c.Args()) != 1 {
					cliDieBadArgs(c)
				}
				err = api.UserDel(c.Args()[0])
				cliHandleErr(err)