func (this *LoginController) Post() { username := this.GetString("username") password := this.GetString("password") if username == "" || password == "" { this.Data["json"] = map[string]interface{}{"result": false, "msg": "invalid request", "refer": "/"} } user, err := FindUser(username) if err != nil { this.Data["json"] = map[string]interface{}{"result": false, "msg": "user does not exist", "refer": "/"} } else { passwd := com.Md5(password + user.Salt) // log.Println(password) // log.Println(passwd) if passwd == user.Password { this.SetSession("username", username) this.Data["json"] = map[string]interface{}{"result": true, "msg": "user[" + user.Username + "] login success ", "refer": "/admin"} } else { this.Data["json"] = map[string]interface{}{"result": false, "msg": "login failed ", "refer": "/"} } } this.ServeJson() }
func (this *SendEmailToGetBackPasswordController) Get() { username := this.GetString("username") if "" == username { this.Data["json"] = map[string]interface{}{"result": false, "msg": "username could not be empty", "refer": "/"} this.ServeJson() return } time := time.Now() code := com.Md5(com.RandString(20) + time.String()) err := AddVerify(username, code, time) if nil != err { this.Data["json"] = map[string]interface{}{"result": false, "msg": "create varify failed", "refer": "/"} this.ServeJson() } else { host := beego.AppConfig.String("host") subject := "blog system get your password back" body := `click the following link to get your password back <font color="red"><a href="` + host + `/password/reset/` + code + `">` + host + `/password/reset/` + code + `</a></font>` currentUser, _ := FindUser(username) email := currentUser.Email err := utils.SendMail(email, subject, body) if nil != err { this.Data["json"] = map[string]interface{}{"result": false, "msg": "send mail failed", "refer": "/"} this.ServeJson() } else { this.Data["json"] = map[string]interface{}{"result": true, "msg": "create varify success", "refer": "/"} this.ServeJson() } } }
// 添加用户 func AddUser(username string, password string) (int64, error) { o := orm.NewOrm() o.Using("default") user := new(Users) user.Username = username user.Salt = com.RandString(10) user.Password = com.Md5(password + user.Salt) return o.Insert(user) }
// get avatar func (this *User) GetAvatar(id int, username string, email string, nickname string) (string, error) { user, err := this.GetUser(id, username, email, nickname) if nil == err { return beego.AppConfig.String("avatar") + com.Md5(user.Email), err } else { log.Warnln("GetAvatar Failed.", err) return "", err } }
// 修改密码 func ChangePassword(username string, oldPassword string, newPassword string) error { o := orm.NewOrm() o.Using("default") salt := com.RandString(10) user := Users{Username: username} err := o.Read(&user, "username") if nil != err { return err } else { if user.Password == com.Md5(oldPassword+user.Salt) { _, err := o.QueryTable("users").Filter("username", username).Update(orm.Params{ "salt": salt, "password": com.Md5(newPassword + salt), }) return err } else { return errors.New("verification failed") } } }
// create session and add session into database func (this *SessionTab) CreateSession() (string, error) { sid := com.Md5(com.CreateGUID()) sid = com.SubString(sid, 0, 7) o := orm.NewOrm() var sess SessionTab sess.Session = sid sess.CreateTime = time.Now() _, err := o.Insert(&sess) return sid, err }
func login(username string, password string) bool { user, err := FindUser(username) if err != nil { return false } else { passwd := com.Md5(password + user.Salt) if passwd == user.Password { return true } else { return false } } }
// 设置密码 func SetPassword(username string, password string) error { o := orm.NewOrm() o.Using("default") salt := com.RandString(10) num, err := o.QueryTable("users").Filter("username", username).Update(orm.Params{ "salt": salt, "password": com.Md5(password + salt), }) if 0 == num { return errors.New("item not exist") } return err }
// run before get func (this *BaseController) Prepare() { // get user level var lev string stn := time.Now() st := stn.UnixNano() this.Data["start"] = st log.Blueln(this.Ctx.Request.UserAgent()) user := this.GetSession("username") if user == nil { lev = "guest" // guest, not login } else { level := this.GetSession("level") if level == nil { lev = "user" } else { if tmplev, ok := level.(string); !ok { lev = "user" } else { lev = tmplev } } username := user.(string) usr := models.User{} u, err := usr.GetUser(0, username, "", "") if err != nil { this.Data["nickname"] = "" this.Data["email_md5"] = "" } else { this.Data["username"] = username this.Data["nickname"] = u.Nickname this.Data["email_md5"] = com.Md5(u.Email) } } this.Data["userIs"] = lev // log.Pinkln(lev) }
// user registor // return: int64 id // error if failed func (this *User) Register(userName string, password string, email string, nickName string) (int64, error) { if len(userName) <= 0 || len(password) < 5 || len(email) <= 0 { return 0, errors.New("check you form, please.") } if len(nickName) <= 0 { nickName = userName + com.RandString(5) // gen the default nickname } o := orm.NewOrm() var user User user.Username = userName user.Salt = com.RandString(7) user.Password = com.Md5(password + user.Salt) user.Email = email user.Level = "user" user.Nickname = nickName return o.Insert(&user) }
// user login // return: bool if login // string user level if exist func (this *User) Login(userName string, password string) (bool, string) { if len(userName) <= 0 || len(password) <= 0 { return false, "" } o := orm.NewOrm() var user User user.Username = userName err := o.Read(&user, "Username") if err != nil { utils.Trace("查询不到") } else { pwd := com.Md5(password + user.Salt) utils.Trace("[pwd] %s\n[password] %s", pwd, user.Password) if user.Password == pwd { return true, user.Level } else { return false, "" } } return false, "" }
// 获取用户头像 func GetGravatar(email string) string { return "http://www.gravatar.com/avatar/" + com.Md5(strings.ToUpper(email)) }