Exemplo n.º 1
0
//用户登录
func (this *User) Login() {
	json := this.ReqJson()
	if json != nil {
		email := json.Get("email").MustString()
		password := json.Get("password").MustString()

		valid := validation.Validation{}
		valid.Email(email, "email")
		valid.MinSize(password, 6, "passwordMin")
		valid.MaxSize(password, 12, "passwordMax")

		if valid.HasErrors() {
			this.CustomAbort(enum.BadRequest.Code(), enum.BadRequest.Str())
		}

		u := user.GetUserByEmail(email)
		if u == nil {
			//用户不存在
			this.RespJson(enum.UserNotExist, nil)
		} else if util.Md5(u.Salt+password) != u.Password {
			//密码错误
			this.RespJson(enum.PasswordIncorrect, nil)
		} else {
			this.SetSession("uId", u.Id.Hex())
			user.SetToken(u.Id, this.StartSession().SessionID()) //using cookie as token
			this.RespJson(enum.OK, map[string]interface{}{"url": "/"})
		}
	}
}
Exemplo n.º 2
0
/**
 * 登录接收
 */
func (this *UserController) Signup() {
	//errmsg := make(map[string]string)
	requestEmail := this.GetString("email")
	requestPassword := this.GetString("password")

	svalid := validation.Validation{}

	svalid.Required(requestEmail, "email")
	svalid.MaxSize(requestEmail, 40, "email")
	svalid.Required(requestPassword, "password")

	if svalid.HasErrors() {
		for _, err := range svalid.Errors {
			//fmt.Println(err.Key, err.Message)
			this.showmsg("Error Message", "["+err.Key+"]"+err.Message)
		}
	}

	var user models.User
	user.Email = requestEmail
	user.Password = models.Md5([]byte(requestPassword))

	var Ru models.ReturnUser
	Ru = user.Select()

	if Ru.Id > 0 {
		this.SetSession("nickname", Ru.Nickname)
		this.SetSession("email", Ru.Email)
		this.SetSession("isadmin", Ru.Isadmin)
		this.Redirect("/", 302)
	} else {
		this.showmsg("Error", "Email OR Password IS WRONG")
	}
}
Exemplo n.º 3
0
// Valid user add
func (u *User) ValidUserAdd() *validation.Validation {
	valid := validation.Validation{}

	u.Username = strings.TrimSpace(u.Username)
	u.Password = strings.TrimSpace(u.Password)

	valid.Required(u.Username, "UsernameReq").Message("用户名不能为空")
	valid.MinSize(u.Username, 6, "UsernameMixSize").Message("用户名不能小于6个字符")
	valid.MaxSize(u.Username, 10, "UsernameMaxSize").Message("用户姓名不能大于10个字符")

	valid.MinSize(u.Password, 6, "PasswordMinSize").Message("用户密码不能小于6为字符")
	valid.MaxSize(u.Password, 10, "PasswordMaxSize").Message("用户密码不能大于10个字符")

	// uLen := len(strings.TrimSpace(u.Username))
	// if uLen < 6 || uLen > 10 {
	// valid.SetError("Username", "用户不能为空,长度大于6小于10")
	// }

	// pLen := len(strings.TrimSpace(u.Password))
	// if pLen < 6 || pLen > 10 {
	// valid.SetError("Password", "用户密码不能为空,长度大于6小于10")
	// }

	return &valid
}
Exemplo n.º 4
0
//用户注册
func (this *User) Register() {
	req := this.ReqJson()
	if req != nil {
		email := req.Get("email").MustString()
		nickName := req.Get("nickName").MustString()
		password := req.Get("password").MustString()

		valid := validation.Validation{}
		valid.Email(email, "email")
		valid.MinSize(nickName, 6, "nickNameMin")
		valid.MaxSize(nickName, 12, "nickNameMax")
		valid.MinSize(password, 6, "passwordMin")
		valid.MaxSize(password, 12, "passwordMax")

		if valid.HasErrors() {
			this.CustomAbort(enum.BadRequest.Code(), enum.BadRequest.Str())
		}

		err := user.AddUser(email, nickName, password)
		if err == nil {
			this.RespJson(enum.OK, nil)
		} else {
			if strings.Contains(err.Error(), "email") {
				this.RespJson(enum.EmailAlreadyExist, nil)
			} else if strings.Contains(err.Error(), "nickname") {
				this.RespJson(enum.NickNameAlreadyExist, nil)
			} else {
				beego.Error(err)
			}
		}
	}
}
Exemplo n.º 5
0
func (this *UserController) Login() {
	if this.Ctx.Input.Method() == "GET" {
		if this.GetSession("user") != nil {
			this.Redirect("/", 302)
		}
		this.TplNames = "console/login.html"
		return
	}

	json := this.ReqJson()
	email := json.Get("email").MustString()
	password := json.Get("password").MustString()

	valid := validation.Validation{}
	valid.Email(email, "email")
	valid.MinSize(password, 6, "passwordMin")
	valid.MaxSize(password, 12, "passwordMax")

	if valid.HasErrors() {
		this.CustomAbort(enum.RespCode.BadRequest.Code(), enum.RespCode.BadRequest.Str())
	}

	user := user.GetUserByEmail(email)
	if user == nil {
		//用户不存在
		this.RespJson(enum.RespCode.UserNotExist, nil)
	} else if util.Md5(user.Salt+password) != user.Password {
		//密码错误
		this.RespJson(enum.RespCode.PasswordIncorrect, nil)
	} else {
		this.SetSession("user", user)
		this.RespJson(enum.RespCode.OK, map[string]interface{}{"url": "/"})
	}
}
Exemplo n.º 6
0
// Valid cmdForm add
func (this *Cmd) ValidCmdAdd() *validation.Validation {
	valid := validation.Validation{}

	valid.Required(this.Shell, "ShellReq").Message("脚本不能为空")
	valid.MinSize(this.Shell, 1, "ShellMixSize").Message("脚本不能小于1个字符")
	valid.MaxSize(this.Shell, 225, "ShellMaxSize").Message("脚本不能大于225个字符")

	valid.Required(this.AgentId, "AgentIdReq").Message("主机不能为空")

	return &valid
}
Exemplo n.º 7
0
// Valid userForm add
func (a *Agent) ValidAgentAdd() *validation.Validation {
	valid := validation.Validation{}

	a.Host = strings.TrimSpace(a.Host)
	a.Port = strings.TrimSpace(a.Port)
	a.LoginName = strings.TrimSpace(a.LoginName)
	a.LoginPass = strings.TrimSpace(a.LoginPass)

	valid.Required(a.Host, "HostReq").Message("远程地址不能为空")
	valid.MinSize(a.Host, 10, "HostMixSize").Message("远程地址不能小于10个字符")
	valid.MaxSize(a.Host, 225, "HostMaxSize").Message("远程地址不能大于225个字符")

	valid.Required(a.Port, "PortReq").Message("端口不能为空")
	valid.MinSize(a.Port, 2, "PortMixSize").Message("端口不能小于2个字符")
	valid.MaxSize(a.Port, 6, "PortMaxSize").Message("端口不能大于6个字符")

	valid.Required(a.LoginName, "LoginNameReq").Message("登录用户名不能为空")
	valid.MinSize(a.LoginName, 1, "LoginNameMixSize").Message("登录用户名不能小于1个字符")
	valid.MaxSize(a.LoginName, 225, "LoginNameMaxSize").Message("登录用户名不能大于225个字符")

	valid.Required(a.LoginPass, "LoginPassReq").Message("登录密码不能为空")
	valid.MinSize(a.LoginPass, 1, "LoginPassMixSize").Message("登录密码不能小于1个字符")
	valid.MaxSize(a.LoginPass, 225, "LoginPassMaxSize").Message("登录密码不能大于225个字符")

	return &valid
}
Exemplo n.º 8
0
func (c *TUserController) ValidEmail() {
	email := c.GetString("email")
	valid := validation.Validation{}
	valid.Email(email, "Email")
	valid.MaxSize(email, 50, "Email")
	if valid.HasErrors() {
		c.Data["json"] = map[string]string{"error": "邮箱不正确"}
	} else {
		err := models.ValidEmail(email)
		if err == nil {
			c.Data["json"] = map[string]string{"error": "邮箱已经存在"}
		} else {
			c.Data["json"] = map[string]string{"message": "这个邮箱未被注册"}
		}
	}
	c.ServeJson()
}
Exemplo n.º 9
0
// We will check if the user data are valid
func (u User) Valid() bool {
	v := validation.Validation{}
	v.Required(u.Name, "name")
	v.MaxSize(u.Name, 20, "nameMax")
	v.Required(u.Pass, "pass")
	v.MaxSize(u.Pass, 30, "passMax")
	v.Email(u.Email, "email")

	if v.HasErrors() {
		for _, e := range v.Errors {
			log.Println("Check valid user data:", e)
		}
		return false
	}

	return true
}
Exemplo n.º 10
0
func (c *TUserController) ValidUserName() {
	username := c.GetString("username")
	valid := validation.Validation{}
	re, _ := regexp.Compile(`[\p{Han},\w]+`)
	valid.Match(username, re, "用户名")
	valid.MinSize(username, 4, "用户名")
	valid.MaxSize(username, 16, "用户名")
	if valid.HasErrors() {
		c.Data["json"] = map[string]string{"error": "用户名不正确"}
	} else {
		err := models.ValidUserName(username)
		if err == nil {
			c.Data["json"] = map[string]string{"error": "用户名已经存在"}
		} else {
			c.Data["json"] = map[string]string{"message": "你可以使用此用户名"}
		}
	}
	c.ServeJson()
}
Exemplo n.º 11
0
// @router /home [get]
func (this *HomeController) Get() {
	// this.Ctx.WriteString("HomeController")
	this.Abort("404")
	valid := validation.Validation{}
	u := user{20}

	valid.MaxSize(u.Age, 15, "ageMax")

	if valid.HasErrors() {
		for _, err := range valid.Errors {
			log.Println(err.Key, err.Message)
		}
	}

	if v := valid.Max(u.Age, 12, "age"); !v.Ok {
		log.Println(v.Error.Key, v.Error.Message)
	}

	this.TplNames = "addUser.html"
}
Exemplo n.º 12
0
func main() {
	u := User{"man", 14}
	valid := validation.Validation{}
	valid.Required(u.Name, "name")
	valid.MaxSize(u.Name, 15, "nameMax")
	//	valid.Range(u.Age, 0, 18, "age")
	//	if valid.HasErrors() {
	//		for _, err := range valid.Errors {
	//			log.Println(err.Key, err.Message)
	//		}
	//	}

	//	if v := valid.Max(u.Age, 140, "age"); !v.Ok {
	//		log.Println(v.Error.Key, v.Error.Message)
	//	}
	minAge := 18
	valid.Min(u.Age, minAge, "age").Message("少儿不宜!")
	// 错误信息格式化
	valid.Min(u.Age, minAge, "age").Message("%d不禁", minAge)
}
Exemplo n.º 13
0
/**
 * 注册逻辑处理
 * @param  {[type]} this *UserController) Join( [description]
 * @return {[type]}      [description]
 */
func (this *UserController) Join() {
	beego.AutoRender = false
	errmsg := make(map[string]string)
	requestEmail := this.GetString("email")
	requestNickname := this.GetString("nickname")
	requestPassword := this.GetString("password")

	jvalid := validation.Validation{}

	jvalid.Required(requestEmail, "email")
	jvalid.MaxSize(requestEmail, 40, "email")
	jvalid.Required(requestNickname, "nickname")
	jvalid.MaxSize(requestNickname, 20, "nickname")
	jvalid.Required(requestPassword, "password")

	fmt.Println(jvalid.HasErrors())

	if jvalid.HasErrors() {
		for _, err := range jvalid.Errors {
			//fmt.Println(err.Key, err.Message)
			this.showmsg("Error Message", "["+err.Key+"]"+err.Message)
		}
	}

	if len(errmsg) == 0 {
		var user models.User
		user.Email = requestEmail
		user.Nickname = requestNickname
		user.Password = models.Md5([]byte(requestPassword))
		user.Lastlogin = time.Now().Unix()
		if err := user.Insert(); err != nil {
			this.Ctx.WriteString(err.Error())
		}
	}
	this.SetSession("nickname", requestNickname)
	this.SetSession("email", requestEmail)
	this.SetSession("isadmin", 0)
	this.Redirect("/", 302)
}
Exemplo n.º 14
0
func (this *UserController) Register() {
	if this.Ctx.Input.Method() == "GET" {
		if can, err := beego.AppConfig.Bool("CanBeRegister"); err == nil && can {
			this.Data["CanBeRegister"] = true
		}
		this.TplNames = "console/register.html"
		return
	}

	req := this.ReqJson()
	email := req.Get("email").MustString()
	nickName := req.Get("nickName").MustString()
	password := req.Get("password").MustString()

	valid := validation.Validation{}
	valid.Email(email, "email")
	valid.MinSize(nickName, 6, "nickNameMin")
	valid.MaxSize(nickName, 12, "nickNameMax")
	valid.MinSize(password, 6, "passwordMin")
	valid.MaxSize(password, 12, "passwordMax")

	if valid.HasErrors() {
		this.CustomAbort(enum.RespCode.BadRequest.Code(), enum.RespCode.BadRequest.Str())
	}

	err := user.AddUser(email, nickName, password)
	if err == nil {
		this.RespJson(enum.RespCode.OK, nil)
	} else {
		if strings.Contains(err.Error(), "email") {
			this.RespJson(enum.RespCode.EmailExist, nil)
		} else if strings.Contains(err.Error(), "nick_name") {
			this.RespJson(enum.RespCode.NickNameExist, nil)
		} else {
			beego.Error(err)
		}
	}
}
Exemplo n.º 15
0
//添加用户
func (this *UserController) Add() {
	input := make(map[string]string)
	errmsg := make(map[string]string)
	if this.Ctx.Request.Method == "POST" {
		username := strings.TrimSpace(this.GetString("username"))
		password := strings.TrimSpace(this.GetString("password"))
		password2 := strings.TrimSpace(this.GetString("password2"))
		email := strings.TrimSpace(this.GetString("email"))
		active, _ := this.GetInt("active")

		input["username"] = username
		input["password"] = password
		input["password2"] = password2
		input["email"] = email

		valid := validation.Validation{}

		if v := valid.Required(username, "username"); !v.Ok {
			errmsg["username"] = "******"
		} else if v := valid.MaxSize(username, 15, "username"); !v.Ok {
			errmsg["username"] = "******"
		}

		if v := valid.Required(password, "password"); !v.Ok {
			errmsg["password"] = "******"
		}

		if v := valid.Required(password2, "password2"); !v.Ok {
			errmsg["password2"] = "请再次输入密码"
		} else if password != password2 {
			errmsg["password2"] = "两次输入的密码不一致"
		}

		if v := valid.Required(email, "email"); !v.Ok {
			errmsg["email"] = "请输入email地址"
		} else if v := valid.Email(email, "email"); !v.Ok {
			errmsg["email"] = "Email无效"
		}

		if active > 0 {
			active = 1
		} else {
			active = 0
		}

		if len(errmsg) == 0 {
			var user models.User
			user.Username = username
			user.Password = models.Md5([]byte(password))
			user.Email = email
			user.Active = int8(active)
			if err := user.Insert(); err != nil {
				this.showmsg(err.Error())
			}
			this.Redirect("/admin/user/list", 302)
		}

	}

	this.Data["input"] = input
	this.Data["errmsg"] = errmsg
	this.display()
}