-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathuser.go
57 lines (50 loc) · 1.1 KB
/
user.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package store
import (
"time"
"unicode/utf8"
)
// User represents an authenticated user.
// Only public fields are marshalled to JSON by default.
type User struct {
ID int64 `json:"id"`
Name string `json:"name"`
CreatedAt time.Time `json:"createdAt"`
AuthService string `json:"-"`
AuthID string `json:"-"`
Blocked bool `json:"-"`
Admin bool `json:"-"`
Avatar string `json:"avatar"`
}
const (
userNameMinLen = 3
userNameMaxLen = 20
)
// validUserNameRune checks if given user name rune is valid.
func validUserNameRune(r rune) bool {
if 'a' <= r && r <= 'z' {
return true
}
if 'A' <= r && r <= 'Z' {
return true
}
if '0' <= r && r <= '9' {
return true
}
if r == '_' || r == '-' {
return true
}
return false
}
// ValidUserName checks if given user name is valid.
func ValidUserName(userName string) bool {
length := utf8.RuneCountInString(userName)
if !(userNameMinLen <= length && length <= userNameMaxLen) {
return false
}
for _, r := range userName {
if !validUserNameRune(r) {
return false
}
}
return true
}