// auth func (this *BaseController) GetAuthUser() *models.User { user := &models.User{} services.GetInstance().AuthService.WithContext(this.Ctx, func() { user = services.GetInstance().AuthService.GetAuthUser() }) return user }
func (this *ServerController) Operate() { if this.Ctx.Input.IsGet() { serverId := this.Ctx.Input.Param(":serverId") beego.Debug("serverId=>", serverId) server := &models.Server{} if serverId != "" { server = server.SelectOneById(serverId) } this.Data["ServerForm"] = server this.Layout = "layout/main.html" this.TplNames = "admin/server/operate.html" } else { server := models.Server{} if err := this.ParseForm(&server); err != nil { this.ErrInputData("数据错误") } user := this.GetAuthUser() server.UserId = user.Id // beego.Debug("server=>", // fmt.Sprintf("%+v", server), // ) var valid *validation.Validation if server.Id == "" { valid = server.ValidServerAdd() } else { valid = server.ValidServerEdit() } b, _ := valid.Valid(&server) if !b { this.Data["ErrorMap"] = valid.ErrorMap() this.Data["ErrorForm"] = server this.Layout = "layout/main.html" this.TplNames = "admin/server/operate.html" return } var message string if server.Id == "" { message = "新增API配置主题成功" if err := server.InsertOne(); err != nil { this.ErrDataBase("新增API配置主题失败") return } } else { message = "更新API配置主题成功" if err := server.UpdateOne(); err != nil { this.ErrDataBase("更新API配置主题失败") return } } services.GetInstance().CacheService.PutServerContent(server.Id, server.Content) this.DoSuccess(message, "/admin/server/index") } }
func (this *AuthController) Login() { if this.Ctx.Input.IsGet() { this.TplNames = "auth/login.html" } else { user := &models.User{} if err := this.ParseForm(user); err != nil { this.ErrInputData("用户信息错误") return } // toolbox.Display("登录UserForm", user) valid := user.ValidUserLogin() b, _ := valid.Valid(user) if !b { beego.Info("Login UserForm error") this.Data["ErrorMap"] = valid.ErrorMap() this.Data["ErrorForm"] = user this.TplNames = "auth/login.html" return } var data map[string]string services.GetInstance().AuthService.WithContext(this.Ctx, func() { data = services.GetInstance().AuthService.Login(user) }) for k, v := range data { if strings.Contains(k, "Error") { beego.Debug("登录失败", v) this.Data["UserForm"] = user this.Data["LoginMsg"] = "登录失败," + v this.TplNames = "auth/login.html" return } } // var key string = "c" // services.GetInstance().CacheService.Put(key, "value", 10) // var v = services.GetInstance().CacheService.Get(key) // beego.Debug("v", v) beego.Debug("登录成功") this.DoSuccess("登录成功", "/admin/index") } }
func (this *AdminController) SshJoin() { var agentId = this.Ctx.Input.Param(":agentId") var sshConfig = services.NewSSHConfig(agentId) if sshConfig.Agent.Id == "" { this.RFailed() } // Join . services.GetInstance().SSHService.Shell(this.Ctx.ResponseWriter, this.Ctx.Request, sshConfig) this.RSuccess() }
func router() { authR := beego.NewNamespace("/auth", beego.NSCond(func(ctx *context.Context) bool { if ctx.Input.Domain() == "api.beego.me" { return true } commonFilter(ctx) return true }), // beego.NSBefore(func(ctx *context.Context) { // beego.Debug("path=>", ctx.Request.URL.Path) // services.etInstance().AuthService.WithContext(ctx, func() { // if services.GetInstance().AuthService.HasLogin() { // ctx.Redirect(200, "/admin/index") // } // }) // }), beego.NSRouter("/login", &controllers.AuthController{}, "*:Login"), beego.NSRouter("/logout", &controllers.AuthController{}, "*:Logout"), ) adminR := beego.NewNamespace("/admin", beego.NSBefore(func(ctx *context.Context) { commonFilter(ctx) services.GetInstance().AuthService.WithContext(ctx, func() { if !services.GetInstance().AuthService.HasLogin() { ctx.Redirect(302, "/auth/login") } }) }), beego.NSRouter("/index", &controllers.AdminController{}, "*:Index"), beego.NSNamespace("/user", beego.NSGet("/info/:id", func(ctx *context.Context) { ctx.Output.Body([]byte("shopinfo")) }), beego.NSRouter("/list", &controllers.AdminController{}, "Get:UserList"), beego.NSRouter("/operate/?:userId", &controllers.AdminController{}, "*:UserOperate"), beego.NSRouter("/delete/:userId", &controllers.AdminController{}, "Get:UserDelete"), ), beego.NSNamespace("/agent", beego.NSRouter("/list", &controllers.AdminController{}, "Get:AgentList"), beego.NSRouter("/operate/?:agentId", &controllers.AdminController{}, "*:AgentOperate"), beego.NSRouter("/delete/:agentId", &controllers.AdminController{}, "Get:AgentDelete"), beego.NSRouter("/:agentId/cmd/list", &controllers.AdminController{}, "Get:CmdList"), beego.NSRouter("/:agentId/cmd/operate", &controllers.AdminController{}, "*:CmdOperate"), beego.NSRouter("/:agentId/cmd/operate/?:cmdId", &controllers.AdminController{}, "*:CmdOperate"), beego.NSRouter("/cmd/delete/:cmdId", &controllers.AdminController{}, "Get:CmdDelete"), ), beego.NSNamespace("/ssh", beego.NSRouter("/:agentId", &controllers.AdminController{}, "Get:SshIndex"), beego.NSRouter("/join/:agentId", &controllers.AdminController{}, "*:SshJoin"), beego.NSRouter("/list", &controllers.AdminController{}, "*:SshList"), ), beego.NSNamespace("/server", beego.NSRouter("/index/?:serverId", &controllers.ServerController{}, "Get:Index"), beego.NSRouter("/operate/?:serverId", &controllers.ServerController{}, "*:Operate"), beego.NSRouter("/delete/:serverId", &controllers.ServerController{}, "Get:Delete"), ), ) guestR := beego.NewNamespace("/guest", beego.NSBefore(func(ctx *context.Context) { commonFilter(ctx) }), beego.NSNamespace("/server", beego.NSRouter("/share/:serverId", &controllers.ServerController{}, "Get:Share"), beego.NSRouter("/:serverId/active/:apiParamId", &controllers.ServerController{}, "*:Active"), beego.NSRouter("/:serverId/encode/:apiParamId", &controllers.ServerController{}, "Post:Encode"), beego.NSRouter("/info", &controllers.ServerController{}, "Get:Info"), ), ) //注册namespace beego.AddNamespace(authR, adminR, guestR) }
func (this *AuthController) Logout() { services.GetInstance().AuthService.WithContext(this.Ctx, func() { services.GetInstance().AuthService.Logout() }) this.TplNames = "auth/login.html" }