You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

142 line
3.1KB

  1. package main
  2. import (
  3. "biukop.com/sfm/loan"
  4. "database/sql"
  5. "encoding/json"
  6. log "github.com/sirupsen/logrus"
  7. "net/http"
  8. "time"
  9. )
  10. type loginForm struct {
  11. Login string `json:"u"`
  12. Pass string `json:"p"`
  13. }
  14. func apiV1Login(w http.ResponseWriter, r *http.Request, ss *loan.Session) {
  15. res := apiV1ResponseBlank()
  16. l := loginForm{}
  17. e := l.retrieveLoginForm(r)
  18. if e != nil {
  19. log.Warn("Failed login - cannot analyze request " + e.Error())
  20. res.add("login", false)
  21. res.add("reason", "incomplete request")
  22. res.sendJson(w)
  23. return
  24. }
  25. //try login
  26. _, e = ss.Login(l.Login, l.Pass)
  27. if e == sql.ErrNoRows { //not found
  28. log.Warnf("Failed login - user not found %+v, error=%s", l, e.Error())
  29. res.add("login", false)
  30. res.add("reason", "User not found")
  31. res.sendJson(w)
  32. return
  33. } else if e != nil {
  34. log.Warn("Failed login - cannot init session " + e.Error())
  35. res.add("login", false)
  36. res.add("reason", "either user name or password is not right")
  37. res.sendJson(w)
  38. return
  39. } else {
  40. //Audit user login, in db
  41. log.Info("login success ", l.Login)
  42. }
  43. //format response
  44. res.add("login", true)
  45. res.add("role", ss.GetRole())
  46. u, e := ss.GetUser()
  47. if e == nil {
  48. res.add("User", u.People)
  49. res.add("UserExtra", getUserExtraForLogin(u, ss))
  50. uex := loan.UserEx{}
  51. err := uex.Read(u.People.Id)
  52. if err != nil {
  53. log.Error("cannot read UserEx when login", err)
  54. } else {
  55. res.add("UserEx", uex)
  56. }
  57. }
  58. res.add("Biukop-Session", ss.Id)
  59. res.add("Biukop-Mid", ss.Get("Biukop-Mid"))
  60. res.add("sessionExpire", ss.ExpireStr())
  61. res.add("sessionExpireHuman", ss.Expire.Format(time.RFC1123Z))
  62. if config.Debug {
  63. u, e := ss.GetUser()
  64. if e == nil {
  65. res.Env.Body["debug_session_user"] = u
  66. res.Env.Session = *ss
  67. } else {
  68. log.Warn("cannot read user for session ", ss)
  69. res.Env.Body["debug_session_user_error"] = e.Error()
  70. }
  71. }
  72. WsNotifyNewLogin(ss)
  73. //send out
  74. apiV1AddTrackingCookie(w, r, ss) //always the last one to set cookies
  75. res.sendJson(w)
  76. }
  77. func getUserExtraForLogin(u loan.User, ss *loan.Session) (ret UserExtra) {
  78. if ss.GetRole() == "user" {
  79. ret = UserExtra{
  80. Login: u.Login,
  81. BSB: "",
  82. ACC: "",
  83. License: "",
  84. Organization: "",
  85. Role: "user",
  86. }
  87. }
  88. if ss.GetRole() == "broker" {
  89. broker := loan.Broker{}
  90. e := broker.Read(ss.User)
  91. if e != nil {
  92. log.Error("fail to retrieve broker for session ", ss, e.Error())
  93. } else {
  94. ret = UserExtra{
  95. Login: broker.Login,
  96. BSB: broker.BSB,
  97. ACC: broker.ACC,
  98. License: broker.License,
  99. Organization: broker.Organization,
  100. Role: "broker",
  101. }
  102. }
  103. }
  104. if ss.GetRole() == "admin" {
  105. ret = UserExtra{
  106. Login: u.Login,
  107. BSB: "",
  108. ACC: "",
  109. License: "",
  110. Organization: "SFM",
  111. Role: "admin",
  112. }
  113. }
  114. return
  115. }
  116. func (m *loginForm) retrieveLoginForm(r *http.Request) (e error) {
  117. e = apiV1DecodeRequestBody(m, r)
  118. if e != nil {
  119. log.Error(e)
  120. return
  121. }
  122. return
  123. }
  124. func apiV1DecodeRequestBody(bb interface{}, r *http.Request) (e error) {
  125. decoder := json.NewDecoder(r.Body)
  126. decoder.DisallowUnknownFields()
  127. e = decoder.Decode(bb)
  128. return
  129. }