func (this *AuthApiController) UpdateUser() { baseResp := this.BasicRespGen() _, err := this.SessionCheck() if err != nil { this.ResposeError(baseResp, err.Error()) return } else { username := this.GetString("cName", this.Ctx.GetCookie("name")) user := ReadUserByName(username) user.Email = strings.TrimSpace(this.GetString("email", user.Email)) user.Cnname = strings.TrimSpace(this.GetString("cnname", user.Cnname)) user.IM = strings.TrimSpace(this.GetString("im", user.IM)) user.QQ = strings.TrimSpace(this.GetString("qq", user.QQ)) user.Phone = strings.TrimSpace(this.GetString("phone", user.Phone)) passwdtmp := strings.TrimSpace(this.GetString("password", "")) oldpasswdtmp := strings.TrimSpace(this.GetString("oldpassword", "")) if passwdtmp != "" { if user.Passwd != str.Md5Encode(g.Config().Salt+oldpasswdtmp) { this.ResposeError(baseResp, "original password is empty or the password you inputed is not matched the original one.") return } else { user.Passwd = str.Md5Encode(g.Config().Salt + passwdtmp) } } _, err := user.Update() if err != nil { this.ResposeError(baseResp, err.Error()) return } } this.ServeApiJson(baseResp) return }
func (this *UserController) ChangePassword() { oldPassword := strings.TrimSpace(this.GetString("old_password", "")) newPassword := strings.TrimSpace(this.GetString("new_password", "")) repeatPassword := strings.TrimSpace(this.GetString("repeat_password", "")) if newPassword != repeatPassword { this.ServeErrJson("password not equal the repeart one") return } me := this.Ctx.Input.GetData("CurrentUser").(*User) if me.Passwd != str.Md5Encode(g.Config().Salt+oldPassword) { this.ServeErrJson("old password error") return } newPass := str.Md5Encode(g.Config().Salt + newPassword) if me.Passwd == newPass { this.ServeOKJson() return } me.Passwd = newPass _, err := me.Update() if err != nil { this.ServeErrJson("occur error " + err.Error()) return } RemoveSessionByUid(me.Id) this.ServeOKJson() }
func (this *AuthController) RegisterPost() { if !g.Config().CanRegister { this.ServeErrJson("registration system is not open") return } name := strings.TrimSpace(this.GetString("name", "")) password := strings.TrimSpace(this.GetString("password", "")) repeatPassword := strings.TrimSpace(this.GetString("repeat_password", "")) if password != repeatPassword { this.ServeErrJson("password not equal the repeart one") return } if !utils.IsUsernameValid(name) { this.ServeErrJson("name pattern is invalid") return } if ReadUserIdByName(name) > 0 { this.ServeErrJson("name is already existent") return } lastId, err := InsertRegisterUser(name, str.Md5Encode(g.Config().Salt+password)) if err != nil { this.ServeErrJson("insert user fail " + err.Error()) return } this.CreateSession(lastId, 3600*24*30) this.ServeOKJson() }
func (this *UserController) CreateUserPost() { me := this.Ctx.Input.GetData("CurrentUser").(*User) if me.Role <= 0 { this.ServeErrJson("no privilege") return } name := strings.TrimSpace(this.GetString("name", "")) password := strings.TrimSpace(this.GetString("password", "")) role, _ := this.GetInt("role", -1) if !utils.IsUsernameValid(name) { this.ServeErrJson("name pattern is invalid") return } if ReadUserIdByName(name) > 0 { this.ServeErrJson("name is already existent") return } _, err := InsertUser(name, str.Md5Encode(g.Config().Salt+password), role) if err != nil { this.ServeErrJson("insert user fail " + err.Error()) } else { this.ServeOKJson() } }
func (this *AuthApiController) Login() { baseResp := this.BasicRespGen() name := this.GetString("name", "") password := this.GetString("password", "") if name == "" || password == "" { this.ResposeError(baseResp, "name or password is blank") return } user := ReadUserByName(name) switch { case user == nil: this.ResposeError(baseResp, "no such user") return case user.Passwd != str.Md5Encode(g.Config().Salt+password): this.ResposeError(baseResp, "password error") return } appSig := this.GetString("sig", "") callback := this.GetString("callback", "") sig, expired := ReadSessionByName(name) switch { case sig != "": baseResp.Data["name"] = name baseResp.Data["sig"] = sig baseResp.Data["expired"] = expired case appSig != "" && callback != "": SaveSessionAttrs(user.Id, appSig, int(time.Now().Unix())+3600*24*30) baseResp.Data["name"] = name baseResp.Data["sig"] = appSig baseResp.Data["expired"] = int(time.Now().Unix()) + 3600*24*30 default: sig, expired := this.CreateSession(user.Id, 3600*24*30) baseResp.Data["name"] = name baseResp.Data["sig"] = sig baseResp.Data["expired"] = expired } this.ServeApiJson(baseResp) return }
func (this *UserController) CreateRoot() { password := strings.TrimSpace(this.GetString("password", "")) if password == "" { this.Ctx.Output.Body([]byte("password is blank")) return } userPtr := &User{ Name: "root", Passwd: str.Md5Encode(g.Config().Salt + password), Role: 2, } _, err := userPtr.Save() if err != nil { this.Ctx.Output.Body([]byte(err.Error())) } else { this.Ctx.Output.Body([]byte("success")) } }
func (this *UserController) ResetPassword() { password := this.GetString("password", "") if password == "" { this.ServeErrJson("password is blank") return } targetUser := this.Ctx.Input.GetData("TargetUser").(*User) if targetUser.Name == "root" { this.ServeErrJson("no privilege") return } targetUser.Passwd = str.Md5Encode(g.Config().Salt + password) _, err := targetUser.Update() if err != nil { this.ServeErrJson("occur error " + err.Error()) return } this.ServeOKJson() }
func (this *AuthApiController) Register() { baseResp := this.BasicRespGen() if !g.Config().CanRegister { this.ResposeError(baseResp, "registration system is not open") return } name := strings.TrimSpace(this.GetString("name", "")) email := strings.TrimSpace(this.GetString("email", "")) password := strings.TrimSpace(this.GetString("password", "")) repeatPassword := strings.TrimSpace(this.GetString("repeatPassword", "")) var lastID int64 var err error switch { case password != repeatPassword: this.ResposeError(baseResp, "password not equal the repeart one") return case !utils.IsUsernameValid(name): this.ResposeError(baseResp, "name pattern is invalid") return case ReadUserIdByName(name) > 0: this.ResposeError(baseResp, "name is already existent") return default: lastID, err = InsertRegisterUser(name, str.Md5Encode(g.Config().Salt+password), email) if err != nil { this.ResposeError(baseResp, "insert user fail "+err.Error()) return } } sig, expired := this.CreateSession(lastID, 3600*24*30) baseResp.Data["name"] = name baseResp.Data["sig"] = sig baseResp.Data["expired"] = expired this.ServeApiJson(baseResp) return }
func (this *AuthController) LoginPost() { name := this.GetString("name", "") password := this.GetString("password", "") if name == "" || password == "" { this.ServeErrJson("name or password is blank") return } var u *User ldapEnabled := this.MustGetBool("ldap", false) if ldapEnabled { sucess, err := utils.LdapBind(g.Config().Ldap.Addr, name, password) if err != nil { this.ServeErrJson(err.Error()) return } if !sucess { this.ServeErrJson("name or password error") return } arr := strings.Split(name, "@") var userName, userEmail string if len(arr) == 2 { userName = arr[0] userEmail = name } else { userName = name userEmail = "" } u = ReadUserByName(userName) if u == nil { // 说明用户不存在 u = &User{ Name: userName, Passwd: "", Email: userEmail, } _, err = u.Save() if err != nil { this.ServeErrJson("insert user fail " + err.Error()) return } } } else { u = ReadUserByName(name) if u == nil { this.ServeErrJson("no such user") return } if u.Passwd != str.Md5Encode(g.Config().Salt+password) { this.ServeErrJson("password error") return } } expired := this.CreateSession(u.Id, 3600*24*30) appSig := this.GetString("sig", "") callback := this.GetString("callback", "") if appSig != "" && callback != "" { SaveSessionAttrs(u.Id, appSig, expired) } this.ServeDataJson(callback) }
func (this *UserController) CreateUserPost() { name := strings.TrimSpace(this.GetString("name", "")) password := strings.TrimSpace(this.GetString("password", "")) cnname := strings.TrimSpace(this.GetString("cnname", "")) email := strings.TrimSpace(this.GetString("email", "")) phone := strings.TrimSpace(this.GetString("phone", "")) im := strings.TrimSpace(this.GetString("im", "")) qq := strings.TrimSpace(this.GetString("qq", "")) if !utils.IsUsernameValid(name) { this.ServeErrJson("name pattern is invalid") return } if ReadUserIdByName(name) > 0 { this.ServeErrJson("name is already existent") return } if password == "" { this.ServeErrJson("password is blank") return } if utils.HasDangerousCharacters(cnname) { this.ServeErrJson("cnname is invalid") return } if utils.HasDangerousCharacters(email) { this.ServeErrJson("email is invalid") return } if utils.HasDangerousCharacters(phone) { this.ServeErrJson("phone is invalid") return } if utils.HasDangerousCharacters(im) { this.ServeErrJson("im is invalid") return } if utils.HasDangerousCharacters(qq) { this.ServeErrJson("qq is invalid") return } lastId, err := InsertRegisterUser(name, str.Md5Encode(g.Config().Salt+password)) if err != nil { this.ServeErrJson("insert user fail " + err.Error()) return } targetUser := ReadUserById(lastId) targetUser.Cnname = cnname targetUser.Email = email targetUser.Phone = phone targetUser.IM = im targetUser.QQ = qq if _, err := targetUser.Update(); err != nil { this.ServeErrJson("occur error " + err.Error()) return } this.ServeOKJson() }
func (this *AuthController) LoginPost() { name := this.GetString("name", "") password := this.GetString("password", "") //qtyep := this.GetString("type","page") if name == "" || password == "" { this.ServeErrJson("name or password is blank") return } var u *User ldapEnabled := this.MustGetBool("ldap", false) if ldapEnabled && name != "root" { sucess, err := utils.LdapBind(g.Config().Ldap.Addr, g.Config().Ldap.BaseDN, g.Config().Ldap.BindDN, g.Config().Ldap.BindPasswd, g.Config().Ldap.UserField, name, password) if err != nil { this.ServeErrJson(err.Error()) return } if !sucess { this.ServeErrJson("name or password error") return } user_attributes, err := utils.Ldapsearch(g.Config().Ldap.Addr, g.Config().Ldap.BaseDN, g.Config().Ldap.BindDN, g.Config().Ldap.BindPasswd, g.Config().Ldap.UserField, name, g.Config().Ldap.Attributes) userSn := "" userMail := "" userTel := "" if err == nil { userSn = user_attributes["sn"] userMail = user_attributes["mail"] userTel = user_attributes["telephoneNumber"] } arr := strings.Split(name, "@") var userName, userEmail string if len(arr) == 2 { userName = arr[0] userEmail = name } else { userName = name userEmail = userMail } u = ReadUserByName(userName) if u == nil { // 说明用户不存在 u = &User{ Name: userName, Passwd: "", Cnname: userSn, Phone: userTel, Email: userEmail, Created: time.Now(), } _, err = u.Save() if err != nil { this.ServeErrJson("insert user fail " + err.Error()) return } } } else { u = ReadUserByName(name) if u == nil { this.ServeErrJson("no such user") return } if u.Passwd != str.Md5Encode(g.Config().Salt+password) { this.ServeErrJson("password error") return } } expired := this.CreateSession(u.Id, 3600*24*30) appSig := this.GetString("sig", "") callback := this.GetString("callback", "") if appSig != "" && callback != "" { SaveSessionAttrs(u.Id, appSig, expired) } this.ServeDataJson(callback) }
func main() { fmt.Printf(stringutil.Reverse("Hello World")) fmt.Printf(str.Md5Encode("This is us")) fmt.Printf(cron.SyncCron()) }