account.go 2.2 KB

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