github.com/wgh-/mattermost-server@v4.8.0-rc2+incompatible/model/team.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package model 5 6 import ( 7 "encoding/json" 8 "fmt" 9 "io" 10 "net/http" 11 "regexp" 12 "strings" 13 "unicode/utf8" 14 ) 15 16 const ( 17 TEAM_OPEN = "O" 18 TEAM_INVITE = "I" 19 TEAM_ALLOWED_DOMAINS_MAX_LENGTH = 500 20 TEAM_COMPANY_NAME_MAX_LENGTH = 64 21 TEAM_DESCRIPTION_MAX_LENGTH = 255 22 TEAM_DISPLAY_NAME_MAX_RUNES = 64 23 TEAM_EMAIL_MAX_LENGTH = 128 24 TEAM_NAME_MAX_LENGTH = 64 25 TEAM_NAME_MIN_LENGTH = 2 26 ) 27 28 type Team struct { 29 Id string `json:"id"` 30 CreateAt int64 `json:"create_at"` 31 UpdateAt int64 `json:"update_at"` 32 DeleteAt int64 `json:"delete_at"` 33 DisplayName string `json:"display_name"` 34 Name string `json:"name"` 35 Description string `json:"description"` 36 Email string `json:"email"` 37 Type string `json:"type"` 38 CompanyName string `json:"company_name"` 39 AllowedDomains string `json:"allowed_domains"` 40 InviteId string `json:"invite_id"` 41 AllowOpenInvite bool `json:"allow_open_invite"` 42 } 43 44 type TeamPatch struct { 45 DisplayName *string `json:"display_name"` 46 Description *string `json:"description"` 47 CompanyName *string `json:"company_name"` 48 InviteId *string `json:"invite_id"` 49 AllowOpenInvite *bool `json:"allow_open_invite"` 50 } 51 52 type Invites struct { 53 Invites []map[string]string `json:"invites"` 54 } 55 56 func InvitesFromJson(data io.Reader) *Invites { 57 var o *Invites 58 json.NewDecoder(data).Decode(&o) 59 return o 60 } 61 62 func (o *Invites) ToEmailList() []string { 63 emailList := make([]string, len(o.Invites)) 64 for _, invite := range o.Invites { 65 emailList = append(emailList, invite["email"]) 66 } 67 return emailList 68 } 69 70 func (o *Invites) ToJson() string { 71 b, _ := json.Marshal(o) 72 return string(b) 73 } 74 75 func (o *Team) ToJson() string { 76 b, _ := json.Marshal(o) 77 return string(b) 78 } 79 80 func TeamFromJson(data io.Reader) *Team { 81 var o *Team 82 json.NewDecoder(data).Decode(&o) 83 return o 84 } 85 86 func TeamMapToJson(u map[string]*Team) string { 87 b, _ := json.Marshal(u) 88 return string(b) 89 } 90 91 func TeamMapFromJson(data io.Reader) map[string]*Team { 92 var teams map[string]*Team 93 json.NewDecoder(data).Decode(&teams) 94 return teams 95 } 96 97 func TeamListToJson(t []*Team) string { 98 b, _ := json.Marshal(t) 99 return string(b) 100 } 101 102 func TeamListFromJson(data io.Reader) []*Team { 103 var teams []*Team 104 json.NewDecoder(data).Decode(&teams) 105 return teams 106 } 107 108 func (o *Team) Etag() string { 109 return Etag(o.Id, o.UpdateAt) 110 } 111 112 func (o *Team) IsValid() *AppError { 113 114 if len(o.Id) != 26 { 115 return NewAppError("Team.IsValid", "model.team.is_valid.id.app_error", nil, "", http.StatusBadRequest) 116 } 117 118 if o.CreateAt == 0 { 119 return NewAppError("Team.IsValid", "model.team.is_valid.create_at.app_error", nil, "id="+o.Id, http.StatusBadRequest) 120 } 121 122 if o.UpdateAt == 0 { 123 return NewAppError("Team.IsValid", "model.team.is_valid.update_at.app_error", nil, "id="+o.Id, http.StatusBadRequest) 124 } 125 126 if len(o.Email) > TEAM_EMAIL_MAX_LENGTH { 127 return NewAppError("Team.IsValid", "model.team.is_valid.email.app_error", nil, "id="+o.Id, http.StatusBadRequest) 128 } 129 130 if len(o.Email) > 0 && !IsValidEmail(o.Email) { 131 return NewAppError("Team.IsValid", "model.team.is_valid.email.app_error", nil, "id="+o.Id, http.StatusBadRequest) 132 } 133 134 if utf8.RuneCountInString(o.DisplayName) == 0 || utf8.RuneCountInString(o.DisplayName) > TEAM_DISPLAY_NAME_MAX_RUNES { 135 return NewAppError("Team.IsValid", "model.team.is_valid.name.app_error", nil, "id="+o.Id, http.StatusBadRequest) 136 } 137 138 if len(o.Name) > TEAM_NAME_MAX_LENGTH { 139 return NewAppError("Team.IsValid", "model.team.is_valid.url.app_error", nil, "id="+o.Id, http.StatusBadRequest) 140 } 141 142 if len(o.Description) > TEAM_DESCRIPTION_MAX_LENGTH { 143 return NewAppError("Team.IsValid", "model.team.is_valid.description.app_error", nil, "id="+o.Id, http.StatusBadRequest) 144 } 145 146 if IsReservedTeamName(o.Name) { 147 return NewAppError("Team.IsValid", "model.team.is_valid.reserved.app_error", nil, "id="+o.Id, http.StatusBadRequest) 148 } 149 150 if !IsValidTeamName(o.Name) { 151 return NewAppError("Team.IsValid", "model.team.is_valid.characters.app_error", nil, "id="+o.Id, http.StatusBadRequest) 152 } 153 154 if !(o.Type == TEAM_OPEN || o.Type == TEAM_INVITE) { 155 return NewAppError("Team.IsValid", "model.team.is_valid.type.app_error", nil, "id="+o.Id, http.StatusBadRequest) 156 } 157 158 if len(o.CompanyName) > TEAM_COMPANY_NAME_MAX_LENGTH { 159 return NewAppError("Team.IsValid", "model.team.is_valid.company.app_error", nil, "id="+o.Id, http.StatusBadRequest) 160 } 161 162 if len(o.AllowedDomains) > TEAM_ALLOWED_DOMAINS_MAX_LENGTH { 163 return NewAppError("Team.IsValid", "model.team.is_valid.domains.app_error", nil, "id="+o.Id, http.StatusBadRequest) 164 } 165 166 return nil 167 } 168 169 func (o *Team) PreSave() { 170 if o.Id == "" { 171 o.Id = NewId() 172 } 173 174 o.CreateAt = GetMillis() 175 o.UpdateAt = o.CreateAt 176 177 if len(o.InviteId) == 0 { 178 o.InviteId = NewId() 179 } 180 } 181 182 func (o *Team) PreUpdate() { 183 o.UpdateAt = GetMillis() 184 } 185 186 func IsReservedTeamName(s string) bool { 187 s = strings.ToLower(s) 188 189 for _, value := range reservedName { 190 if strings.Index(s, value) == 0 { 191 return true 192 } 193 } 194 195 return false 196 } 197 198 func IsValidTeamName(s string) bool { 199 200 if !IsValidAlphaNum(s) { 201 return false 202 } 203 204 if len(s) < TEAM_NAME_MIN_LENGTH { 205 return false 206 } 207 208 return true 209 } 210 211 var validTeamNameCharacter = regexp.MustCompile(`^[a-z0-9-]$`) 212 213 func CleanTeamName(s string) string { 214 s = strings.ToLower(strings.Replace(s, " ", "-", -1)) 215 216 for _, value := range reservedName { 217 if strings.Index(s, value) == 0 { 218 s = strings.Replace(s, value, "", -1) 219 } 220 } 221 222 s = strings.TrimSpace(s) 223 224 for _, c := range s { 225 char := fmt.Sprintf("%c", c) 226 if !validTeamNameCharacter.MatchString(char) { 227 s = strings.Replace(s, char, "", -1) 228 } 229 } 230 231 s = strings.Trim(s, "-") 232 233 if !IsValidTeamName(s) { 234 s = NewId() 235 } 236 237 return s 238 } 239 240 func (o *Team) Sanitize() { 241 o.Email = "" 242 o.AllowedDomains = "" 243 } 244 245 func (o *Team) SanitizeForNotLoggedIn() { 246 o.Email = "" 247 o.AllowedDomains = "" 248 o.CompanyName = "" 249 if !o.AllowOpenInvite { 250 o.InviteId = "" 251 } 252 } 253 254 func (t *Team) Patch(patch *TeamPatch) { 255 if patch.DisplayName != nil { 256 t.DisplayName = *patch.DisplayName 257 } 258 259 if patch.Description != nil { 260 t.Description = *patch.Description 261 } 262 263 if patch.CompanyName != nil { 264 t.CompanyName = *patch.CompanyName 265 } 266 267 if patch.InviteId != nil { 268 t.InviteId = *patch.InviteId 269 } 270 271 if patch.AllowOpenInvite != nil { 272 t.AllowOpenInvite = *patch.AllowOpenInvite 273 } 274 } 275 276 func (t *TeamPatch) ToJson() string { 277 b, err := json.Marshal(t) 278 if err != nil { 279 return "" 280 } 281 282 return string(b) 283 } 284 285 func TeamPatchFromJson(data io.Reader) *TeamPatch { 286 decoder := json.NewDecoder(data) 287 var team TeamPatch 288 err := decoder.Decode(&team) 289 if err != nil { 290 return nil 291 } 292 293 return &team 294 }