user.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 modification payload.
  34. func (u User) ValidateUserModification() error {
  35. if u.Theme != "" {
  36. return ValidateTheme(u.Theme)
  37. }
  38. if u.Password != "" {
  39. return u.ValidatePassword()
  40. }
  41. return nil
  42. }
  43. // ValidateUserLogin validates user credential requirements.
  44. func (u User) ValidateUserLogin() error {
  45. if u.Username == "" {
  46. return errors.New("The username is mandatory")
  47. }
  48. if u.Password == "" {
  49. return errors.New("The password is mandatory")
  50. }
  51. return nil
  52. }
  53. // ValidatePassword validates user password requirements.
  54. func (u User) ValidatePassword() error {
  55. if u.Password != "" && len(u.Password) < 6 {
  56. return errors.New("The password must have at least 6 characters")
  57. }
  58. return nil
  59. }
  60. // Merge update the current user with another user.
  61. func (u *User) Merge(override *User) {
  62. if override.Username != "" && u.Username != override.Username {
  63. u.Username = override.Username
  64. }
  65. if override.Password != "" && u.Password != override.Password {
  66. u.Password = override.Password
  67. }
  68. if u.IsAdmin != override.IsAdmin {
  69. u.IsAdmin = override.IsAdmin
  70. }
  71. if override.Theme != "" && u.Theme != override.Theme {
  72. u.Theme = override.Theme
  73. }
  74. if override.Language != "" && u.Language != override.Language {
  75. u.Language = override.Language
  76. }
  77. if override.Timezone != "" && u.Timezone != override.Timezone {
  78. u.Timezone = override.Timezone
  79. }
  80. }
  81. // Users represents a list of users.
  82. type Users []*User