// 登录 func (this *Login) Login() { //AJAX 请求执行登录 if this.IsAjax() { username := this.GetString("username") password := this.GetString("password") if username == "" || password == "" { this.JsonOut(nil, "1", "用户名或密码不能为空!") } else { m := models.NewAdmin() admin, err := m.FindOneBy(bson.M{"username": username}) if err == nil { // 判断密码 if admin.PassWord == utils.Encrypt(password) { if admin.Status { data := make(map[string]interface{}) data["id"] = admin.Id.Hex() data["name"] = admin.UserName data["goto"] = "/admin/dashboard" // 保存SESSION this.SetSession("ADMIN_ID", admin.Id.Hex()) this.JsonOut(data, "0") } else { this.JsonOut(nil, "1", "用户"+username+"已被注销,无法登录!") } } else { this.JsonOut(nil, "1", "用户"+username+"密码错误!") } } else if beego.AppConfig.String("RunMode") == "dev" && beego.AppConfig.String("Developer") == username { // 新增超级管理员 m2 := models.NewAdmin() m2.UserName = username m2.PassWord = utils.Encrypt(password) m2.Status = true m2.Persist() this.JsonOut(nil, "1", "新增用户"+username+"成功!") } else { this.JsonOut(nil, "", "服务器繁忙,请稍后再访问", "503") this.JsonOut(nil, "1", "用户"+username+"不存在!") } } } else { this.display("", "login.tpl") } }
// 检测用户是否登录 func (this *base) CheckIsLogin() { if nil == this.CurrentAdmin { aid := this.GetSession("ADMIN_ID") if id, ok := aid.(string); ok && id != "" { m := models.NewAdmin() admin, err := m.Find(id) // 用户组权限 m2 := models.NewAdminRole() adminRole, err2 := m2.Find(admin.RoleId) if nil == err2 && nil != adminRole { admin.Privileges = adminRole.Privileges } if err == nil { this.CurrentAdmin = admin } else { this.Redirect("/admin/login", 302) } } else { this.Redirect("/admin/login", 302) } } }
// 编辑管理员信息(新增或修改) func (this *Admins) Edit() { id := this.GetString(":id") o := models.NewAdmin() if id == this.CurrentAdmin.Id.Hex() { o = this.CurrentAdmin } else { if id != "" { if o1, err := o.Find(id); nil == err && nil != o1 { o = o1 } } } fm := models.AdminForm{} if err := this.ParseForm(&fm); err != nil { this.JsonOut(nil, "1", err.Error()) } valid := validation.Validation{} valid.Required(fm.UserName, "userName").Message("账号不能为空") valid.Required(fm.Email, "email").Message("邮箱不能为空") valid.Required(fm.RoleId, "roleId").Message("角色不能为空") valid.Required(fm.Status, "status").Message("状态不能为空") // 非新增 if o.Id.Valid() { if fm.PassWord != "" { valid.MinSize(fm.PassWord, 6, "passWord").Message("密码至少需要6位") } } else { valid.Required(fm.PassWord, "passWord").Message("密码不能为空") valid.MinSize(fm.PassWord, 6, "passWord").Message("密码至少需要6位") } if errmsg, ok := this.HasErrMsgs(valid); ok { this.JsonOut(nil, "1", errmsg) } o.UserName = fm.UserName o.Email = fm.Email o.RoleId = fm.RoleId o.Status = fm.Status if fm.PassWord != "" { o.PassWord = utils.Encrypt(fm.PassWord) } err := o.Persist() if nil == err { this.JsonOut(nil, "0") } else if o.Id.Valid() { this.JsonOut(nil, "1", "修改管理员失败") } else { this.JsonOut(nil, "1", "添加管理员失败") } }
// 删除管理员信息 func (this *Admins) Delete() { id := this.GetString(":id") var o *models.Admin if id == this.CurrentAdmin.Id.Hex() { o = this.CurrentAdmin } else { o, _ = models.NewAdmin().Find(id) } o.Remove() this.JsonOut(nil, "0") }
func (this *Sites) Delete() { id := this.GetString(":id") if "" != id { o, err := models.NewAdmin().Find(id) if nil == err && nil != o { o.Remove() this.JsonOut(nil, "0") } } this.JsonOut(nil, "1", "删除网址失败") }
// 单个管理员 func (this *Admins) Info() { id := this.GetString(":id") var o *models.Admin if id == this.CurrentAdmin.Id.Hex() { o = this.CurrentAdmin } else { o, _ = models.NewAdmin().Find(id) } data := make(map[string]interface{}) data["admin"] = o this.JsonOut(data, "0") }
// 列出所有管理员 func (this *Admins) List() { var condition bson.M roleId := this.GetString("roleId") if "" != roleId { condition = bson.M{"role_id": roleId} } page, rows := this.GetPageSize() m := models.NewAdmin() items := m.FindBy(condition, page, rows) records := m.Count(condition) data := make(map[string]interface{}) data["admins"] = items data["pagination"] = models.Pageinate{ Records: records, Page: page, Rows: rows, } this.JsonOut(data, "0") }