/// <summary> /// Takes in an IUnMaskableObject and unmasks it based on whether certain columns are masked. /// </summary> /// <param name="obj">The object to unmask.</param> /// <returns>The unmasked IUnMaskableObject</returns> public async Task <IUnMaskableObject> UnMaskAsync(IUnMaskableObject obj) { // If the object is already unmasked we will simply return what was passed. if (!obj.IsUnMasked()) { // Get the mask information: a list of tuples<object, bool>, the data and whether it's column is masked. List <Tuple <object, bool> > info = obj.GetMaskInformation(); // The list of objects to pass to the constructor. object[] parameters = new object[info.Count]; for (int i = 0; i < info.Count; i++) { // Loop through the list and get all individual tuples. Tuple <object, bool> data = info[i]; // If the data's column is masked... if (data.Item2) { // If the data is a string simply add the actual value to the parameters. if (data.Item1 is string) { MapObject map = await _mapDAO.ReadByIdAsync(data.Item1.ToString()).ConfigureAwait(false) as MapObject; parameters[i] = map.Actual; } else { // Otherwise we must parse the string actual value into an integer and add that to the parameters. MapObject map = await _mapDAO.ReadByIdAsync(data.Item1.ToString()).ConfigureAwait(false) as MapObject; parameters[i] = Int32.Parse(map.Actual); } } else { parameters[i] = data.Item1; } } // Create an object of the type that was passed to this function by getting the constructor and invoking it with // the collected parameters. IUnMaskableObject resultObject = (IUnMaskableObject)obj.GetType().GetConstructor(obj.GetParameterTypes()).Invoke(parameters); resultObject.SetToUnMasked(); return(resultObject); } else { return(obj); } }
/// <summary> /// Decrements the mappings of a masked object in the Map table, specifically during an update operation, /// where a record used for the update must be passed in as well to determine whether columns are being updated. /// </summary> /// <param name="updateRecord">The record being used for the update.</param> /// <param name="maskedObject">The object to decrement the mappings for.</param> /// <returns>(bool) whether the function executed without exception</returns> public async Task <bool> DecrementMappingForUpdateAsync(IMaskableRecord updateRecord, IUnMaskableObject maskedObject) { if (maskedObject.IsUnMasked()) { throw new ArgumentException(Constants.DecrementUpdateUnmasked); } List <Tuple <object, bool> > recordInfo = updateRecord.GetMaskInformation(); List <Tuple <object, bool> > objectInfo = maskedObject.GetMaskInformation(); for (int i = 0; i < recordInfo.Count; i++) { Tuple <object, bool> recordData = recordInfo[i]; Tuple <object, bool> objectData = objectInfo[i]; // If the column is masked... if (recordData.Item2) { // The data is being updated if the corresponding record data is not null or -1. bool beingUpdated = true; if (recordData.Item1 is int) { if ((int)recordData.Item1 == -1) { beingUpdated = false; } } if (recordData.Item1 == null) { beingUpdated = false; } if (recordData.Item1 is long) { if ((long)recordData.Item1 == -1) { beingUpdated = false; } } // If the data is being updated and it is not the primary key (first entry in the mask information), // decrement the occurrences of the hash. if (beingUpdated && i != 0) { await DecrementOccurrencesAsync(objectData.Item1.ToString()).ConfigureAwait(false); } } } return(true); }
/// <summary> /// Decrements the mappings of a masked object in the Map table, specifically during a delete operation. /// </summary> /// <param name="maskedObject">The object to decrement the mappings of</param> /// <returns>(bool) whether the function executed without exception</returns> public async Task <bool> DecrementMappingForDeleteAsync(IUnMaskableObject maskedObject) { if (maskedObject.IsUnMasked()) { throw new ArgumentException(Constants.DecrementDeleteUnmasked); } List <Tuple <object, bool> > info = maskedObject.GetMaskInformation(); for (int i = 0; i < info.Count; i++) { Tuple <object, bool> data = info[i]; // If the column is masked, decrement the occurrences of the hash. if (data.Item2) { await DecrementOccurrencesAsync(data.Item1.ToString()).ConfigureAwait(false); } } return(true); }