func (self *HomeController) Get() { self.TplNames = "u/home.html" self.LayoutSections["Heads"] = "u/user-head.html" self.LayoutSections["Scripts"] = "" username := self.GetString(":name") u := new(models.User) u.Username = username // not exist the user if err := u.Read("Username"); err == orm.ErrNoRows { self.Redirect("/", 404) return } self.Data["u"] = *u self.Data["me"] = false // the login name is self if self.Username == username { self.Data["me"] = true } }
func (self *SigninController) Post() { self.setTemplate() flash := beego.NewFlash() email := self.GetString("email") password := self.GetString("password") remember := self.GetString("remember") u := new(models.User) u.Email = email u.Password = password if err := u.Read("Email"); err != nil { flash.Error("该账号不存在~") flash.Store(&self.Controller) return } if utils.HmacSha256(password, u.Salt) != u.Password { flash.Error("密码无法通过校验~") flash.Store(&self.Controller) return } u.LastLoginTime = time.Now() u.LoginCount = u.LoginCount + 1 u.LastLoginIp = self.GetClientIP() u.Update("LastLoginTime", "LoginCount") // 登录成功设置session self.SetSession("userid", u.Id) self.SetSession("username", u.Username) self.SetSession("userrole", u.Role) self.SetSession("useremail", u.Email) self.SetSession("usercontent", u.Content) self.SetSession("useravatar", u.Avatar) // 设置cookie // 设置提示栏cookie标记 self.Ctx.SetCookie("signbar", SignBarClose, 31536000, "/") if remember == RememberOn { self.Ctx.SetCookie("remember", RememberOn, 31536000, "/") } else { self.Ctx.SetCookie("remember", RememberOff, 31536000, "/") } self.Redirect("/", 302) }
func (self *NewAnswerController) Post() { qid, _ := self.GetInt64(":qid") suid, _ := self.GetSession("userid").(int64) rc := template.HTMLEscapeString(strings.TrimSpace(self.GetString("content"))) redirect := "/q/" + self.GetString(":qid") + "/" // 问题已不存在 if qid <= 0 { self.Redirect("/", 302) return } // 内容为空 if rc == "" { self.Redirect(redirect, 302) return } // 用户已不存在 usr := models.User{Id: suid} if err := usr.Read(); err != nil { self.Redirect("/", 302) return } // 不等于0,即是注册用户或管理层 此时把ctype设置为1 主要是为了区分游客 r := new(models.Reply) r.Pid = qid r.Uid = suid r.Ctype = 1 r.Content = rc r.Author = usr.Username r.Created = time.Now() r.Updated = r.Created r.ReplyTime = r.Created // 为安全,先行保存回应,顺手获得aid,在后面顺手再更新替换@通知的链接 if _, err := r.Add(); err != nil { fmt.Println("#", r.Id, ":", err) } else { //如果回应内容中有@通知 则处理以下事件 /* if users := helper.AtUsers(rc); len(users) > 0 { if tp, err := model.GetQuestion(qid); err == nil { todo := []string{} for _, v := range users { //判断被通知之用户名是否真实存在 if u, e := model.GetUserByUsername(v); e == nil && u != nil { //存在的则加入待操作列 todo = append(todo, v) //替换被通知用户的用户名带上用户主页链接 rc = strings.Replace(rc, "@"+v, "<a href='/user/"+u.Username+"/' title='"+u.Nickname+"' target='_blank'><span>@</span><span>"+u.Username+"</span></a>", -1) //发送通知内容到用户的 时间线 model.AddTimeline(usr.Username+"在「"+tp.Title+"」的回应里提到了你~", rc+"[<a href='/"+self.GetString(":qid")+"/#answer-"+strconv.Itoa(int(aid))+"'>"+tp.Title+"</a>]", tp.Cid, tp.Nid, u.Id, usr.Username, usr.Content) } } if len(todo) > 0 { model.SetReplyContentByRid(aid, rc) } } } */ self.Redirect(redirect+"#a-"+strconv.Itoa(int(r.Id)), 302) return } }
func (self *SignupController) Post() { self.setTemplate() flash := beego.NewFlash() email := strings.TrimSpace(strings.ToLower(self.GetString("email"))) username := strings.ToLower(self.GetString("username")) password := self.GetString("password") repassword := self.GetString("repassword") u := new(models.User) u.Email = email u.Username = username u.Password = password u.Valid = validation.Validation{} u.ValidateUserName().ValidateEmail().ValidatePassword() if u.Valid.HasErrors() { // validation does not pass var errMsg string = "" for _, err := range u.Valid.Errors { errMsg += err.Message } flash.Error(errMsg) flash.Store(&self.Controller) return } if password != repassword { flash.Error("两次密码不匹配~") flash.Store(&self.Controller) return } if !setting.Cpt.VerifyReq(self.Ctx.Request) { flash.Error("验证码不正确~") flash.Store(&self.Controller) return } salt := utils.GetSalt(8) pwd := utils.HmacSha256(password, salt) u.Salt = salt u.Password = pwd u.Role = 1 u.Updated = time.Now() u.LastLoginTime = u.Updated u.Avatar = setting.Default_Avatar if err := u.Insert(); err != nil { flash.Error("用户注册信息写入数据库时发生错误~") flash.Store(&self.Controller) return } // 注册账号成功,以下自动登录并设置session self.SetSession("userid", u.Id) self.SetSession("username", u.Username) self.SetSession("userrole", u.Role) self.SetSession("useremail", u.Email) self.SetSession("usercontent", u.Content) // 设置cookie // 设置提示栏cookie标记 self.Ctx.SetCookie("signbar", RememberOff, 31536000, "/") flash.Notice("账号登录成功~") flash.Store(&self.Controller) // session 写入后直接跳到首页 self.Redirect("/u/signin", 302) }