Skip to content

Instantly share code, notes, and snippets.

@BillCacy
Last active July 31, 2020 06:15
Show Gist options
  • Save BillCacy/6ba14de86a027ff729a62e2c5520ddf8 to your computer and use it in GitHub Desktop.
Save BillCacy/6ba14de86a027ff729a62e2c5520ddf8 to your computer and use it in GitHub Desktop.
Sitecore 9 Forms Encrypted Field Save Action
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using Sample.Foundation.Core.Helpers;
using Sitecore.Diagnostics;
using Sitecore.ExperienceForms.Data;
using Sitecore.ExperienceForms.Data.Entities;
using Sitecore.ExperienceForms.Models;
using Sitecore.ExperienceForms.Processing;
using Sitecore.ExperienceForms.Processing.Actions;
namespace Sample.Feature.Forms.Actions
{
public class SaveDataWithEncryption : SaveData
{
private IFormDataProvider _dataProvider;
public SaveDataWithEncryption(ISubmitActionData submitActionData) : base(submitActionData)
{
}
internal SaveDataWithEncryption(ISubmitActionData submitActionData, IFormDataProvider dataProvider)
: this(submitActionData)
{
Assert.ArgumentNotNull((object)dataProvider, nameof(dataProvider));
_dataProvider = dataProvider;
}
protected override bool TryParse(string value, out string target)
{
target = string.Empty;
return true;
}
protected override bool Execute(string data, FormSubmitContext formSubmitContext)
{
Assert.ArgumentNotNull((object)formSubmitContext, nameof(formSubmitContext));
return SavePostedData(formSubmitContext.FormId, formSubmitContext.SessionId, formSubmitContext.Fields);
}
protected override bool SavePostedData(
Guid formId,
Guid sessionId,
IList<IViewModel> postedFields)
{
try
{
FormEntry formEntry = new FormEntry()
{
Created = DateTime.UtcNow,
FormItemId = formId,
FormEntryId = sessionId,
Fields = (ICollection<FieldData>)new List<FieldData>()
};
if (postedFields != null)
{
foreach (IViewModel postedField in (IEnumerable<IViewModel>)postedFields)
SaveDataWithEncryption.AddFieldData(postedField, formEntry);
}
FormDataProvider.CreateEntry(formEntry);
return true;
}
catch (Exception ex)
{
Logger.LogError(ex.Message, ex, (object)this);
return false;
}
}
protected static new void AddFieldData(IViewModel postedField, FormEntry formEntry)
{
Assert.ArgumentNotNull((object)postedField, nameof(postedField));
Assert.ArgumentNotNull((object)formEntry, nameof(formEntry));
if (!(postedField is IValueField valueField) || !valueField.AllowSave)
return;
PropertyInfo property = postedField.GetType().GetProperty("Value");
object postedValue = (object)property != null ? property.GetValue((object)postedField) : (object)null;
if (postedValue == null)
return;
string fullName = postedValue.GetType().FullName;
string reflectedTypeName = property.ReflectedType.FullName;
string fieldValue = ParseFieldValue(postedValue);
FieldData fieldData = new FieldData
{
FieldDataId = Guid.NewGuid(),
FieldItemId = Guid.Parse(postedField.ItemId),
FieldName = postedField.Name,
FormEntryId = formEntry.FormEntryId,
ValueType = fullName,
Value = reflectedTypeName == Constants.FormFields.SingleLineTextEncryptedFieldType
? string.IsNullOrWhiteSpace(fieldValue) ? fieldValue : EncryptFieldValue(fieldValue)
: fieldValue
};
formEntry.Fields.Add(fieldData);
}
protected static new string ParseFieldValue(object postedValue)
{
Assert.ArgumentNotNull(postedValue, nameof(postedValue));
List<string> stringList = new List<string>();
if (postedValue is IList list)
{
foreach (object obj in (IEnumerable)list)
stringList.Add(obj.ToString());
}
else
stringList.Add(postedValue.ToString());
return string.Join(",", (IEnumerable<string>)stringList);
}
private static string EncryptFieldValue(string valueToEncrypt)
{
try
{
if (string.IsNullOrWhiteSpace(valueToEncrypt))
{
Log.Error($"{Constants.Logging.LogPreText}Empty or null string passed to {nameof(EncryptFieldValue)}.\r\n\tOriginal (un-encrypted) value will be stored.", new Exception(), typeof(SaveDataWithEncryption));
return string.Empty;
}
/*
* YOUR ENCRYPTION LOGIC GOES HERE
*/
}
catch (Exception ex)
{
Log.Error($"{Constants.Logging.LogPreText}An error occurred encrypting the string.\r\n\tOriginal (un-encrypted) value will be stored.", ex, typeof(SaveDataWithEncryption));
return valueToEncrypt;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment