func CreateDb() bool { q, err := ConnDb() defer q.Close() if err != nil { fmt.Println(err) return false } else { mg, _ := SetMg() defer mg.Close() mg.CreateTableIfNotExists(new(User)) mg.CreateTableIfNotExists(new(Category)) mg.CreateTableIfNotExists(new(Node)) mg.CreateTableIfNotExists(new(Topic)) mg.CreateTableIfNotExists(new(Reply)) mg.CreateTableIfNotExists(new(Kvs)) mg.CreateTableIfNotExists(new(File)) //用户等级划分:正数是普通用户,负数是管理员各种等级划分,为0则尚未注册 if GetUserByRole(-1000).Role != -1000 { AddUser("root@localhost", "root", "系统默认管理员", helper.Encrypt_password("rootpass", nil), -1000) fmt.Println("Default User:root,Password:rootpass") if GetAllTopic(0, 0, "id") == nil { //分類默認數據 AddCategory("Category!", "This is Category!") AddNode("Node!", "This is Node!", 1, 1) SetTopic(0, 1, 1, 1, 0, "Topic Title", `<p>This is Topic!</p>`, "root", "") } } if GetKV("author") != "Insion" { SetKV("author", "Insion") SetKV("title", "Toropress") SetKV("title_en", "Toropress") SetKV("keywords", "Toropress,") SetKV("description", "Toropress,") SetKV("company", "Toropress") SetKV("copyright", "2013 Copyright Toropress .All Right Reserved") SetKV("site_email", "*****@*****.**") SetKV("tweibo", "http://t.qq.com/yours") SetKV("sweibo", "http://weibo.com/yours") } return true } return false }
func (self *RegHandler) Post() { self.TplNames = "reg.html" self.Ctx.Request.ParseForm() username := self.Ctx.Request.Form.Get("username") password := self.Ctx.Request.Form.Get("password") usererr := helper.CheckUsername(username) if usererr == false { self.Data["UsernameErr"] = "Username error, Please to again" return } passerr := helper.CheckPassword(password) if passerr == false { self.Data["PasswordErr"] = "Password error, Please to again" return } pwd := helper.Encrypt_password(password, nil) //now := torgo.Date(time.Now(), "Y-m-d H:i:s") userInfo := models.CheckUserByNickname(username) fmt.Println("=============================") fmt.Println(userInfo.Nickname) // 检查该用户是否已经被注册 if userInfo.Nickname == "" { //注册用户 regErr := models.AddUser(username+"@insion.co", username, "", pwd, 1) fmt.Println("reg:s") fmt.Println(regErr) fmt.Println("reg:e ") //注册成功设置session // self.SetSession("userid", userInfo.Id) // self.SetSession("username", userInfo.Nickname) // self.SetSession("userrole", userInfo.Role) // self.SetSession("useremail", userInfo.Email) self.Ctx.Redirect(302, "/login") } else { self.Data["UsernameErr"] = "User already exists" } self.Render() }
func CreateDb() { if err := Engine.Sync(new(User), new(Category), new(Node), new(Topic), new(Reply), new(Kvs), new(File)); err != nil { fmt.Println("Database struct sync failed") fmt.Println("Engine.Sync ERRORS:", err) } else { fmt.Println("Database struct sync successfully") } //用户等级划分:正数是普通用户,负数是管理员各种等级划分,为0则尚未注册 if GetUserByRole(-1000).Role != -1000 { AddUser("root@localhost", "root", "系统默认管理员", helper.Encrypt_password("rootpass", nil), -1000) fmt.Println("Default User:root,Password:rootpass") if GetAllTopic(0, 0, "id") == nil { //分類默認數據 AddCategory("Category!", "This is Category!") AddNode("Node!", "This is Node!", 1, 1) SetTopic(0, 1, 1, 1, 0, "Topic Title", `<p>This is Topic!</p>`, "root", "") } } if GetKV("author") != "Insion" { SetKV("author", "Insion") SetKV("title", "Toropress") SetKV("title_en", "Toropress") SetKV("keywords", "Toropress,") SetKV("description", "Toropress,") SetKV("company", "Toropress") SetKV("copyright", "2013 Copyright Toropress .All Right Reserved") SetKV("site_email", "*****@*****.**") SetKV("tweibo", "http://t.qq.com/yours") SetKV("sweibo", "http://weibo.com/yours") } }
func (self *RSettingHandler) Post() { switch { case self.Ctx.Request.RequestURI == "/root-setting-setroot": //设置管理员 newroot := self.GetString("newroot") realname := self.GetString("realname") curpassword := self.GetString("curpassword") newpassword := self.GetString("newpassword") repassword := self.GetString("repassword") if newroot != "" && realname != "" && curpassword != "" && repassword != "" && newpassword == repassword { sess_username, _ := self.GetSession("username").(string) usr := models.GetUserByNickname(sess_username) if helper.Validate_password(usr.Password, curpassword) { if e := models.AddUser("", newroot, realname, helper.Encrypt_password(newpassword, nil), -1000); e != nil { self.Data["MsgErr"] = "添加新管理员“" + newroot + "”失败!" } else { self.Data["MsgErr"] = "添加新管理员“" + newroot + "”成功!" } } else { self.Data["MsgErr"] = "当前密码不正确!" } } else { msg := "" if curpassword == "" { msg = msg + "当前管理员密码不能为空!" } if newpassword == "" { msg = msg + "新增管理员密码不能为空!" } if repassword == "" { msg = msg + "新增管理员确认密码不能为空!" } if newpassword != repassword { msg = msg + "两次输入的新增管理员密码不一致!" } self.Data["MsgErr"] = msg } case self.Ctx.Request.RequestURI == "/root-setting-password": //密码修改 oldpassword := self.GetString("oldpassword") newpassword := self.GetString("newpassword") repassword := self.GetString("repassword") if oldpassword != "" && repassword != "" && newpassword == repassword { sess_username, _ := self.GetSession("username").(string) usr := models.GetUserByNickname(sess_username) if helper.Validate_password(usr.Password, oldpassword) { usr.Password = helper.Encrypt_password(newpassword, nil) if e := models.SaveUser(usr); e != nil { self.Data["MsgErr"] = "更新密码失败!" } else { self.Data["MsgErr"] = "更新密码成功!" } } } else { msg := "" if oldpassword == "" { msg = msg + "原密码不能为空!" } if newpassword == "" { msg = msg + "新密码不能为空!" } if repassword == "" { msg = msg + "请输入确认密码!" } if newpassword != repassword { msg = msg + "两次输入的新密码不一致!" } self.Data["MsgErr"] = msg } case self.Ctx.Request.RequestURI == "/root-setting": //常规设置 POST title := self.GetString("title") title_en := self.GetString("title_en") keywords := self.GetString("keywords") description := self.GetString("description") company := self.GetString("company") copyright := self.GetString("copyright") site_email := self.GetString("site_email") tweibo := self.GetString("tweibo") sweibo := self.GetString("sweibo") statistics := self.GetString("statistics") models.SetKV("title", title) models.SetKV("title_en", title_en) models.SetKV("keywords", keywords) models.SetKV("description", description) models.SetKV("company", company) models.SetKV("copyright", copyright) models.SetKV("site_email", site_email) models.SetKV("tweibo", tweibo) models.SetKV("sweibo", sweibo) models.SetKV("statistics", statistics) } self.SetSession("MsgErr", self.Data["MsgErr"]) self.Redirect(self.Ctx.Request.RequestURI, 302) }