login.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package controllers
  2. import (
  3. "github.com/cnlh/nps/lib/common"
  4. "github.com/cnlh/nps/lib/file"
  5. "github.com/cnlh/nps/server"
  6. "github.com/cnlh/nps/vender/github.com/astaxie/beego"
  7. "time"
  8. )
  9. type LoginController struct {
  10. beego.Controller
  11. }
  12. func (self *LoginController) Index() {
  13. self.TplName = "login/index.html"
  14. }
  15. func (self *LoginController) Verify() {
  16. var auth bool
  17. if self.GetString("password") == beego.AppConfig.String("web_password") && self.GetString("username") == beego.AppConfig.String("web_username") {
  18. self.SetSession("isAdmin", true)
  19. auth = true
  20. server.Bridge.Register.Store(common.GetIpByAddr(self.Ctx.Request.RemoteAddr), time.Now().Add(time.Hour*time.Duration(2)))
  21. }
  22. b, err := beego.AppConfig.Bool("allow_user_login")
  23. if err == nil && b && !auth {
  24. file.GetCsvDb().Clients.Range(func(key, value interface{}) bool {
  25. v := value.(*file.Client)
  26. if !v.Status || v.NoDisplay {
  27. return true
  28. }
  29. if v.WebUserName == "" && v.WebPassword == "" {
  30. if self.GetString("username") != "user" || v.VerifyKey != self.GetString("password") {
  31. return true
  32. } else {
  33. auth = true
  34. }
  35. }
  36. if !auth && v.WebPassword == self.GetString("password") && self.GetString("username") == v.WebUserName {
  37. auth = true
  38. }
  39. if auth {
  40. self.SetSession("isAdmin", false)
  41. self.SetSession("clientId", v.Id)
  42. return false
  43. }
  44. return true
  45. })
  46. }
  47. if auth {
  48. self.SetSession("auth", true)
  49. self.Data["json"] = map[string]interface{}{"status": 1, "msg": "login success"}
  50. } else {
  51. self.Data["json"] = map[string]interface{}{"status": 0, "msg": "username or password incorrect"}
  52. }
  53. self.ServeJSON()
  54. }
  55. func (self *LoginController) Out() {
  56. self.SetSession("auth", false)
  57. self.Redirect("/login/index", 302)
  58. }