user_auth.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package sqlstore
  2. import (
  3. "time"
  4. "github.com/grafana/grafana/pkg/bus"
  5. m "github.com/grafana/grafana/pkg/models"
  6. )
  7. func init() {
  8. bus.AddHandler("sql", GetUserByAuthInfo)
  9. bus.AddHandler("sql", GetAuthInfo)
  10. bus.AddHandler("sql", SetAuthInfo)
  11. bus.AddHandler("sql", DeleteAuthInfo)
  12. }
  13. func GetUserByAuthInfo(query *m.GetUserByAuthInfoQuery) error {
  14. user := &m.User{}
  15. has := false
  16. var err error
  17. authQuery := &m.GetAuthInfoQuery{}
  18. // Try to find the user by auth module and id first
  19. if query.AuthModule != "" && query.AuthId != "" {
  20. authQuery.AuthModule = query.AuthModule
  21. authQuery.AuthId = query.AuthId
  22. err = GetAuthInfo(authQuery)
  23. // if user id was specified and doesn't match the user_auth entry, remove it
  24. if err == nil && query.UserId != 0 && query.UserId != authQuery.Result.UserId {
  25. err = DeleteAuthInfo(&m.DeleteAuthInfoCommand{
  26. UserAuth: authQuery.Result,
  27. })
  28. if err != nil {
  29. sqlog.Error("Error removing user_auth entry", "error", err)
  30. }
  31. authQuery.Result = nil
  32. } else if err == nil {
  33. has, err = x.Id(authQuery.Result.UserId).Get(user)
  34. if err != nil {
  35. return err
  36. }
  37. if !has {
  38. // if the user has been deleted then remove the entry
  39. err = DeleteAuthInfo(&m.DeleteAuthInfoCommand{
  40. UserAuth: authQuery.Result,
  41. })
  42. if err != nil {
  43. sqlog.Error("Error removing user_auth entry", "error", err)
  44. }
  45. authQuery.Result = nil
  46. }
  47. } else if err != m.ErrUserNotFound {
  48. return err
  49. }
  50. }
  51. // If not found, try to find the user by id
  52. if !has && query.UserId != 0 {
  53. has, err = x.Id(query.UserId).Get(user)
  54. if err != nil {
  55. return err
  56. }
  57. }
  58. // If not found, try to find the user by email address
  59. if !has && query.Email != "" {
  60. user = &m.User{Email: query.Email}
  61. has, err = x.Get(user)
  62. if err != nil {
  63. return err
  64. }
  65. }
  66. // If not found, try to find the user by login
  67. if !has && query.Login != "" {
  68. user = &m.User{Login: query.Login}
  69. has, err = x.Get(user)
  70. if err != nil {
  71. return err
  72. }
  73. }
  74. // No user found
  75. if !has {
  76. return m.ErrUserNotFound
  77. }
  78. // create authInfo record to link accounts
  79. if authQuery.Result == nil && query.AuthModule != "" && query.AuthId != "" {
  80. cmd2 := &m.SetAuthInfoCommand{
  81. UserId: user.Id,
  82. AuthModule: query.AuthModule,
  83. AuthId: query.AuthId,
  84. }
  85. if err := SetAuthInfo(cmd2); err != nil {
  86. return err
  87. }
  88. }
  89. query.Result = user
  90. return nil
  91. }
  92. func GetAuthInfo(query *m.GetAuthInfoQuery) error {
  93. userAuth := &m.UserAuth{
  94. AuthModule: query.AuthModule,
  95. AuthId: query.AuthId,
  96. }
  97. has, err := x.Get(userAuth)
  98. if err != nil {
  99. return err
  100. }
  101. if !has {
  102. return m.ErrUserNotFound
  103. }
  104. query.Result = userAuth
  105. return nil
  106. }
  107. func SetAuthInfo(cmd *m.SetAuthInfoCommand) error {
  108. return inTransaction(func(sess *DBSession) error {
  109. authUser := &m.UserAuth{
  110. UserId: cmd.UserId,
  111. AuthModule: cmd.AuthModule,
  112. AuthId: cmd.AuthId,
  113. Created: time.Now(),
  114. }
  115. _, err := sess.Insert(authUser)
  116. return err
  117. })
  118. }
  119. func DeleteAuthInfo(cmd *m.DeleteAuthInfoCommand) error {
  120. return inTransaction(func(sess *DBSession) error {
  121. _, err := sess.Delete(cmd.UserAuth)
  122. return err
  123. })
  124. }