Exemplo n.º 1
0
        /// <summary>
        /// Updates an entry in the IP Address data store using the <paramref name="ipRecord"/>.
        /// </summary>
        /// <param name="ipRecord">The unmasked record conveying the data to update.</param>
        /// <returns>Task (bool) whether the function completed without exception</returns>
        public async Task <bool> UpdateIPAsync(IPAddressRecord ipRecord)
        {
            // Record must be unmasked.
            if (ipRecord.IsMasked())
            {
                throw new ArgumentException(Constants.UpdateIPRecordMasked);
            }

            // If the column is masked, mask the ip.
            string id = ipRecord.GetData()[Constants.AnonymousUserDAOIPColumn] as string;

            if (Constants.AnonymousUserDAOIsColumnMasked[Constants.AnonymousUserDAOIPColumn])
            {
                id = _maskingService.MaskString(id);
            }

            IPAddressRecord maskedRecord = await _maskingService.MaskAsync(ipRecord, false).ConfigureAwait(false) as IPAddressRecord;

            // Get the masked object from the ipDAO and decrement its mapping before updating.
            IPAddressObject maskedObj = await _anonymousUserDAO.ReadByIdAsync(id).ConfigureAwait(false) as IPAddressObject;

            await _maskingService.DecrementMappingForUpdateAsync(maskedRecord, maskedObj).ConfigureAwait(false);

            return(await _anonymousUserDAO.UpdateAsync(maskedRecord).ConfigureAwait(false));
        }
Exemplo n.º 2
0
        private bool DataEquals(IPAddressRecord ipRecord, IPAddressObject ipObject)
        {
            IDictionary <string, object> recordData = ipRecord.GetData();

            Console.WriteLine(recordData[Constants.AnonymousUserDAOIPColumn]);
            Console.WriteLine(recordData[Constants.AnonymousUserDAOtimestampLockedColumn]);
            Console.WriteLine(recordData[Constants.AnonymousUserDAOregistrationFailuresColumn]);
            Console.WriteLine(recordData[Constants.AnonymousUserDAOlastRegFailTimestampColumn]);
            Console.WriteLine(ipObject.IP);
            Console.WriteLine(ipObject.TimestampLocked);
            Console.WriteLine(ipObject.RegistrationFailures);
            Console.WriteLine(ipObject.LastRegFailTimestamp);

            if (((string)recordData[Constants.AnonymousUserDAOIPColumn]).Equals(ipObject.IP) &&
                ((long)recordData[Constants.AnonymousUserDAOtimestampLockedColumn]).Equals(ipObject.TimestampLocked) &&
                ((int)recordData[Constants.AnonymousUserDAOregistrationFailuresColumn]).Equals(ipObject.RegistrationFailures) &&
                ((long)recordData[Constants.AnonymousUserDAOlastRegFailTimestampColumn]).Equals(ipObject.LastRegFailTimestamp))
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Asynchronously creates the <paramref name="record"/> in the data store.
        /// </summary>
        /// <param name="record">The record to insert (ISQLRecord)</param>
        /// <returns>Task(bool) whether the function executed without exception.</returns>
        public async Task <bool> CreateAsync(ISQLRecord record)
        {
            // Try casting the record to an IPAddressRecord, throw an argument exception if it fails.
            try
            {
                IPAddressRecord temp = (IPAddressRecord)record;
            }
            catch
            {
                throw new ArgumentException(Constants.IPCreateInvalidArgument);
            }

            // Get the data stored in the record.
            IPAddressRecord ipRecord = (IPAddressRecord)record;
            IDictionary <string, object> recordData = ipRecord.GetData();

            // Get the connection inside a using statement to properly dispose/close.
            using (MySqlConnection connection = new MySqlConnection(SQLConnection))
            {
                // Open the connection
                connection.Open();

                // Construct the sql string .. start by inserting into the table name
                string sqlString = $"INSERT INTO {Constants.AnonymousUserDAOTableName} (";

                // Loop through the data.
                foreach (KeyValuePair <string, object> pair in recordData)
                {
                    // Check for null values in the data (string == null, numeric == -1), and throw a NoNullAllowedException
                    // if one is found.
                    if (pair.Value is int)
                    {
                        if ((int)pair.Value == -1)
                        {
                            throw new NoNullAllowedException(Constants.IPRecordNoNull);
                        }
                    }
                    if (pair.Value is string)
                    {
                        if (pair.Value == null)
                        {
                            throw new NoNullAllowedException(Constants.IPRecordNoNull);
                        }
                    }
                    if (pair.Value is long)
                    {
                        if ((long)pair.Value == -1)
                        {
                            throw new NoNullAllowedException(Constants.IPRecordNoNull);
                        }
                    }

                    // Otherwise add the key to the string (column name).
                    sqlString += $"{pair.Key},";
                }

                // Remove the last comma, add the VALUES keyword
                sqlString  = sqlString.Remove(sqlString.Length - 1);
                sqlString += ") VALUES (";

                // Loop through the data once again, but instead of constructing the string with user input, use
                // @PARAM0, @PARAM1 parameters to prevent against sql injections from user input.
                int count = 0;
                foreach (KeyValuePair <string, object> pair in recordData)
                {
                    sqlString += $"@PARAM{count},";
                    count++;
                }

                // Remove the last comma and add the last ) and ;
                sqlString  = sqlString.Remove(sqlString.Length - 1);
                sqlString += ");";

                // Get the command object inside a using statement to properly dispose/close.
                using (MySqlCommand command = new MySqlCommand(sqlString, connection))
                {
                    count = 0;

                    // Loop through the data again to add the parameter values to the corresponding @PARAMs in the string.
                    foreach (KeyValuePair <string, object> pair in recordData)
                    {
                        command.Parameters.AddWithValue($"@PARAM{count}", pair.Value);
                        count++;
                    }

                    // Asynchronously execute the non query.
                    await command.ExecuteNonQueryAsync().ConfigureAwait(false);
                }

                return(true);
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Update the <paramref name="record"/> in the data store based on the values that are not null inside it.
        /// </summary>
        /// <param name="record">The record containing the information to update (ISQLRecord)</param>
        /// <returns>Task (bool) whether the function executed without exception</returns>
        public async Task <bool> UpdateAsync(ISQLRecord record)
        {
            // Try casting the record to an IPAddressRecord, throw an argument exception if it fails.
            try
            {
                IPAddressRecord temp = (IPAddressRecord)record;
            }
            catch
            {
                throw new ArgumentException(Constants.IPUpdateInvalidArgument);
            }

            // Get the record data.
            IPAddressRecord ipRecord = (IPAddressRecord)record;
            IDictionary <string, object> recordData = ipRecord.GetData();

            // Get the connection inside a using statement to properly dispose/close.
            using (MySqlConnection connection = new MySqlConnection(SQLConnection))
            {
                // Open the connection.
                connection.Open();

                // Construct the sql string to update the table name where..
                string sqlString = $"UPDATE {Constants.AnonymousUserDAOTableName} SET ";

                // Loop through the record data.
                int count = 0;
                foreach (KeyValuePair <string, object> pair in recordData)
                {
                    // Check if the value at the ip column is contained within the table, throw an argument
                    // exception if it doesn't exist.
                    if (pair.Key == Constants.AnonymousUserDAOIPColumn)
                    {
                        if (!await CheckIPExistenceAsync((string)pair.Value).ConfigureAwait(false))
                        {
                            throw new ArgumentException(Constants.IPUpdateDNE);
                        }
                    }

                    // Update only the values where the record value is not null (string == null, numeric == -1).
                    // Again, use parameters to prevent against sql injections.
                    if (pair.Key != Constants.AnonymousUserDAOIPColumn)
                    {
                        if (pair.Value is int)
                        {
                            if ((int)pair.Value != -1)
                            {
                                sqlString += $"{pair.Key} = @PARAM{count},";
                            }
                        }
                        if (pair.Value is string)
                        {
                            if (pair.Value != null)
                            {
                                sqlString += $"{pair.Key} = @PARAM{count},";
                            }
                        }
                        if (pair.Value is long)
                        {
                            if ((long)pair.Value != -1)
                            {
                                sqlString += $"{pair.Key} = @PARAM{count},";
                            }
                        }
                    }

                    count++;
                }

                // Remove the last comma and identify the record by its ip column.
                sqlString  = sqlString.Remove(sqlString.Length - 1);
                sqlString += $" WHERE {Constants.AnonymousUserDAOIPColumn} = '{recordData[Constants.AnonymousUserDAOIPColumn]}';";

                // Get the command inside a using statement to properly dispose/close.
                using (MySqlCommand command = new MySqlCommand(sqlString, connection))
                {
                    // Loop through the record data again to add values to the parameters.
                    count = 0;
                    foreach (KeyValuePair <string, object> pair in recordData)
                    {
                        if (pair.Key != Constants.AnonymousUserDAOIPColumn)
                        {
                            if (pair.Value is int)
                            {
                                if ((int)pair.Value != -1)
                                {
                                    command.Parameters.AddWithValue($"@PARAM{count}", pair.Value);
                                }
                            }
                            if (pair.Value is string)
                            {
                                if (pair.Value != null)
                                {
                                    command.Parameters.AddWithValue($"@PARAM{count}", pair.Value);
                                }
                            }
                            if (pair.Value is long)
                            {
                                if ((long)pair.Value != -1)
                                {
                                    command.Parameters.AddWithValue($"@PARAM{count}", pair.Value);
                                }
                            }
                        }

                        count++;
                    }

                    // Execute the non query asynchronously.
                    await command.ExecuteNonQueryAsync().ConfigureAwait(false);
                }

                return(true);
            }
        }