/// <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); }
/// <summary> /// Add new tags to a topic if they are allowed, ignore existing ones, remove ones that have been deleted /// </summary> /// <param name="tags"></param> /// <param name="topic"></param> /// <param name="isAllowedToAddTags"></param> public void Add(string[] tags, Topic topic, bool isAllowedToAddTags) { if (topic.Tags == null) { topic.Tags = new List <TopicTag>(); } foreach (var newTag in tags) { var tagExists = false; // Check it's not already in the list foreach (var topicTag in topic.Tags) { if (topicTag.Tag == newTag) { tagExists = true; break; } } // Continue if tag doesn't already exist on topic if (!tagExists) { var tag = GetTagName(newTag); if (tag != null) { // Exists topic.Tags.Add(tag); } else if (isAllowedToAddTags) { // Doesn't exists var nTag = new TopicTag { Tag = newTag, Slug = ServiceHelpers.CreateUrl(newTag) }; //Add(nTag); topic.Tags.Add(nTag); } } } // Find tags that don't exist now var tagsRemoved = new List <TopicTag>(); foreach (var topicTag in topic.Tags) { var hasTag = false; // Finally check for removed tags foreach (var tag in tags) { if (topicTag.Tag == tag) { hasTag = true; } } if (hasTag == false) { tagsRemoved.Add(topicTag); } } // Now remove from Topic foreach (var topicTag in tagsRemoved) { topic.Tags.Remove(topicTag); } }
/// <summary> /// Create new user /// </summary> /// <param name="newUser"></param> /// <param name="loginType"></param> /// <returns></returns> public async Task <IPipelineProcess <MembershipUser> > CreateUser(MembershipUser newUser, LoginType loginType) { // Get the site settings var settings = _settingsService.GetSettings(false); // Santise the user fields newUser = SanitizeUser(newUser); // Hash the password var salt = StringUtils.CreateSalt(Constants.SaltSize); var hash = StringUtils.GenerateSaltedHash(newUser.Password, salt); newUser.Password = hash; newUser.PasswordSalt = salt; // Add the roles newUser.Roles = new List <MembershipRole> { settings.NewMemberStartingRole }; // Set dates newUser.CreateDate = newUser.LastPasswordChangedDate = DateTime.UtcNow; newUser.LastLockoutDate = (DateTime)SqlDateTime.MinValue; newUser.LastLoginDate = DateTime.UtcNow; newUser.IsLockedOut = false; newUser.Slug = ServiceHelpers.GenerateSlug(newUser.UserName, GetUserBySlugLike(ServiceHelpers.CreateUrl(newUser.UserName)), null); // Get the pipelines var userCreatePipes = ForumConfiguration.Instance.PipelinesUserCreate; // The model to process var piplineModel = new PipelineProcess <MembershipUser>(newUser); // Add the login type to piplineModel.ExtendedData.Add(Constants.ExtendedDataKeys.LoginType, JsonConvert.SerializeObject(loginType)); // Get instance of the pipeline to use var createUserPipeline = new Pipeline <IPipelineProcess <MembershipUser>, MembershipUser>(_context); // Register the pipes var allMembershipUserPipes = ImplementationManager.GetInstances <IPipe <IPipelineProcess <MembershipUser> > >(); // Loop through the pipes and add the ones we want foreach (var pipe in userCreatePipes) { if (allMembershipUserPipes.ContainsKey(pipe)) { createUserPipeline.Register(allMembershipUserPipes[pipe]); } } // Process the pipeline return(await createUserPipeline.Process(piplineModel)); }