github.com/lologarithm/mattermost-server@v5.3.2-0.20181002060438-c82a84ed765b+incompatible/app/import_validators.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package app 5 6 import ( 7 "encoding/json" 8 "net/http" 9 "os" 10 "strings" 11 "unicode/utf8" 12 13 "github.com/mattermost/mattermost-server/model" 14 ) 15 16 func validateSchemeImportData(data *SchemeImportData) *model.AppError { 17 18 if data.Scope == nil { 19 return model.NewAppError("BulkImport", "app.import.validate_scheme_import_data.null_scope.error", nil, "", http.StatusBadRequest) 20 } 21 22 switch *data.Scope { 23 case model.SCHEME_SCOPE_TEAM: 24 if data.DefaultTeamAdminRole == nil || data.DefaultTeamUserRole == nil || data.DefaultChannelAdminRole == nil || data.DefaultChannelUserRole == nil { 25 return model.NewAppError("BulkImport", "app.import.validate_scheme_import_data.wrong_roles_for_scope.error", nil, "", http.StatusBadRequest) 26 } 27 case model.SCHEME_SCOPE_CHANNEL: 28 if data.DefaultTeamAdminRole != nil || data.DefaultTeamUserRole != nil || data.DefaultChannelAdminRole == nil || data.DefaultChannelUserRole == nil { 29 return model.NewAppError("BulkImport", "app.import.validate_scheme_import_data.wrong_roles_for_scope.error", nil, "", http.StatusBadRequest) 30 } 31 default: 32 return model.NewAppError("BulkImport", "app.import.validate_scheme_import_data.unknown_scheme.error", nil, "", http.StatusBadRequest) 33 } 34 35 if data.Name == nil || !model.IsValidSchemeName(*data.Name) { 36 return model.NewAppError("BulkImport", "app.import.validate_scheme_import_data.name_invalid.error", nil, "", http.StatusBadRequest) 37 } 38 39 if data.DisplayName == nil || len(*data.DisplayName) == 0 || len(*data.DisplayName) > model.SCHEME_DISPLAY_NAME_MAX_LENGTH { 40 return model.NewAppError("BulkImport", "app.import.validate_scheme_import_data.display_name_invalid.error", nil, "", http.StatusBadRequest) 41 } 42 43 if data.Description != nil && len(*data.Description) > model.SCHEME_DESCRIPTION_MAX_LENGTH { 44 return model.NewAppError("BulkImport", "app.import.validate_scheme_import_data.description_invalid.error", nil, "", http.StatusBadRequest) 45 } 46 47 if data.DefaultTeamAdminRole != nil { 48 if err := validateRoleImportData(data.DefaultTeamAdminRole); err != nil { 49 return err 50 } 51 } 52 53 if data.DefaultTeamUserRole != nil { 54 if err := validateRoleImportData(data.DefaultTeamUserRole); err != nil { 55 return err 56 } 57 } 58 59 if data.DefaultChannelAdminRole != nil { 60 if err := validateRoleImportData(data.DefaultChannelAdminRole); err != nil { 61 return err 62 } 63 } 64 65 if data.DefaultChannelUserRole != nil { 66 if err := validateRoleImportData(data.DefaultChannelUserRole); err != nil { 67 return err 68 } 69 } 70 71 return nil 72 } 73 74 func validateRoleImportData(data *RoleImportData) *model.AppError { 75 76 if data.Name == nil || !model.IsValidRoleName(*data.Name) { 77 return model.NewAppError("BulkImport", "app.import.validate_role_import_data.name_invalid.error", nil, "", http.StatusBadRequest) 78 } 79 80 if data.DisplayName == nil || len(*data.DisplayName) == 0 || len(*data.DisplayName) > model.ROLE_DISPLAY_NAME_MAX_LENGTH { 81 return model.NewAppError("BulkImport", "app.import.validate_role_import_data.display_name_invalid.error", nil, "", http.StatusBadRequest) 82 } 83 84 if data.Description != nil && len(*data.Description) > model.ROLE_DESCRIPTION_MAX_LENGTH { 85 return model.NewAppError("BulkImport", "app.import.validate_role_import_data.description_invalid.error", nil, "", http.StatusBadRequest) 86 } 87 88 if data.Permissions != nil { 89 for _, permission := range *data.Permissions { 90 permissionValidated := false 91 for _, p := range model.ALL_PERMISSIONS { 92 if permission == p.Id { 93 permissionValidated = true 94 break 95 } 96 } 97 98 if !permissionValidated { 99 return model.NewAppError("BulkImport", "app.import.validate_role_import_data.invalid_permission.error", nil, "permission"+permission, http.StatusBadRequest) 100 } 101 } 102 } 103 104 return nil 105 } 106 107 func validateTeamImportData(data *TeamImportData) *model.AppError { 108 109 if data.Name == nil { 110 return model.NewAppError("BulkImport", "app.import.validate_team_import_data.name_missing.error", nil, "", http.StatusBadRequest) 111 } else if len(*data.Name) > model.TEAM_NAME_MAX_LENGTH { 112 return model.NewAppError("BulkImport", "app.import.validate_team_import_data.name_length.error", nil, "", http.StatusBadRequest) 113 } else if model.IsReservedTeamName(*data.Name) { 114 return model.NewAppError("BulkImport", "app.import.validate_team_import_data.name_reserved.error", nil, "", http.StatusBadRequest) 115 } else if !model.IsValidTeamName(*data.Name) { 116 return model.NewAppError("BulkImport", "app.import.validate_team_import_data.name_characters.error", nil, "", http.StatusBadRequest) 117 } 118 119 if data.DisplayName == nil { 120 return model.NewAppError("BulkImport", "app.import.validate_team_import_data.display_name_missing.error", nil, "", http.StatusBadRequest) 121 } else if utf8.RuneCountInString(*data.DisplayName) == 0 || utf8.RuneCountInString(*data.DisplayName) > model.TEAM_DISPLAY_NAME_MAX_RUNES { 122 return model.NewAppError("BulkImport", "app.import.validate_team_import_data.display_name_length.error", nil, "", http.StatusBadRequest) 123 } 124 125 if data.Type == nil { 126 return model.NewAppError("BulkImport", "app.import.validate_team_import_data.type_missing.error", nil, "", http.StatusBadRequest) 127 } else if *data.Type != model.TEAM_OPEN && *data.Type != model.TEAM_INVITE { 128 return model.NewAppError("BulkImport", "app.import.validate_team_import_data.type_invalid.error", nil, "", http.StatusBadRequest) 129 } 130 131 if data.Description != nil && len(*data.Description) > model.TEAM_DESCRIPTION_MAX_LENGTH { 132 return model.NewAppError("BulkImport", "app.import.validate_team_import_data.description_length.error", nil, "", http.StatusBadRequest) 133 } 134 135 if data.Scheme != nil && !model.IsValidSchemeName(*data.Scheme) { 136 return model.NewAppError("BulkImport", "app.import.validate_team_import_data.scheme_invalid.error", nil, "", http.StatusBadRequest) 137 } 138 139 return nil 140 } 141 142 func validateChannelImportData(data *ChannelImportData) *model.AppError { 143 144 if data.Team == nil { 145 return model.NewAppError("BulkImport", "app.import.validate_channel_import_data.team_missing.error", nil, "", http.StatusBadRequest) 146 } 147 148 if data.Name == nil { 149 return model.NewAppError("BulkImport", "app.import.validate_channel_import_data.name_missing.error", nil, "", http.StatusBadRequest) 150 } else if len(*data.Name) > model.CHANNEL_NAME_MAX_LENGTH { 151 return model.NewAppError("BulkImport", "app.import.validate_channel_import_data.name_length.error", nil, "", http.StatusBadRequest) 152 } else if !model.IsValidChannelIdentifier(*data.Name) { 153 return model.NewAppError("BulkImport", "app.import.validate_channel_import_data.name_characters.error", nil, "", http.StatusBadRequest) 154 } 155 156 if data.DisplayName == nil { 157 return model.NewAppError("BulkImport", "app.import.validate_channel_import_data.display_name_missing.error", nil, "", http.StatusBadRequest) 158 } else if utf8.RuneCountInString(*data.DisplayName) == 0 || utf8.RuneCountInString(*data.DisplayName) > model.CHANNEL_DISPLAY_NAME_MAX_RUNES { 159 return model.NewAppError("BulkImport", "app.import.validate_channel_import_data.display_name_length.error", nil, "", http.StatusBadRequest) 160 } 161 162 if data.Type == nil { 163 return model.NewAppError("BulkImport", "app.import.validate_channel_import_data.type_missing.error", nil, "", http.StatusBadRequest) 164 } else if *data.Type != model.CHANNEL_OPEN && *data.Type != model.CHANNEL_PRIVATE { 165 return model.NewAppError("BulkImport", "app.import.validate_channel_import_data.type_invalid.error", nil, "", http.StatusBadRequest) 166 } 167 168 if data.Header != nil && utf8.RuneCountInString(*data.Header) > model.CHANNEL_HEADER_MAX_RUNES { 169 return model.NewAppError("BulkImport", "app.import.validate_channel_import_data.header_length.error", nil, "", http.StatusBadRequest) 170 } 171 172 if data.Purpose != nil && utf8.RuneCountInString(*data.Purpose) > model.CHANNEL_PURPOSE_MAX_RUNES { 173 return model.NewAppError("BulkImport", "app.import.validate_channel_import_data.purpose_length.error", nil, "", http.StatusBadRequest) 174 } 175 176 if data.Scheme != nil && !model.IsValidSchemeName(*data.Scheme) { 177 return model.NewAppError("BulkImport", "app.import.validate_channel_import_data.scheme_invalid.error", nil, "", http.StatusBadRequest) 178 } 179 180 return nil 181 } 182 183 func validateUserImportData(data *UserImportData) *model.AppError { 184 if data.ProfileImage != nil { 185 if _, err := os.Stat(*data.ProfileImage); os.IsNotExist(err) { 186 return model.NewAppError("BulkImport", "app.import.validate_user_import_data.profile_image.error", nil, "", http.StatusBadRequest) 187 } 188 } 189 190 if data.Username == nil { 191 return model.NewAppError("BulkImport", "app.import.validate_user_import_data.username_missing.error", nil, "", http.StatusBadRequest) 192 } else if !model.IsValidUsername(*data.Username) { 193 return model.NewAppError("BulkImport", "app.import.validate_user_import_data.username_invalid.error", nil, "", http.StatusBadRequest) 194 } 195 196 if data.Email == nil { 197 return model.NewAppError("BulkImport", "app.import.validate_user_import_data.email_missing.error", nil, "", http.StatusBadRequest) 198 } else if len(*data.Email) == 0 || len(*data.Email) > model.USER_EMAIL_MAX_LENGTH { 199 return model.NewAppError("BulkImport", "app.import.validate_user_import_data.email_length.error", nil, "", http.StatusBadRequest) 200 } 201 202 if data.AuthService != nil && len(*data.AuthService) == 0 { 203 return model.NewAppError("BulkImport", "app.import.validate_user_import_data.auth_service_length.error", nil, "", http.StatusBadRequest) 204 } 205 206 if data.AuthData != nil && data.Password != nil { 207 return model.NewAppError("BulkImport", "app.import.validate_user_import_data.auth_data_and_password.error", nil, "", http.StatusBadRequest) 208 } 209 210 if data.AuthData != nil && len(*data.AuthData) > model.USER_AUTH_DATA_MAX_LENGTH { 211 return model.NewAppError("BulkImport", "app.import.validate_user_import_data.auth_data_length.error", nil, "", http.StatusBadRequest) 212 } 213 214 if data.Password != nil && len(*data.Password) == 0 { 215 return model.NewAppError("BulkImport", "app.import.validate_user_import_data.password_length.error", nil, "", http.StatusBadRequest) 216 } 217 218 if data.Password != nil && len(*data.Password) > model.USER_PASSWORD_MAX_LENGTH { 219 return model.NewAppError("BulkImport", "app.import.validate_user_import_data.password_length.error", nil, "", http.StatusBadRequest) 220 } 221 222 if data.Nickname != nil && utf8.RuneCountInString(*data.Nickname) > model.USER_NICKNAME_MAX_RUNES { 223 return model.NewAppError("BulkImport", "app.import.validate_user_import_data.nickname_length.error", nil, "", http.StatusBadRequest) 224 } 225 226 if data.FirstName != nil && utf8.RuneCountInString(*data.FirstName) > model.USER_FIRST_NAME_MAX_RUNES { 227 return model.NewAppError("BulkImport", "app.import.validate_user_import_data.first_name_length.error", nil, "", http.StatusBadRequest) 228 } 229 230 if data.LastName != nil && utf8.RuneCountInString(*data.LastName) > model.USER_LAST_NAME_MAX_RUNES { 231 return model.NewAppError("BulkImport", "app.import.validate_user_import_data.last_name_length.error", nil, "", http.StatusBadRequest) 232 } 233 234 if data.Position != nil && utf8.RuneCountInString(*data.Position) > model.USER_POSITION_MAX_RUNES { 235 return model.NewAppError("BulkImport", "app.import.validate_user_import_data.position_length.error", nil, "", http.StatusBadRequest) 236 } 237 238 if data.Roles != nil && !model.IsValidUserRoles(*data.Roles) { 239 return model.NewAppError("BulkImport", "app.import.validate_user_import_data.roles_invalid.error", nil, "", http.StatusBadRequest) 240 } 241 242 if data.NotifyProps != nil { 243 if data.NotifyProps.Desktop != nil && !model.IsValidUserNotifyLevel(*data.NotifyProps.Desktop) { 244 return model.NewAppError("BulkImport", "app.import.validate_user_import_data.notify_props_desktop_invalid.error", nil, "", http.StatusBadRequest) 245 } 246 247 if data.NotifyProps.DesktopSound != nil && !model.IsValidTrueOrFalseString(*data.NotifyProps.DesktopSound) { 248 return model.NewAppError("BulkImport", "app.import.validate_user_import_data.notify_props_desktop_sound_invalid.error", nil, "", http.StatusBadRequest) 249 } 250 251 if data.NotifyProps.Email != nil && !model.IsValidTrueOrFalseString(*data.NotifyProps.Email) { 252 return model.NewAppError("BulkImport", "app.import.validate_user_import_data.notify_props_email_invalid.error", nil, "", http.StatusBadRequest) 253 } 254 255 if data.NotifyProps.Mobile != nil && !model.IsValidUserNotifyLevel(*data.NotifyProps.Mobile) { 256 return model.NewAppError("BulkImport", "app.import.validate_user_import_data.notify_props_mobile_invalid.error", nil, "", http.StatusBadRequest) 257 } 258 259 if data.NotifyProps.MobilePushStatus != nil && !model.IsValidPushStatusNotifyLevel(*data.NotifyProps.MobilePushStatus) { 260 return model.NewAppError("BulkImport", "app.import.validate_user_import_data.notify_props_mobile_push_status_invalid.error", nil, "", http.StatusBadRequest) 261 } 262 263 if data.NotifyProps.ChannelTrigger != nil && !model.IsValidTrueOrFalseString(*data.NotifyProps.ChannelTrigger) { 264 return model.NewAppError("BulkImport", "app.import.validate_user_import_data.notify_props_channel_trigger_invalid.error", nil, "", http.StatusBadRequest) 265 } 266 267 if data.NotifyProps.CommentsTrigger != nil && !model.IsValidCommentsNotifyLevel(*data.NotifyProps.CommentsTrigger) { 268 return model.NewAppError("BulkImport", "app.import.validate_user_import_data.notify_props_comments_trigger_invalid.error", nil, "", http.StatusBadRequest) 269 } 270 } 271 272 if data.UseMarkdownPreview != nil && !model.IsValidTrueOrFalseString(*data.UseMarkdownPreview) { 273 return model.NewAppError("BulkImport", "app.import.validate_user_import_data.advanced_props_feature_markdown_preview.error", nil, "", http.StatusBadRequest) 274 } 275 276 if data.UseFormatting != nil && !model.IsValidTrueOrFalseString(*data.UseFormatting) { 277 return model.NewAppError("BulkImport", "app.import.validate_user_import_data.advanced_props_formatting.error", nil, "", http.StatusBadRequest) 278 } 279 280 if data.ShowUnreadSection != nil && !model.IsValidTrueOrFalseString(*data.ShowUnreadSection) { 281 return model.NewAppError("BulkImport", "app.import.validate_user_import_data.advanced_props_show_unread_section.error", nil, "", http.StatusBadRequest) 282 } 283 284 if data.Teams != nil { 285 return validateUserTeamsImportData(data.Teams) 286 } 287 288 return nil 289 } 290 291 func validateUserTeamsImportData(data *[]UserTeamImportData) *model.AppError { 292 if data == nil { 293 return nil 294 } 295 296 for _, tdata := range *data { 297 if tdata.Name == nil { 298 return model.NewAppError("BulkImport", "app.import.validate_user_teams_import_data.team_name_missing.error", nil, "", http.StatusBadRequest) 299 } 300 301 if tdata.Roles != nil && !model.IsValidUserRoles(*tdata.Roles) { 302 return model.NewAppError("BulkImport", "app.import.validate_user_teams_import_data.invalid_roles.error", nil, "", http.StatusBadRequest) 303 } 304 305 if tdata.Channels != nil { 306 if err := validateUserChannelsImportData(tdata.Channels); err != nil { 307 return err 308 } 309 } 310 311 if tdata.Theme != nil && 0 < len(strings.Trim(*tdata.Theme, " \t\r")) { 312 var unused map[string]string 313 if err := json.NewDecoder(strings.NewReader(*tdata.Theme)).Decode(&unused); err != nil { 314 return model.NewAppError("BulkImport", "app.import.validate_user_teams_import_data.invalid_team_theme.error", nil, err.Error(), http.StatusBadRequest) 315 } 316 } 317 } 318 319 return nil 320 } 321 322 func validateUserChannelsImportData(data *[]UserChannelImportData) *model.AppError { 323 if data == nil { 324 return nil 325 } 326 327 for _, cdata := range *data { 328 if cdata.Name == nil { 329 return model.NewAppError("BulkImport", "app.import.validate_user_channels_import_data.channel_name_missing.error", nil, "", http.StatusBadRequest) 330 } 331 332 if cdata.Roles != nil && !model.IsValidUserRoles(*cdata.Roles) { 333 return model.NewAppError("BulkImport", "app.import.validate_user_channels_import_data.invalid_roles.error", nil, "", http.StatusBadRequest) 334 } 335 336 if cdata.NotifyProps != nil { 337 if cdata.NotifyProps.Desktop != nil && !model.IsChannelNotifyLevelValid(*cdata.NotifyProps.Desktop) { 338 return model.NewAppError("BulkImport", "app.import.validate_user_channels_import_data.invalid_notify_props_desktop.error", nil, "", http.StatusBadRequest) 339 } 340 341 if cdata.NotifyProps.Mobile != nil && !model.IsChannelNotifyLevelValid(*cdata.NotifyProps.Mobile) { 342 return model.NewAppError("BulkImport", "app.import.validate_user_channels_import_data.invalid_notify_props_mobile.error", nil, "", http.StatusBadRequest) 343 } 344 345 if cdata.NotifyProps.MarkUnread != nil && !model.IsChannelMarkUnreadLevelValid(*cdata.NotifyProps.MarkUnread) { 346 return model.NewAppError("BulkImport", "app.import.validate_user_channels_import_data.invalid_notify_props_mark_unread.error", nil, "", http.StatusBadRequest) 347 } 348 } 349 } 350 351 return nil 352 } 353 354 func validateReactionImportData(data *ReactionImportData, parentCreateAt int64) *model.AppError { 355 if data.User == nil { 356 return model.NewAppError("BulkImport", "app.import.validate_reaction_import_data.user_missing.error", nil, "", http.StatusBadRequest) 357 } 358 359 if data.EmojiName == nil { 360 return model.NewAppError("BulkImport", "app.import.validate_reaction_import_data.emoji_name_missing.error", nil, "", http.StatusBadRequest) 361 } else if utf8.RuneCountInString(*data.EmojiName) > model.EMOJI_NAME_MAX_LENGTH { 362 return model.NewAppError("BulkImport", "app.import.validate_reaction_import_data.emoji_name_length.error", nil, "", http.StatusBadRequest) 363 } 364 365 if data.CreateAt == nil { 366 return model.NewAppError("BulkImport", "app.import.validate_reaction_import_data.create_at_missing.error", nil, "", http.StatusBadRequest) 367 } else if *data.CreateAt == 0 { 368 return model.NewAppError("BulkImport", "app.import.validate_reaction_import_data.create_at_zero.error", nil, "", http.StatusBadRequest) 369 } else if *data.CreateAt < parentCreateAt { 370 return model.NewAppError("BulkImport", "app.import.validate_reaction_import_data.create_at_before_parent.error", nil, "", http.StatusBadRequest) 371 } 372 373 return nil 374 } 375 376 func validateReplyImportData(data *ReplyImportData, parentCreateAt int64, maxPostSize int) *model.AppError { 377 if data.User == nil { 378 return model.NewAppError("BulkImport", "app.import.validate_reply_import_data.user_missing.error", nil, "", http.StatusBadRequest) 379 } 380 381 if data.Message == nil { 382 return model.NewAppError("BulkImport", "app.import.validate_reply_import_data.message_missing.error", nil, "", http.StatusBadRequest) 383 } else if utf8.RuneCountInString(*data.Message) > maxPostSize { 384 return model.NewAppError("BulkImport", "app.import.validate_reply_import_data.message_length.error", nil, "", http.StatusBadRequest) 385 } 386 387 if data.CreateAt == nil { 388 return model.NewAppError("BulkImport", "app.import.validate_reply_import_data.create_at_missing.error", nil, "", http.StatusBadRequest) 389 } else if *data.CreateAt == 0 { 390 return model.NewAppError("BulkImport", "app.import.validate_reply_import_data.create_at_zero.error", nil, "", http.StatusBadRequest) 391 } else if *data.CreateAt < parentCreateAt { 392 return model.NewAppError("BulkImport", "app.import.validate_reply_import_data.create_at_before_parent.error", nil, "", http.StatusBadRequest) 393 } 394 395 return nil 396 } 397 398 func validatePostImportData(data *PostImportData, maxPostSize int) *model.AppError { 399 if data.Team == nil { 400 return model.NewAppError("BulkImport", "app.import.validate_post_import_data.team_missing.error", nil, "", http.StatusBadRequest) 401 } 402 403 if data.Channel == nil { 404 return model.NewAppError("BulkImport", "app.import.validate_post_import_data.channel_missing.error", nil, "", http.StatusBadRequest) 405 } 406 407 if data.User == nil { 408 return model.NewAppError("BulkImport", "app.import.validate_post_import_data.user_missing.error", nil, "", http.StatusBadRequest) 409 } 410 411 if data.Message == nil { 412 return model.NewAppError("BulkImport", "app.import.validate_post_import_data.message_missing.error", nil, "", http.StatusBadRequest) 413 } else if utf8.RuneCountInString(*data.Message) > maxPostSize { 414 return model.NewAppError("BulkImport", "app.import.validate_post_import_data.message_length.error", nil, "", http.StatusBadRequest) 415 } 416 417 if data.CreateAt == nil { 418 return model.NewAppError("BulkImport", "app.import.validate_post_import_data.create_at_missing.error", nil, "", http.StatusBadRequest) 419 } else if *data.CreateAt == 0 { 420 return model.NewAppError("BulkImport", "app.import.validate_post_import_data.create_at_zero.error", nil, "", http.StatusBadRequest) 421 } 422 423 if data.Reactions != nil { 424 for _, reaction := range *data.Reactions { 425 validateReactionImportData(&reaction, *data.CreateAt) 426 } 427 } 428 429 if data.Replies != nil { 430 for _, reply := range *data.Replies { 431 validateReplyImportData(&reply, *data.CreateAt, maxPostSize) 432 } 433 } 434 435 return nil 436 } 437 438 func validateDirectChannelImportData(data *DirectChannelImportData) *model.AppError { 439 if data.Members == nil { 440 return model.NewAppError("BulkImport", "app.import.validate_direct_channel_import_data.members_required.error", nil, "", http.StatusBadRequest) 441 } 442 443 if len(*data.Members) != 2 { 444 if len(*data.Members) < model.CHANNEL_GROUP_MIN_USERS { 445 return model.NewAppError("BulkImport", "app.import.validate_direct_channel_import_data.members_too_few.error", nil, "", http.StatusBadRequest) 446 } else if len(*data.Members) > model.CHANNEL_GROUP_MAX_USERS { 447 return model.NewAppError("BulkImport", "app.import.validate_direct_channel_import_data.members_too_many.error", nil, "", http.StatusBadRequest) 448 } 449 } 450 451 if data.Header != nil && utf8.RuneCountInString(*data.Header) > model.CHANNEL_HEADER_MAX_RUNES { 452 return model.NewAppError("BulkImport", "app.import.validate_direct_channel_import_data.header_length.error", nil, "", http.StatusBadRequest) 453 } 454 455 if data.FavoritedBy != nil { 456 for _, favoriter := range *data.FavoritedBy { 457 found := false 458 for _, member := range *data.Members { 459 if favoriter == member { 460 found = true 461 break 462 } 463 } 464 if !found { 465 return model.NewAppError("BulkImport", "app.import.validate_direct_channel_import_data.unknown_favoriter.error", map[string]interface{}{"Username": favoriter}, "", http.StatusBadRequest) 466 } 467 } 468 } 469 470 return nil 471 } 472 473 func validateDirectPostImportData(data *DirectPostImportData, maxPostSize int) *model.AppError { 474 if data.ChannelMembers == nil { 475 return model.NewAppError("BulkImport", "app.import.validate_direct_post_import_data.channel_members_required.error", nil, "", http.StatusBadRequest) 476 } 477 478 if len(*data.ChannelMembers) != 2 { 479 if len(*data.ChannelMembers) < model.CHANNEL_GROUP_MIN_USERS { 480 return model.NewAppError("BulkImport", "app.import.validate_direct_post_import_data.channel_members_too_few.error", nil, "", http.StatusBadRequest) 481 } else if len(*data.ChannelMembers) > model.CHANNEL_GROUP_MAX_USERS { 482 return model.NewAppError("BulkImport", "app.import.validate_direct_post_import_data.channel_members_too_many.error", nil, "", http.StatusBadRequest) 483 } 484 } 485 486 if data.User == nil { 487 return model.NewAppError("BulkImport", "app.import.validate_direct_post_import_data.user_missing.error", nil, "", http.StatusBadRequest) 488 } 489 490 if data.Message == nil { 491 return model.NewAppError("BulkImport", "app.import.validate_direct_post_import_data.message_missing.error", nil, "", http.StatusBadRequest) 492 } else if utf8.RuneCountInString(*data.Message) > maxPostSize { 493 return model.NewAppError("BulkImport", "app.import.validate_direct_post_import_data.message_length.error", nil, "", http.StatusBadRequest) 494 } 495 496 if data.CreateAt == nil { 497 return model.NewAppError("BulkImport", "app.import.validate_direct_post_import_data.create_at_missing.error", nil, "", http.StatusBadRequest) 498 } else if *data.CreateAt == 0 { 499 return model.NewAppError("BulkImport", "app.import.validate_direct_post_import_data.create_at_zero.error", nil, "", http.StatusBadRequest) 500 } 501 502 if data.FlaggedBy != nil { 503 for _, flagger := range *data.FlaggedBy { 504 found := false 505 for _, member := range *data.ChannelMembers { 506 if flagger == member { 507 found = true 508 break 509 } 510 } 511 if !found { 512 return model.NewAppError("BulkImport", "app.import.validate_direct_post_import_data.unknown_flagger.error", map[string]interface{}{"Username": flagger}, "", http.StatusBadRequest) 513 } 514 } 515 } 516 517 if data.Reactions != nil { 518 for _, reaction := range *data.Reactions { 519 validateReactionImportData(&reaction, *data.CreateAt) 520 } 521 } 522 523 if data.Replies != nil { 524 for _, reply := range *data.Replies { 525 validateReplyImportData(&reply, *data.CreateAt, maxPostSize) 526 } 527 } 528 529 return nil 530 } 531 532 func validateEmojiImportData(data *EmojiImportData) *model.AppError { 533 if data == nil { 534 return model.NewAppError("BulkImport", "app.import.validate_emoji_import_data.empty.error", nil, "", http.StatusBadRequest) 535 } 536 537 if data.Name == nil || len(*data.Name) == 0 { 538 return model.NewAppError("BulkImport", "app.import.validate_emoji_import_data.name_missing.error", nil, "", http.StatusBadRequest) 539 } 540 541 if err := model.IsValidEmojiName(*data.Name); err != nil { 542 return err 543 } 544 545 if data.Image == nil || len(*data.Image) == 0 { 546 return model.NewAppError("BulkImport", "app.import.validate_emoji_import_data.image_missing.error", nil, "", http.StatusBadRequest) 547 } 548 549 return nil 550 }