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": "/"}) } }
//create an user, with dup_key error for email or nickname. func AddUser(email, nickname, password string) error { salt := util.RandString(8) password = util.Md5(salt + password) u := &User{Email: email, NickName: nickname, Salt: salt, Password: password} o := orm.NewOrm() _, err := o.Insert(u) return err }
func ModifyTopic(id interface{}, u *user.User, title, tags, abstract, markdown, htmlContent, hash string) error { //1、判断是否真的为该用户 2、hash值是否匹配 o := orm.NewOrm() t := &Topic{} o.QueryTable("Topic").Filter("Id", id).RelatedSel().One(t) if t.User.Id != u.Id { return errors.New(enum.RespCode.UnAuthorized.Str()) } if t.Hash != hash { return errors.New(enum.RespCode.Conflict.Str()) } t.Title = title t.Tags = tags t.Abstract = abstract t.Markdown = markdown t.HtmlContent = htmlContent t.Hash = util.Md5(fmt.Sprint(t)) t.UpdateTime = time.Now() _, err := o.Update(t) return err }
func AddTopic(user *user.User, title, tags, abstract, markdown, htmlContent string) error { topic := &Topic{User: user, Title: title, Tags: tags, Abstract: abstract, Markdown: markdown, HtmlContent: htmlContent, PV: 1} topic.Hash = util.Md5(fmt.Sprint(topic)) _, err := orm.NewOrm().Insert(topic) return err }