user_auth.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 err != m.ErrUserNotFound {
  24. if err != nil {
  25. return err
  26. }
  27. // if user id was specified and doesn't match the user_auth entry, remove it
  28. if query.UserId != 0 && query.UserId != authQuery.Result.UserId {
  29. err = DeleteAuthInfo(&m.DeleteAuthInfoCommand{
  30. UserAuth: authQuery.Result,
  31. })
  32. if err != nil {
  33. sqlog.Error("Error removing user_auth entry", "error", err)
  34. }
  35. authQuery.Result = nil
  36. } else {
  37. has, err = x.Id(authQuery.Result.UserId).Get(user)
  38. if err != nil {
  39. return err
  40. }
  41. if !has {
  42. // if the user has been deleted then remove the entry
  43. err = DeleteAuthInfo(&m.DeleteAuthInfoCommand{
  44. UserAuth: authQuery.Result,
  45. })
  46. if err != nil {
  47. sqlog.Error("Error removing user_auth entry", "error", err)
  48. }
  49. authQuery.Result = nil
  50. }
  51. }
  52. }
  53. }
  54. // If not found, try to find the user by id
  55. if !has && query.UserId != 0 {
  56. has, err = x.Id(query.UserId).Get(user)
  57. if err != nil {
  58. return err
  59. }
  60. }
  61. // If not found, try to find the user by email address
  62. if !has && query.Email != "" {
  63. user = &m.User{Email: query.Email}
  64. has, err = x.Get(user)
  65. if err != nil {
  66. return err
  67. }
  68. }
  69. // If not found, try to find the user by login
  70. if !has && query.Login != "" {
  71. user = &m.User{Login: query.Login}
  72. has, err = x.Get(user)
  73. if err != nil {
  74. return err
  75. }
  76. }
  77. // No user found
  78. if !has {
  79. return m.ErrUserNotFound
  80. }
  81. // create authInfo record to link accounts
  82. if authQuery.Result == nil && query.AuthModule != "" && query.AuthId != "" {
  83. cmd2 := &m.SetAuthInfoCommand{
  84. UserId: user.Id,
  85. AuthModule: query.AuthModule,
  86. AuthId: query.AuthId,
  87. }
  88. if err := SetAuthInfo(cmd2); err != nil {
  89. return err
  90. }
  91. }
  92. query.Result = user
  93. return nil
  94. }
  95. func GetAuthInfo(query *m.GetAuthInfoQuery) error {
  96. userAuth := &m.UserAuth{
  97. AuthModule: query.AuthModule,
  98. AuthId: query.AuthId,
  99. }
  100. has, err := x.Get(userAuth)
  101. if err != nil {
  102. return err
  103. }
  104. if !has {
  105. return m.ErrUserNotFound
  106. }
  107. query.Result = userAuth
  108. return nil
  109. }
  110. func SetAuthInfo(cmd *m.SetAuthInfoCommand) error {
  111. return inTransaction(func(sess *DBSession) error {
  112. authUser := &m.UserAuth{
  113. UserId: cmd.UserId,
  114. AuthModule: cmd.AuthModule,
  115. AuthId: cmd.AuthId,
  116. Created: time.Now(),
  117. }
  118. _, err := sess.Insert(authUser)
  119. return err
  120. })
  121. }
  122. func DeleteAuthInfo(cmd *m.DeleteAuthInfoCommand) error {
  123. return inTransaction(func(sess *DBSession) error {
  124. _, err := sess.Delete(cmd.UserAuth)
  125. return err
  126. })
  127. }