| iomodo | 48c837e | 2021-02-19 01:17:07 +0400 | [diff] [blame] | 1 | package model |
| 2 | |
| 3 | import ( |
| iomodo | 48c837e | 2021-02-19 01:17:07 +0400 | [diff] [blame] | 4 | "regexp" |
| iomodo | 48c837e | 2021-02-19 01:17:07 +0400 | [diff] [blame] | 5 | "unicode" |
| 6 | |
| 7 | "github.com/pkg/errors" |
| 8 | ) |
| 9 | |
| 10 | const ( |
| iomodo | 838bd5c | 2021-03-21 15:37:03 +0400 | [diff] [blame] | 11 | userNameMaxLength = 64 |
| 12 | userNameMinLength = 1 |
| iomodo | 48c837e | 2021-02-19 01:17:07 +0400 | [diff] [blame] | 13 | ) |
| 14 | |
| 15 | // User contains the details about the user. |
| iomodo | 48c837e | 2021-02-19 01:17:07 +0400 | [diff] [blame] | 16 | type User struct { |
| 17 | ID string `json:"id"` |
| 18 | CreateAt int64 `json:"create_at,omitempty"` |
| 19 | UpdateAt int64 `json:"update_at,omitempty"` |
| 20 | DeleteAt int64 `json:"delete_at"` |
| 21 | Username string `json:"username"` |
| 22 | Password string `json:"password,omitempty"` |
| iomodo | 48c837e | 2021-02-19 01:17:07 +0400 | [diff] [blame] | 23 | LastPasswordUpdate int64 `json:"last_password_update,omitempty"` |
| 24 | } |
| 25 | |
| 26 | // IsValid validates the user and returns an error if it isn't configured |
| 27 | // correctly. |
| 28 | func (u *User) IsValid() error { |
| 29 | if !isValidID(u.ID) { |
| 30 | return invalidUserError("id", "") |
| 31 | } |
| 32 | |
| 33 | if u.CreateAt == 0 { |
| 34 | return invalidUserError("create_at", u.ID) |
| 35 | } |
| 36 | |
| 37 | if u.UpdateAt == 0 { |
| 38 | return invalidUserError("update_at", u.ID) |
| 39 | } |
| 40 | |
| 41 | if !isValidUsername(u.Username) { |
| 42 | return invalidUserError("username", u.ID) |
| 43 | } |
| 44 | |
| iomodo | 48c837e | 2021-02-19 01:17:07 +0400 | [diff] [blame] | 45 | return nil |
| 46 | } |
| 47 | |
| iomodo | 493dd48 | 2021-04-02 00:07:51 +0400 | [diff] [blame] | 48 | // IsValidInput validates the user input and returns an error |
| 49 | func (u *User) IsValidInput() error { |
| 50 | if !isValidUsername(u.Username) { |
| 51 | return invalidUserError("username", u.ID) |
| 52 | } |
| 53 | |
| 54 | return nil |
| 55 | } |
| 56 | |
| iomodo | 352127d | 2021-03-26 20:10:32 +0400 | [diff] [blame] | 57 | // Clone clones the object |
| iomodo | 07334d2 | 2021-02-24 01:08:40 +0400 | [diff] [blame] | 58 | func (u *User) Clone() *User { |
| 59 | user := *u |
| 60 | return &user |
| 61 | } |
| 62 | |
| iomodo | 352127d | 2021-03-26 20:10:32 +0400 | [diff] [blame] | 63 | // SanitizeInput removes input data from the user object that is not user controlled |
| 64 | func (u *User) SanitizeInput() { |
| 65 | u.ID = "" |
| 66 | u.CreateAt = 0 |
| 67 | u.UpdateAt = 0 |
| 68 | u.DeleteAt = 0 |
| 69 | u.LastPasswordUpdate = 0 |
| 70 | } |
| 71 | |
| 72 | // SanitizeOutput removes output data from the user object that is not user controlled |
| 73 | func (u *User) SanitizeOutput() { |
| 74 | u.Password = "" |
| 75 | } |
| 76 | |
| iomodo | 48c837e | 2021-02-19 01:17:07 +0400 | [diff] [blame] | 77 | func isValidID(value string) bool { |
| 78 | if len(value) != 26 { |
| 79 | return false |
| 80 | } |
| 81 | |
| 82 | for _, r := range value { |
| 83 | if !unicode.IsLetter(r) && !unicode.IsNumber(r) { |
| 84 | return false |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | return true |
| 89 | } |
| 90 | |
| 91 | func invalidUserError(fieldName string, userID string) error { |
| 92 | return errors.Errorf("Invalid User field: %s; id: %s", fieldName, userID) |
| 93 | } |
| 94 | |
| 95 | func isValidUsername(s string) bool { |
| 96 | if len(s) < userNameMinLength || len(s) > userNameMaxLength { |
| 97 | return false |
| 98 | } |
| 99 | |
| 100 | validUsernameChars := regexp.MustCompile(`^[a-z0-9\.\-_]+$`) |
| iomodo | 48c837e | 2021-02-19 01:17:07 +0400 | [diff] [blame] | 101 | |
| iomodo | 838bd5c | 2021-03-21 15:37:03 +0400 | [diff] [blame] | 102 | return validUsernameChars.MatchString(s) |
| iomodo | 48c837e | 2021-02-19 01:17:07 +0400 | [diff] [blame] | 103 | } |