func (p *PageSetting) InfoUpdate() { username := p.SESSION[p.M["SESSION_UNAME"].(string)] if p.R.Method == "POST" { if _, ok := p.POST["ajax"]; ok { p.Hide = true mgoServer := Middleware.Get("db").(*helper.Mongo) m := utils.M{ "status": "1", "message": "", } if !ok { m["status"] = "0" m["message"] = "修改用户时必须指定用户名!" } colQuerier := utils.M{"name": username, "delete": 0} cnt, err := mgoServer.C(ColUser).Find(colQuerier).Count() if err != nil || cnt == 0 { m["status"] = "0" m["message"] = "用户不存在!" } else { email, emailOk := p.POST["email"] password, passwordOk := p.POST["password"] updateField, change := utils.M{}, utils.M{} if emailOk { updateField["email"] = email } if passwordOk { password = utils.Strings(password).Md5() updateField["password"] = password } if emailOk || passwordOk { updateField["update_time"] = time.Now().Unix() change["$set"] = updateField err := mgoServer.C(ColUser).Update(colQuerier, change) if err != nil { m["status"] = "0" m["message"] = "用户资料更新失败!" } else { m["message"] = "用户资料更新成功!" } } else { m["status"] = "0" m["message"] = "请输入需要更新的资料!" } } ret, _ := json.Marshal(m) p.RW.Write(ret) return } } }
func (p *PageIndex) Login() { if p.R.Method == "POST" { if _, ok := p.POST["ajax"]; ok { p.Hide = true mgoServer := Middleware.Get("db").(*helper.Mongo) m := utils.M{ "status": 1, "message": "", } p.RW.Header().Set("Content-Type", "application/json") p.RW.Header().Set("Cache-Control", "no-store") username := p.POST["username"] password := p.POST["password"] passwordMd5 := utils.Strings(password).Md5() colQuerier := utils.M{"name": username, "status": 1, "delete": 0} colSelecter := utils.M{"password": 1} col := ModelUser{} var jres []byte err := mgoServer.C(ColUser).Find(colQuerier).Select(colSelecter).One(&col) if err != nil || col.Password == "" { m["status"] = -1 m["message"] = "无此用户" } else { if passwordMd5 != col.Password { m["status"] = 0 m["message"] = "密码错误" } else { m["back_url"] = "" if _, ok := p.GET["back_url"]; ok { m["back_url"] = p.GET["back_url"] } p.SESSION[p.M["SESSION_UNAME"].(string)] = username p.SESSION[p.M["SESSION_UKEY"].(string)] = passwordMd5 } } jres, _ = json.Marshal(m) p.RW.Write(jres) return } } }
// Create User func (p *PageUser) CreateUser() { if p.R.Method == "POST" { if _, ok := p.POST["ajax"]; ok { mgoServer := Middleware.Get("db").(*helper.Mongo) m := utils.M{ "status": 1, "message": "", } p.Hide = true username := p.POST["username"] email := p.POST["email"] colQuerier := utils.M{"name": username, "delete": 0} var col ModelUser err := mgoServer.C(ColUser).Find(colQuerier).One(&col) if col.Name != "" || err == nil { m["status"] = 0 m["message"] = "该用户名已存在" } else { password := utils.Strings(p.POST["password"]).Md5() tnow := time.Now() mgoServer.C(ColUser).Insert(&ModelUser{ Email: email, Name: username, Password: password, Status: 1, Create_time: tnow.Unix(), Update_time: tnow.Unix(), }) } ret, _ := json.Marshal(m) p.RW.Write(ret) return } } }
func (p *PageInstall) Index() { fileInstallLock := "./data/install.lock" if _, err := os.Stat(fileInstallLock); err == nil { p.RW.Write([]byte("程序已经安装过,如需要重新安装,请删除data目录下的install.lock文件后重试")) } else { mgoServer := Middleware.Get("db").(*helper.Mongo) email := "*****@*****.**" username := "******" password := "******" passwordMd5 := utils.Strings(password).Md5() tnow := time.Now() mgoServer.C(ColUser).Insert(&ModelUser{ Email: email, Name: username, Password: passwordMd5, Status: 1, Create_time: tnow.Unix(), Update_time: tnow.Unix(), }) mgoServer.C(ColModule).Insert(&ModelModule{ Name: "模块管理", Path: "module/", Order: 0, Status: 1, Create_time: tnow.Unix(), Update_time: tnow.Unix(), }) mgoServer.C(ColModule).Insert(&ModelModule{ Name: "用户管理", Path: "user/", Order: 0, Status: 1, Create_time: tnow.Unix(), Update_time: tnow.Unix(), }) mgoServer.C(ColModule).Insert(&ModelModule{ Name: "角色管理", Path: "role/", Order: 0, Status: 1, Create_time: tnow.Unix(), Update_time: tnow.Unix(), }) mgoServer.C(ColRole).Insert(&ModelRole{ Name: "超级管理员", Users: []string{username}, Status: 1, Right: utils.M{ "scope": "3", "modules": []utils.M{}, }, Create_time: tnow.Unix(), Update_time: tnow.Unix(), }) ioutil.WriteFile(fileInstallLock, []byte("installed"), 0777) sessionSign := p.COOKIE[p.Session.CookieName] if sessionSign != "" { p.Session.Clear(sessionSign) } p.RW.Write([]byte("安装成功...<br/>用户名:" + username + ",密码:" + password + "<br/>")) } http.Redirect(p.RW, p.R, "/login.html", http.StatusFound) p.Close = true }
func (ar *ActiveRecord) getTableName(rowsSlicePtr interface{}) string { return utils.Strings(utils.Struct{rowsSlicePtr}.GetTypeName()).SnakeCasedName() }