-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
170 lines (147 loc) · 4.56 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package models
import (
"encoding/json"
"io"
"net/http"
"net/mail"
"regexp"
"strings"
"unicode/utf8"
u "github.com/titouanfreville/popcubeexternalapi/utils"
)
const (
userNotifyAll = "all"
userNotifyMention = "mention"
userNotifyNone = "none"
userAuthServiceEmail = "email"
userAuthServiceUsername = "username"
)
var (
userChannel = []string{"general", "random"}
// Protected user name cause they are taken by system or used for special mentions.
restrictedUsernames = []string{
"all",
"channel",
"popcubebot",
"here",
}
// EmptyUser em user
EmptyUser = User{}
// Definition of character user can possess in there names.
validUsernameChars = regexp.MustCompile(`^[a-z0-9\.\-_]+$`)
)
// User object.
//
// An user is an account who have an access to a specific organisation. Each user is unique inside a given organisation, but users are not shared between
// organisations. Required apply only for creation of the object.
//
// swagger:model
type User struct {
// id of the user
//
// min: 0
IDUser uint64 `gorm:"primary_key;column:idUser;AUTO_INCREMENT" json:"id,omitempty"`
// User name
//
// required: true
// max length: 64
Username string `gorm:"column:userName; not null; unique;" json:"username,omitempty"`
// User email
//
// required: true
// max lenght: 128
Email string `gorm:"column:email; not null; unique;" json:"email,omitempty"`
// State if email was verified
//
// required: true
EmailVerified bool `gorm:"column:emailVerified; not null;" json:"email_verified,omitempty"`
// User is deleted from organisation but still in database
//
// required: true
Deleted bool `gorm:"column:deleted; not null;" json:"deleted,omitempty"`
// Avatar used by user
Avatar string `gorm:"column:avatar;" json:"avatar, omitempty"`
// User nickname
NickName string `gorm:"column:nickName; unique" json:"nickname, omitempty"`
// First name
FirstName string `gorm:"column:firstName;" json:"first_name, omitempty"`
// User Lastname
LastName string `gorm:"column:lastName;" json:"last_name, omitempty"`
Organisation []Organisation `gorm:"ForeignKey:IDOrganisation;AssociationForeignKey:Refer" db:"-" json:"-"`
// Org key of user in the organisation
//
// required: true
IDOrganisation uint64 `gorm:"column:idOrganisation; not null;" json:"id_organisation,omitempty"`
}
// Bind method used in API to manage request.
func (user *User) Bind(r *http.Request) error {
return nil
}
// IsValid valwebIDates the user and returns an error if it isn't configured
// correctly.
func (user *User) IsValid(isUpdate bool) *u.AppError {
if !isUpdate {
if len(user.Email) == 0 {
return u.NewLocAppError("user.IsValid", "model.user.is_valid.Email.app_error", nil, "")
}
}
if !IsValidUsername(user.Username) {
return u.NewLocAppError("user.IsValid", "model.user.is_valid.Username.app_error", nil, "")
}
if len(user.Email) > 128 || !IsValidEmail(user.Email) {
return u.NewLocAppError("user.IsValid", "model.user.is_valid.Email.app_error", nil, "")
}
if utf8.RuneCountInString(user.NickName) > 64 {
return u.NewLocAppError("user.IsValid", "model.user.is_valid.NickName.app_error", nil, "")
}
if utf8.RuneCountInString(user.FirstName) > 64 {
return u.NewLocAppError("user.IsValid", "model.user.is_valid.first_name.app_error", nil, "")
}
if utf8.RuneCountInString(user.LastName) > 64 {
return u.NewLocAppError("user.IsValid", "model.user.is_valid.last_name.app_error", nil, "")
}
return nil
}
// PreSave have to be run before saving user in DB. It will fill necessary information (webID, username, etc. ) and hash password
func (user *User) PreSave() {
user.Username = strings.ToLower(user.Username)
user.Email = strings.ToLower(user.Email)
}
// ToJSON convert a user to a json string
func (user *User) ToJSON() string {
b, err := json.Marshal(user)
if err != nil {
return ""
}
return string(b)
}
// UserFromJSON will decode the input and return a user
func UserFromJSON(data io.Reader) *User {
decoder := json.NewDecoder(data)
var user User
err := decoder.Decode(&user)
if err == nil {
return &user
}
return nil
}
// IsValidUsername will check if provided userName is correct
func IsValidUsername(user string) bool {
if len(user) == 0 || len(user) > 64 {
return false
}
if !validUsernameChars.MatchString(user) {
return false
}
for _, restrictedUsername := range restrictedUsernames {
if user == restrictedUsername {
return false
}
}
return true
}
// IsValidEmail check email validity
func IsValidEmail(email string) bool {
_, err := mail.ParseAddress(email)
return err == nil && u.IsLower(email)
}