signup.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package api
  2. import (
  3. "github.com/grafana/grafana/pkg/api/dtos"
  4. "github.com/grafana/grafana/pkg/bus"
  5. "github.com/grafana/grafana/pkg/events"
  6. "github.com/grafana/grafana/pkg/metrics"
  7. "github.com/grafana/grafana/pkg/middleware"
  8. m "github.com/grafana/grafana/pkg/models"
  9. "github.com/grafana/grafana/pkg/setting"
  10. "github.com/grafana/grafana/pkg/util"
  11. )
  12. // POST /api/user/signup
  13. func SignUp(c *middleware.Context, form dtos.SignUpForm) Response {
  14. if !setting.AllowUserSignUp {
  15. return ApiError(401, "User signup is disabled", nil)
  16. }
  17. existing := m.GetUserByLoginQuery{LoginOrEmail: form.Email}
  18. if err := bus.Dispatch(&existing); err == nil {
  19. return ApiError(401, "User with same email address already exists", nil)
  20. }
  21. cmd := m.CreateTempUserCommand{}
  22. cmd.OrgId = -1
  23. cmd.Email = form.Email
  24. cmd.Status = m.TmpUserSignUpStarted
  25. cmd.InvitedByUserId = c.UserId
  26. cmd.Code = util.GetRandomString(20)
  27. cmd.RemoteAddr = c.Req.RemoteAddr
  28. if err := bus.Dispatch(&cmd); err != nil {
  29. return ApiError(500, "Failed to create signup", err)
  30. }
  31. // user := cmd.Resu
  32. bus.Publish(&events.SignUpStarted{
  33. Email: form.Email,
  34. Code: cmd.Code,
  35. })
  36. // loginUserWithUser(&user, c)
  37. metrics.M_Api_User_SignUpStarted.Inc(1)
  38. return Json(200, util.DynMap{"status": "SignUpCreated"})
  39. }
  40. func SignUpStep2(c *middleware.Context, form dtos.SignUpStep2Form) Response {
  41. if !setting.AllowUserSignUp {
  42. return ApiError(401, "User signup is disabled", nil)
  43. }
  44. query := m.GetTempUserByCodeQuery{Code: form.Code}
  45. if err := bus.Dispatch(&query); err != nil {
  46. if err == m.ErrTempUserNotFound {
  47. return ApiError(404, "Invalid email verification code", nil)
  48. }
  49. return ApiError(500, "Failed to read temp user", err)
  50. }
  51. tempUser := query.Result
  52. if tempUser.Email != form.Email {
  53. return ApiError(404, "Email verification code does not match email", nil)
  54. }
  55. existing := m.GetUserByLoginQuery{LoginOrEmail: tempUser.Email}
  56. if err := bus.Dispatch(&existing); err == nil {
  57. return ApiError(401, "User with same email address already exists", nil)
  58. }
  59. return Json(200, util.DynMap{"status": "SignUpCreated"})
  60. }