user.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright 2017 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package model
  5. import (
  6. "errors"
  7. "time"
  8. )
  9. // User represents a user in the system.
  10. type User struct {
  11. ID int64 `json:"id"`
  12. Username string `json:"username"`
  13. Password string `json:"password,omitempty"`
  14. IsAdmin bool `json:"is_admin,omitempty"`
  15. Theme string `json:"theme,omitempty"`
  16. Language string `json:"language,omitempty"`
  17. Timezone string `json:"timezone,omitempty"`
  18. EntryDirection string `json:"entry_sorting_direction,omitempty"`
  19. LastLoginAt *time.Time `json:"last_login_at,omitempty"`
  20. Extra map[string]string `json:"-"`
  21. }
  22. // NewUser returns a new User.
  23. func NewUser() *User {
  24. return &User{Extra: make(map[string]string)}
  25. }
  26. // ValidateUserCreation validates new user.
  27. func (u User) ValidateUserCreation() error {
  28. if err := u.ValidateUserLogin(); err != nil {
  29. return err
  30. }
  31. return u.ValidatePassword()
  32. }
  33. // ValidateUserModification validates user for modification.
  34. func (u User) ValidateUserModification() error {
  35. if u.ID <= 0 {
  36. return errors.New("The ID is mandatory")
  37. }
  38. if u.Username == "" {
  39. return errors.New("The username is mandatory")
  40. }
  41. if err := u.ValidatePassword(); err != nil {
  42. return err
  43. }
  44. return ValidateTheme(u.Theme)
  45. }
  46. // ValidateUserLogin validates user credential requirements.
  47. func (u User) ValidateUserLogin() error {
  48. if u.Username == "" {
  49. return errors.New("The username is mandatory")
  50. }
  51. if u.Password == "" {
  52. return errors.New("The password is mandatory")
  53. }
  54. return nil
  55. }
  56. // ValidatePassword validates user password requirements.
  57. func (u User) ValidatePassword() error {
  58. if u.Password != "" && len(u.Password) < 6 {
  59. return errors.New("The password must have at least 6 characters")
  60. }
  61. return nil
  62. }
  63. // Merge update the current user with another user.
  64. func (u *User) Merge(override *User) {
  65. if u.Username != override.Username {
  66. u.Username = override.Username
  67. }
  68. if u.Password != override.Password {
  69. u.Password = override.Password
  70. }
  71. if u.IsAdmin != override.IsAdmin {
  72. u.IsAdmin = override.IsAdmin
  73. }
  74. if u.Theme != override.Theme {
  75. u.Theme = override.Theme
  76. }
  77. if u.Language != override.Language {
  78. u.Language = override.Language
  79. }
  80. if u.Timezone != override.Timezone {
  81. u.Timezone = override.Timezone
  82. }
  83. }
  84. // Users represents a list of users.
  85. type Users []*User