Пример #1
0
//@URL: /users @method:POST
func (uc *UserController) Register() {
	restweb.Logger.Debug("User Register")

	var one model.User
	userModel := model.UserModel{}

	uid := uc.Input.Get("user[handle]")
	nick := uc.Input.Get("user[nick]")
	pwd := uc.Input.Get("user[password]")
	pwdConfirm := uc.Input.Get("user[confirmPassword]")
	one.Mail = uc.Input.Get("user[mail]")
	one.School = uc.Input.Get("user[school]")
	one.Motto = uc.Input.Get("user[motto]")

	valid := restweb.Validation{}
	valid.MinSize(uid, 4, "uid")
	valid.Match(uid, "\\w+", "uid")

	if !valid.HasError {
		_, err := userModel.Detail(uid)
		if err != nil && err != model.NotFoundErr {
			http.Error(uc.W, err.Error(), 500)
			return
		} else if err == nil {
			valid.AppendError("uid", "Handle is currently in use.")
		}
	}

	valid.Required(nick, "nick")
	valid.MinSize(pwd, 6, "pwd")
	valid.Equal(pwd, pwdConfirm, "pwdConfirm")
	valid.Mail(one.Mail, "mail")

	if !valid.HasError {
		one.Uid = uid
		one.Nick = nick
		one.Pwd = pwd
		one.Privilege = config.PrivilegePU

		err := userModel.Insert(one)
		if err != nil {
			uc.Error(err.Error(), 500)
			return
		}

		uc.W.Header().Add("Location", "/users/"+uid)
		uc.W.WriteHeader(201)
	} else {
		hint := valid.RenderErrMap()
		b, _ := json.Marshal(&hint)
		uc.W.WriteHeader(400)
		uc.W.Write(b)
	}
}
Пример #2
0
func (this *UserController) Register(w http.ResponseWriter, r *http.Request) {
	class.Logger.Debug("User Register")
	this.Init(w, r)

	var one model.User
	userModel := model.UserModel{}

	uid := r.FormValue("user[handle]")
	nick := r.FormValue("user[nick]")
	pwd := r.FormValue("user[password]")
	pwdConfirm := r.FormValue("user[confirmPassword]")
	one.Mail = r.FormValue("user[mail]")
	one.School = r.FormValue("user[school]")
	one.Motto = r.FormValue("user[motto]")

	ok := 1
	hint := make(map[string]string)

	if uid == "" {
		ok, hint["uid"] = 0, "Handle should not be empty."
	} else {
		qry := make(map[string]string)
		qry["uid"] = uid
		ret, err := userModel.List(qry)
		if err != nil {
			http.Error(w, err.Error(), 500)
		} else if len(ret) > 0 {
			ok, hint["uid"] = 0, "This handle is currently in use."
		}
	}
	if nick == "" {
		ok, hint["nick"] = 0, "Nick should not be empty."
	}
	if len(pwd) < 6 {
		ok, hint["pwd"] = 0, "Password should contain at least six characters."
	}
	if pwd != pwdConfirm {
		ok, hint["pwdConfirm"] = 0, "Confirmation mismatched."
	}
	if ok == 1 {
		one.Uid = uid
		one.Nick = nick
		one.Pwd = pwd
		//one.Privilege = config.PrivilegePU
		one.Privilege = config.PrivilegeAD

		err := userModel.Insert(one)
		if err != nil {
			http.Error(w, err.Error(), 500)
			return
		}

		this.SetSession(w, r, "Uid", uid)
		this.SetSession(w, r, "Privilege", "1")
		w.WriteHeader(200)
	} else {
		b, err := json.Marshal(&hint)
		if err != nil {
			http.Error(w, "json error", 500)
			return
		}

		w.WriteHeader(400)
		w.Write(b)
	}
}