user_auth.go 2.6 KB

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