Exemple #1
0
func (self *Settings) Password() {

	flash := beego.NewFlash()
	self.TplNames = "settings.html"

	sess_userid, _ := self.GetSession("userid").(int64)
	curpass := self.GetString("curpass")
	newpassword := self.GetString("password")
	newrepassword := self.GetString("repassword")

	if curpass == "" {
		flash.Error("当前密码不能为空!")
		flash.Store(&self.Controller)

		self.Redirect("/settings/", 302)
		return
	}

	if newpassword == "" {
		flash.Error("设置密码不能为空!")
		flash.Store(&self.Controller)

		self.Redirect("/settings/", 302)
		return
	}

	if newrepassword == "" {
		flash.Error("重验设置密码不能为空!")
		flash.Store(&self.Controller)

		self.Redirect("/settings/", 302)
		return
	}

	if newpassword != newrepassword {
		flash.Error("两次密码不一致!")
		flash.Store(&self.Controller)

		self.Redirect("/settings/", 302)
		return
	}

	if helper.CheckPassword(curpass) == false {
		flash.Error("当前密码含有非法字符或当前密码过短(至少4~30位密码)!")
		flash.Store(&self.Controller)

		self.Redirect("/settings/", 302)
		return

	}

	if helper.CheckPassword(newpassword) == false {
		flash.Error("设置密码含有非法字符或设置密码过短(至少4~30位密码)!")
		flash.Store(&self.Controller)

		self.Redirect("/settings/", 302)
		return

	}

	if usrinfo, err := model.GetUser(sess_userid); usrinfo != nil && err == nil {

		if helper.Validate_hash(usrinfo.Password, curpass) {
			usrinfo.Password = helper.Encrypt_hash(newpassword, nil)

			if _, err := model.PutUser(usrinfo.Id, usrinfo); err == nil {
				flash.Notice("设置密码成功~")
			} else {
				flash.Error("设置密码失败~")
			}
			flash.Store(&self.Controller)

			self.Redirect("/settings/", 302)
			return
		} else {

			flash.Error("密码无法通过校验~")
			flash.Store(&self.Controller)

			self.Redirect("/settings/", 302)
			return
		}
	} else {

		flash.Error("该账号不存在~")
		flash.Store(&self.Controller)

		self.Redirect("/settings/", 302)
		return
	}

}
Exemple #2
0
func (self *SignupHandler) Post() {

	self.TplNames = "signup.html"

	flash := beego.NewFlash()
	email := strings.ToLower(self.GetString("email"))
	username := strings.ToLower(self.GetString("username"))
	password := self.GetString("password")
	repassword := self.GetString("repassword")

	if password == "" {
		flash.Error("密码为空~")
		flash.Store(&self.Controller)

		return

	}

	if password != repassword {
		flash.Error("两次密码不匹配~")
		flash.Store(&self.Controller)

		return

	}

	if helper.CheckPassword(password) == false {
		flash.Error("密码含有非法字符或密码过短(至少4~30位密码)!")
		flash.Store(&self.Controller)

		return

	}

	if username == "" {
		flash.Error("用户名是为永久性设定,不能少于4个字或多于30个字,请慎重考虑,不能为空~")
		flash.Store(&self.Controller)

		return

	}

	if helper.CheckUsername(username) == false {
		flash.Error("用户名是为永久性设定,不能少于4个字或多于30个字,请慎重考虑,不能为空~")
		flash.Store(&self.Controller)

		return
	}

	if helper.CheckEmail(email) == false {
		flash.Error("Email格式不合符规格~")
		flash.Store(&self.Controller)

		return

	}

	if usrinfo, err := model.GetUserByEmail(email); usrinfo != nil {

		flash.Error("此账号不能使用~")
		flash.Store(&self.Controller)

		return

	} else if err != nil {

		flash.Error("检索账号期间出错~")
		flash.Store(&self.Controller)

		return
	}

	if usrid, err := model.AddUser(email, username, "", "", helper.Encrypt_hash(password, nil), 1); err != nil {
		flash.Error("用户注册信息写入数据库时发生错误~")
		flash.Store(&self.Controller)

		return

	} else {

		if usrinfo, err := model.GetUser(usrid); err == nil {

			//注册账号成功,以下自动登录并设置session
			self.SetSession("userid", usrid)
			self.SetSession("username", usrinfo.Username)
			self.SetSession("userrole", usrinfo.Role)
			self.SetSession("useremail", usrinfo.Email)
			self.SetSession("usercontent", usrinfo.Content)

			flash.Notice("账号登录成功~")
			flash.Store(&self.Controller)

			//session 写入后直接跳到首页
			self.Redirect("/", 302)

		} else {

			flash.Notice("注册账号成功,请手动登录~")
			flash.Store(&self.Controller)

			//注册成功后直接跳转到登录页
			self.Redirect("/signin/", 302)

		}

	}

}
Exemple #3
0
func (self *SigninHandler) Post() {
	self.TplNames = "signin.html"

	flash := beego.NewFlash()
	email := self.GetString("email")
	password := self.GetString("password")
	remember := self.GetString("remember")
	rememberbyform := self.GetString("remember")

	if email == "" {
		flash.Error("EMAIL为空~")
		flash.Store(&self.Controller)

		return

	}

	if password == "" {
		flash.Error("密码为空~")
		flash.Store(&self.Controller)

		return

	}

	if helper.CheckEmail(email) == false {
		flash.Error("Email格式不合符规格~")
		flash.Store(&self.Controller)

		return

	}

	if helper.CheckPassword(password) == false {
		flash.Error("密码含有非法字符或密码过短(至少4~30位密码)!")
		flash.Store(&self.Controller)

		return

	}

	if usrinfo, err := model.GetUserByEmail(email); usrinfo != nil && err == nil {

		if helper.Validate_hash(usrinfo.Password, password) {

			//登录成功设置session
			self.SetSession("userid", usrinfo.Id)
			self.SetSession("username", usrinfo.Username)
			self.SetSession("userrole", usrinfo.Role)
			self.SetSession("useremail", usrinfo.Email)
			self.SetSession("usercontent", usrinfo.Content)

			//设置cookie
			if remember == "on" && rememberbyform == "on" {

				self.Ctx.SetCookie("remember", "on", 31536000, "/")
			} else {

				self.Ctx.SetCookie("remember", "off", 31536000, "/")
			}
			self.Redirect("/", 302)
		} else {

			flash.Error("密码无法通过校验~")
			flash.Store(&self.Controller)
			return
		}
	} else {

		flash.Error("该账号不存在~")
		flash.Store(&self.Controller)
		return
	}
}