user_auth.go 2.7 KB

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