account.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package models
  2. import (
  3. "errors"
  4. "time"
  5. )
  6. var (
  7. CreateAccount func(acccount *Account) error
  8. UpdateAccount func(acccount *Account) error
  9. GetAccountByLogin func(emailOrName string) (*Account, error)
  10. GetAccount func(accountId int64) (*Account, error)
  11. GetOtherAccountsFor func(accountId int64) ([]*OtherAccount, error)
  12. GetCollaboratorsForAccount func(accountId int64) ([]*CollaboratorInfo, error)
  13. AddCollaborator func(collaborator *Collaborator) error
  14. )
  15. // Typed errors
  16. var (
  17. ErrAccountNotFound = errors.New("Account not found")
  18. )
  19. type CollaboratorLink struct {
  20. AccountId int64
  21. Role string
  22. Email string
  23. ModifiedOn time.Time
  24. CreatedOn time.Time
  25. }
  26. type OtherAccount struct {
  27. Id int64
  28. Email string
  29. Role string
  30. }
  31. type Account struct {
  32. Id int64
  33. Login string `xorm:"UNIQUE NOT NULL"`
  34. Email string `xorm:"UNIQUE NOT NULL"`
  35. Name string `xorm:"UNIQUE NOT NULL"`
  36. FullName string
  37. Password string
  38. IsAdmin bool
  39. Salt string `xorm:"VARCHAR(10)"`
  40. Company string
  41. NextDashboardId int
  42. UsingAccountId int64
  43. Collaborators []CollaboratorLink `xorm:"-"`
  44. Created time.Time `xorm:"CREATED"`
  45. Updated time.Time `xorm:"UPDATED"`
  46. }
  47. func (account *Account) AddCollaborator(newCollaborator *Account) error {
  48. for _, collaborator := range account.Collaborators {
  49. if collaborator.AccountId == newCollaborator.Id {
  50. return errors.New("Collaborator already exists")
  51. }
  52. }
  53. account.Collaborators = append(account.Collaborators, CollaboratorLink{
  54. AccountId: newCollaborator.Id,
  55. Email: newCollaborator.Email,
  56. Role: "admin",
  57. CreatedOn: time.Now(),
  58. ModifiedOn: time.Now(),
  59. })
  60. return nil
  61. }
  62. func (account *Account) RemoveCollaborator(accountId int64) {
  63. list := account.Collaborators
  64. for i, collaborator := range list {
  65. if collaborator.AccountId == accountId {
  66. account.Collaborators = append(list[:i], list[i+1:]...)
  67. break
  68. }
  69. }
  70. }
  71. func (account *Account) HasCollaborator(accountId int64) bool {
  72. for _, collaborator := range account.Collaborators {
  73. if collaborator.AccountId == accountId {
  74. return true
  75. }
  76. }
  77. return false
  78. }