accounts.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package sqlstore
  2. import (
  3. "time"
  4. "github.com/go-xorm/xorm"
  5. "github.com/torkelo/grafana-pro/pkg/bus"
  6. m "github.com/torkelo/grafana-pro/pkg/models"
  7. )
  8. func init() {
  9. bus.AddHandler("sql", GetAccountInfo)
  10. bus.AddHandler("sql", AddCollaborator)
  11. bus.AddHandler("sql", GetOtherAccounts)
  12. bus.AddHandler("sql", CreateAccount)
  13. bus.AddHandler("sql", SetUsingAccount)
  14. bus.AddHandler("sql", GetAccountById)
  15. bus.AddHandler("sql", GetAccountByLogin)
  16. bus.AddHandler("sql", GetAccountByToken)
  17. }
  18. func CreateAccount(cmd *m.CreateAccountCommand) error {
  19. return inTransaction(func(sess *xorm.Session) error {
  20. account := m.Account{
  21. Email: cmd.Email,
  22. Login: cmd.Login,
  23. Password: cmd.Password,
  24. Salt: cmd.Salt,
  25. Created: time.Now(),
  26. Updated: time.Now(),
  27. }
  28. _, err := sess.Insert(&account)
  29. cmd.Result = account
  30. return err
  31. })
  32. }
  33. func SetUsingAccount(cmd *m.SetUsingAccountCommand) error {
  34. return inTransaction(func(sess *xorm.Session) error {
  35. account := m.Account{}
  36. sess.Id(cmd.AccountId).Get(&account)
  37. account.UsingAccountId = cmd.UsingAccountId
  38. _, err := sess.Id(account.Id).Update(&account)
  39. return err
  40. })
  41. }
  42. func GetAccountInfo(query *m.GetAccountInfoQuery) error {
  43. var account m.Account
  44. has, err := x.Id(query.Id).Get(&account)
  45. if err != nil {
  46. return err
  47. } else if has == false {
  48. return m.ErrAccountNotFound
  49. }
  50. query.Result = m.AccountDTO{
  51. Name: account.Name,
  52. Email: account.Email,
  53. Collaborators: make([]*m.CollaboratorDTO, 0),
  54. }
  55. sess := x.Table("collaborator")
  56. sess.Join("INNER", "account", "account.id=collaborator.account_Id")
  57. sess.Where("collaborator.for_account_id=?", query.Id)
  58. err = sess.Find(&query.Result.Collaborators)
  59. return err
  60. }
  61. func AddCollaborator(cmd *m.AddCollaboratorCommand) error {
  62. return inTransaction(func(sess *xorm.Session) error {
  63. entity := m.Collaborator{
  64. AccountId: cmd.AccountId,
  65. ForAccountId: cmd.ForAccountId,
  66. Role: cmd.Role,
  67. Created: time.Now(),
  68. Updated: time.Now(),
  69. }
  70. _, err := sess.Insert(&entity)
  71. return err
  72. })
  73. }
  74. func GetAccountById(query *m.GetAccountByIdQuery) error {
  75. var err error
  76. var account m.Account
  77. has, err := x.Id(query.Id).Get(&account)
  78. if err != nil {
  79. return err
  80. } else if has == false {
  81. return m.ErrAccountNotFound
  82. }
  83. if account.UsingAccountId == 0 {
  84. account.UsingAccountId = account.Id
  85. }
  86. query.Result = &account
  87. return nil
  88. }
  89. func GetAccountByToken(query *m.GetAccountByTokenQuery) error {
  90. var err error
  91. var account m.Account
  92. sess := x.Join("INNER", "token", "token.account_id = account.id")
  93. sess.Omit("token.id", "token.account_id", "token.name", "token.token",
  94. "token.role", "token.updated", "token.created")
  95. has, err := sess.Where("token.token=?", query.Token).Get(&account)
  96. if err != nil {
  97. return err
  98. } else if has == false {
  99. return m.ErrAccountNotFound
  100. }
  101. if account.UsingAccountId == 0 {
  102. account.UsingAccountId = account.Id
  103. }
  104. query.Result = &account
  105. return nil
  106. }
  107. func GetAccountByLogin(query *m.GetAccountByLoginQuery) error {
  108. var err error
  109. account := m.Account{Login: query.Login}
  110. has, err := x.Get(&account)
  111. if err != nil {
  112. return err
  113. } else if has == false {
  114. return m.ErrAccountNotFound
  115. }
  116. if account.UsingAccountId == 0 {
  117. account.UsingAccountId = account.Id
  118. }
  119. query.Result = &account
  120. return nil
  121. }
  122. func GetOtherAccounts(query *m.GetOtherAccountsQuery) error {
  123. query.Result = make([]*m.OtherAccountDTO, 0)
  124. sess := x.Table("collaborator")
  125. sess.Join("INNER", "account", "collaborator.for_account_id=account.id")
  126. sess.Where("account_id=?", query.AccountId)
  127. sess.Cols("collaborator.id", "collaborator.role", "account.email")
  128. err := sess.Find(&query.Result)
  129. return err
  130. }