/// <summary> /// Extract users from CSV format and import them /// </summary> /// <returns></returns> public CsvReport FromCsv(List <string> allLines) { var usersProcessed = new List <string>(); var commaSeparator = new[] { ',' }; var report = new CsvReport(); if (allLines == null || allLines.Count == 0) { report.Errors.Add(new CsvErrorWarning { ErrorWarningType = CsvErrorWarningType.BadDataFormat, Message = "No users found." }); return(report); } var settings = _settingsService.GetSettings(true); var lineCounter = 0; foreach (var line in allLines) { try { lineCounter++; // Each line is made up of n items in a predefined order var values = line.Split(commaSeparator); if (values.Length < 2) { report.Errors.Add(new CsvErrorWarning { ErrorWarningType = CsvErrorWarningType.MissingKeyOrValue, Message = $"Line {lineCounter}: insufficient values supplied." }); continue; } var userName = values[0]; if (userName.IsNullEmpty()) { report.Errors.Add(new CsvErrorWarning { ErrorWarningType = CsvErrorWarningType.MissingKeyOrValue, Message = $"Line {lineCounter}: no username supplied." }); continue; } var email = values[1]; if (email.IsNullEmpty()) { report.Errors.Add(new CsvErrorWarning { ErrorWarningType = CsvErrorWarningType.MissingKeyOrValue, Message = $"Line {lineCounter}: no email supplied." }); continue; } // get the user var userToImport = GetUser(userName); if (userToImport != null) { report.Errors.Add(new CsvErrorWarning { ErrorWarningType = CsvErrorWarningType.AlreadyExists, Message = $"Line {lineCounter}: user already exists in forum." }); continue; } if (usersProcessed.Contains(userName)) { report.Errors.Add(new CsvErrorWarning { ErrorWarningType = CsvErrorWarningType.AlreadyExists, Message = $"Line {lineCounter}: user already exists in import file." }); continue; } usersProcessed.Add(userName); userToImport = CreateEmptyUser(); userToImport.UserName = userName; userToImport.Slug = ServiceHelpers.GenerateSlug(userToImport.UserName, GetUserBySlugLike(ServiceHelpers.CreateUrl(userToImport.UserName)).Select(x => x.Slug).ToList(), userToImport.Slug); userToImport.Email = email; userToImport.IsApproved = true; userToImport.PasswordSalt = StringUtils.CreateSalt(Constants.SaltSize); string createDateStr = null; if (values.Length >= 3) { createDateStr = values[2]; } userToImport.CreateDate = createDateStr.IsNullEmpty() ? DateTime.UtcNow : DateTime.Parse(createDateStr); if (values.Length >= 4) { userToImport.Age = int.Parse(values[3]); } if (values.Length >= 5) { userToImport.Location = values[4]; } if (values.Length >= 6) { userToImport.Website = values[5]; } if (values.Length >= 7) { userToImport.Facebook = values[6]; } if (values.Length >= 8) { userToImport.Signature = values[7]; } userToImport.Roles = new List <MembershipRole> { settings.NewMemberStartingRole }; Add(userToImport); } catch (Exception ex) { report.Errors.Add(new CsvErrorWarning { ErrorWarningType = CsvErrorWarningType.GeneralError, Message = ex.Message }); } } return(report); }