user.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. LastLoginAt *time.Time `json:"last_login_at,omitempty"`
  19. Extra map[string]string `json:"-"`
  20. }
  21. // NewUser returns a new User.
  22. func NewUser() *User {
  23. return &User{Extra: make(map[string]string)}
  24. }
  25. // ValidateUserCreation validates new user.
  26. func (u User) ValidateUserCreation() error {
  27. if err := u.ValidateUserLogin(); err != nil {
  28. return err
  29. }
  30. if err := u.ValidatePassword(); err != nil {
  31. return err
  32. }
  33. return nil
  34. }
  35. // ValidateUserModification validates user for modification.
  36. func (u User) ValidateUserModification() error {
  37. if u.Username == "" {
  38. return errors.New("The username is mandatory")
  39. }
  40. if err := u.ValidatePassword(); err != nil {
  41. return err
  42. }
  43. if err := ValidateTheme(u.Theme); err != nil {
  44. return err
  45. }
  46. return nil
  47. }
  48. // ValidateUserLogin validates user credential requirements.
  49. func (u User) ValidateUserLogin() error {
  50. if u.Username == "" {
  51. return errors.New("The username is mandatory")
  52. }
  53. if u.Password == "" {
  54. return errors.New("The password is mandatory")
  55. }
  56. return nil
  57. }
  58. // ValidatePassword validates user password requirements.
  59. func (u User) ValidatePassword() error {
  60. if u.Password != "" && len(u.Password) < 6 {
  61. return errors.New("The password must have at least 6 characters")
  62. }
  63. return nil
  64. }
  65. // Merge update the current user with another user.
  66. func (u *User) Merge(override *User) {
  67. if u.Username != override.Username {
  68. u.Username = override.Username
  69. }
  70. if u.Password != override.Password {
  71. u.Password = override.Password
  72. }
  73. if u.IsAdmin != override.IsAdmin {
  74. u.IsAdmin = override.IsAdmin
  75. }
  76. if u.Theme != override.Theme {
  77. u.Theme = override.Theme
  78. }
  79. if u.Language != override.Language {
  80. u.Language = override.Language
  81. }
  82. if u.Timezone != override.Timezone {
  83. u.Timezone = override.Timezone
  84. }
  85. }
  86. // Users represents a list of users.
  87. type Users []*User