/// <summary> /// Deletes the specified item. /// </summary> /// <param name="item">The item.</param> /// <returns></returns> public override bool Delete( FinancialPersonSavedAccount item ) { if ( item.FinancialPaymentDetailId.HasValue ) { var paymentDetailsService = new FinancialPaymentDetailService( (Rock.Data.RockContext)this.Context ); var paymentDetail = paymentDetailsService.Get( item.FinancialPaymentDetailId.Value ); if ( paymentDetail != null ) { paymentDetailsService.Delete( paymentDetail ); } } return base.Delete( item ); }
public static void ClassInitialize(TestContext testContext) { _rockContext = new RockContext(); _accountService = new FinancialAccountService(_rockContext); _batchService = new FinancialBatchService(_rockContext); _transactionService = new FinancialTransactionService(_rockContext); _paymentService = new FinancialPaymentDetailService(_rockContext); _achievementTypeService = new AchievementTypeService(_rockContext); _definedValueService = new DefinedValueService(_rockContext); _definedTypeService = new DefinedTypeService(_rockContext); _transactionDetailService = new FinancialTransactionDetailService(_rockContext); DeleteTestData(); PopulateDefinedValues(); CreateFinancialTransactionData(); CreateAchievementTypeData(); }
/// <summary> /// Cleanups the financial transaction null currency. /// </summary> /// <param name="dataMap">The data map.</param> /// <returns></returns> private int CleanupFinancialTransactionNullCurrency(JobDataMap dataMap) { int totalRowsUpdated = 0; var rockContext = new Rock.Data.RockContext(); int?currencyTypeUnknownId = DefinedValueCache.Get(Rock.SystemGuid.DefinedValue.CURRENCY_TYPE_UNKNOWN.AsGuid())?.Id; if (currencyTypeUnknownId.HasValue) { var financialPaymentDetailsToUpdate = new FinancialPaymentDetailService(rockContext).Queryable().Where(a => a.CurrencyTypeValueId == null); totalRowsUpdated = rockContext.BulkUpdate(financialPaymentDetailsToUpdate, a => new FinancialPaymentDetail { CurrencyTypeValueId = currencyTypeUnknownId.Value }); } return(totalRowsUpdated); }
void IJob.Execute(IJobExecutionContext context) { // Get the current password string currentEncryptionPassword = RockCrypto.GetConfiguredEncryptionPassword(); byte[] currentEncryptionKey = RockCrypto.GetDerivedKey(currentEncryptionPassword); // Generate a new password string newEncryptionPassword = RockCrypto.GenerateRandomBase64String(128); byte[] newEncryptionKey = RockCrypto.GetDerivedKey(newEncryptionPassword); context.UpdateLastStatusMessage("Migrating Attribute DefaultValues"); // Re-encrypt all Attribute DefaultValues using (RockContext rockContext = new RockContext()) { // Make a new Attribute service AttributeService attributeService = new AttributeService(rockContext); // Get all attributes that have an encrypted DefaultValue // Normally we just use the field type to define what's encrypted or not, but System // Settings don't have a field type so we have to do a bit of extra searching. IEnumerable <Attribute> attributes = attributeService.Queryable().Where(a => _EncryptedAttributeFieldTypes.Contains(a.FieldType.Class) || a.DefaultValue.StartsWith("EAAAA") ); // Go through each attribute and try re-encrypting it. foreach (Attribute attribute in attributes) { if (!string.IsNullOrEmpty(attribute.DefaultValue)) { string decryptedValue = TryDecrypt(attribute.DefaultValue, currentEncryptionKey); if (!string.IsNullOrEmpty(decryptedValue)) { attribute.DefaultValue = RockCrypto.EncryptString(decryptedValue, newEncryptionKey); } } } // Save the attributes rockContext.SaveChanges(true); } context.UpdateLastStatusMessage("Migrating AttributeValue Values"); // Re-encrypt all AttributeValue Values using (RockContext rockContext = new RockContext()) { // Make a new Attribute Value service AttributeValueService attributeValueService = new AttributeValueService(rockContext); // Get all attribute valuess that have an encrypted DefaultValue // Normally we just use the field type to define what's encrypted or not, but System // Settings don't have a field type so we have to do a bit of extra searching. IEnumerable <AttributeValue> attributeValues = attributeValueService.Queryable() .Where(a => a.Value != null && a.Value != "") .Where(a => _EncryptedAttributeFieldTypes.Contains(a.Attribute.FieldType.Class) || a.Value.StartsWith("EAAAA")); // Track numbers since this will take a while int totalCount = attributeValues.Count(); int currentCount = 1; // Go through each attribute value and try re-encrypting it. foreach (AttributeValue attributeValue in attributeValues) { if (!string.IsNullOrEmpty(attributeValue.Value)) { string decryptedValue = TryDecrypt(attributeValue.Value, currentEncryptionKey); if (!string.IsNullOrEmpty(decryptedValue)) { attributeValue.Value = RockCrypto.EncryptString(decryptedValue, newEncryptionKey); } } // Update the status and save every 250 attribute values currentCount++; if (currentCount % 250 == 0) { context.UpdateLastStatusMessage($@"Migrating AttributeValue {currentCount} of {totalCount}"); } } context.UpdateLastStatusMessage($@"Saving AttributeValues"); // Save the attribute values rockContext.SaveChanges(true); } context.UpdateLastStatusMessage("Migrating encrypted FinancialPaymentDetail fields"); // Re-encrypt all encrypted FinancialPaymentDetail fields using (RockContext rockContext = new RockContext()) { // Make a new Financial Payment Detail service FinancialPaymentDetailService financialPaymentDetailService = new FinancialPaymentDetailService(rockContext); IEnumerable <FinancialPaymentDetail> financialPaymentDetails = financialPaymentDetailService.Queryable().Where(fpd => (fpd.ExpirationMonthEncrypted != null && fpd.ExpirationMonthEncrypted != "") || (fpd.ExpirationYearEncrypted != null && fpd.ExpirationYearEncrypted != "") || (fpd.NameOnCardEncrypted != null && fpd.NameOnCardEncrypted != "") ); // Track numbers since this will take a while int totalCount = financialPaymentDetails.Count(); int currentCount = 1; // Go through each financial payment detail and try re-encrypting it. foreach (FinancialPaymentDetail financialPaymentDetail in financialPaymentDetails) { // Check Expiration Month if (!string.IsNullOrEmpty(financialPaymentDetail.ExpirationMonthEncrypted)) { string decryptedValue = TryDecrypt(financialPaymentDetail.ExpirationMonthEncrypted, currentEncryptionKey); if (!string.IsNullOrEmpty(decryptedValue)) { financialPaymentDetail.ExpirationMonthEncrypted = RockCrypto.EncryptString(decryptedValue, newEncryptionKey); } } // Check Expiration Year if (!string.IsNullOrEmpty(financialPaymentDetail.ExpirationYearEncrypted)) { string decryptedValue = TryDecrypt(financialPaymentDetail.ExpirationYearEncrypted, currentEncryptionKey); if (!string.IsNullOrEmpty(decryptedValue)) { financialPaymentDetail.ExpirationYearEncrypted = RockCrypto.EncryptString(decryptedValue, newEncryptionKey); } } // Check Name On Card if (!string.IsNullOrEmpty(financialPaymentDetail.NameOnCardEncrypted)) { string decryptedValue = TryDecrypt(financialPaymentDetail.NameOnCardEncrypted, currentEncryptionKey); if (!string.IsNullOrEmpty(decryptedValue)) { financialPaymentDetail.NameOnCardEncrypted = RockCrypto.EncryptString(decryptedValue, newEncryptionKey); } } // Update the status and save every 250 financial payment detail currentCount++; if (currentCount % 250 == 0) { context.UpdateLastStatusMessage($@"Migrating FinancialPaymentDetail {currentCount} of {totalCount}"); } } context.UpdateLastStatusMessage($@"Saving FinancialPaymentDetails"); // Save the financial payment details rockContext.SaveChanges(true); } context.UpdateLastStatusMessage("Migrating encrypted FinancialTransaction fields"); // Re-encrypt all encrypted FinancialTransaction fields using (RockContext rockContext = new RockContext()) { // Make a new Financial Transaction service FinancialTransactionService financialTransactionService = new FinancialTransactionService(rockContext); IEnumerable <FinancialTransaction> financialTransactions = financialTransactionService.Queryable().Where(ft => (ft.CheckMicrEncrypted != null && ft.CheckMicrEncrypted != "") || (ft.CheckMicrParts != null && ft.CheckMicrParts != "") ); // Track numbers since this will take a while int totalCount = financialTransactions.Count(); int currentCount = 1; // Go through each financial payment detail and try re-encrypting it. foreach (FinancialTransaction financialTransaction in financialTransactions) { // Check the Check Micr if (!string.IsNullOrEmpty(financialTransaction.CheckMicrEncrypted)) { string decryptedValue = TryDecrypt(financialTransaction.CheckMicrEncrypted, currentEncryptionKey); if (!string.IsNullOrEmpty(decryptedValue)) { financialTransaction.CheckMicrEncrypted = RockCrypto.EncryptString(decryptedValue, newEncryptionKey); } } // Check the Check Micr Parts if (!string.IsNullOrEmpty(financialTransaction.CheckMicrParts)) { string decryptedValue = TryDecrypt(financialTransaction.CheckMicrParts, currentEncryptionKey); if (!string.IsNullOrEmpty(decryptedValue)) { financialTransaction.CheckMicrParts = RockCrypto.EncryptString(decryptedValue, newEncryptionKey); } } // Update the status and save every 250 financial payment detail currentCount++; if (currentCount % 250 == 0) { context.UpdateLastStatusMessage($@"Migrating FinancialTransaction {currentCount} of {totalCount}"); } } context.UpdateLastStatusMessage($@"Saving FinancialTransactions"); // Save the financial transactions rockContext.SaveChanges(true); } context.UpdateLastStatusMessage("Done"); // Delete the job so it doesn't run again using (RockContext rockContext = new RockContext()) { // Make a new Job service ServiceJobService jobService = new ServiceJobService(rockContext); // Get the current job var job = jobService.Get(context.GetJobId()); // Delete it if (job != null) { jobService.Delete(job); rockContext.SaveChanges(true); } } // Save the new password // This will cause Rock to restart, so it has to be last RockCrypto.SetConfiguredEncryptionPassword(newEncryptionPassword); }
/// <summary> /// Executes the specified context. /// </summary> /// <param name="context">The context.</param> public void Execute(IJobExecutionContext context) { JobDataMap dataMap = context.JobDetail.JobDataMap; // Get the configured timeout, or default to 60 minutes if it is blank var commandTimeout = dataMap.GetString(AttributeKey.CommandTimeout).AsIntegerOrNull() ?? 3600; // Get a list of all FinancialPaymentDetails that need to have the Encrypted fields decrypted into the plain text fields #pragma warning disable 612, 618 var financialPaymentDetailIdsToUpdate = new FinancialPaymentDetailService(new RockContext()) .Queryable() .Where(pd => (pd.ExpirationMonth == null && pd.ExpirationMonthEncrypted != null) || (pd.NameOnCardEncrypted != null && pd.NameOnCard == null)) .OrderByDescending(a => a.Id) .Select(a => a.Id) .ToList(); #pragma warning restore 612, 618 var runtime = System.Diagnostics.Stopwatch.StartNew(); var lastProgressUpdate = DateTime.MinValue; double recordsProcessed = 0; var totalRecords = financialPaymentDetailIdsToUpdate.Count(); // Load the FinancialPayemntDetail record for each of the financialPaymentDetailIdsToUpdate // and convert the encrypted fields to plain text field. foreach (var financialPaymentDetailId in financialPaymentDetailIdsToUpdate) { using (var rockContext = new RockContext()) { var financialPaymentDetail = new FinancialPaymentDetailService(rockContext).Get(financialPaymentDetailId); if (financialPaymentDetail != null) { // Tell EF that the whole FinancialPaymentDetail record has been modified. // This will ensure that all the logic for setting the field data // is processed, and that all the appropriate PostSaveChanges, etc is done. rockContext.Entry(financialPaymentDetail).State = EntityState.Modified; rockContext.SaveChanges(); } var processTime = runtime.ElapsedMilliseconds; recordsProcessed++; var recordsPerMillisecond = recordsProcessed / processTime; var recordsRemaining = totalRecords - recordsProcessed; var minutesRemaining = recordsRemaining / recordsPerMillisecond / 1000 / 60; if (RockDateTime.Now - lastProgressUpdate > TimeSpan.FromSeconds(10)) { // Update the status every 10 seconds so that the progress can be shown. context.UpdateLastStatusMessage($"Processing {recordsProcessed} of {totalRecords} records. Approximately {minutesRemaining:N0} minutes remaining."); lastProgressUpdate = RockDateTime.Now; } } } context.UpdateLastStatusMessage($"Processed {recordsProcessed} of {totalRecords} records. "); // Now that all the rows that need to have been decrypted have been processed, the job can be deleted. ServiceJobService.DeleteJob(context.GetJobId()); }