account.go 2.1 KB

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