using System; using System.Collections.Generic; using System.Text; using System.Collections.Specialized; using System.Collections; using System.Globalization; using System.Web.Hosting; using System.Diagnostics; using System.Configuration.Provider; namespace umbraco.providers { /// /// Security Helper Methods /// internal class SecUtility { /// /// Gets the boolean value. /// /// The config. /// Name of the value. /// if set to true [default value]. /// internal static bool GetBooleanValue(NameValueCollection config, string valueName, bool defaultValue) { bool flag; string str = config[valueName]; if (str == null) return defaultValue; if (!bool.TryParse(str, out flag)) { throw new ProviderException("Value must be boolean."); } return flag; } /// /// Checks the array parameter. /// /// The param. /// if set to true [check for null]. /// if set to true [check if empty]. /// if set to true [check for commas]. /// Size of the max. /// Name of the param. internal static void CheckArrayParameter(ref string[] param, bool checkForNull, bool checkIfEmpty, bool checkForCommas, int maxSize, string paramName) { if (param == null) { throw new ArgumentNullException(paramName); } if (param.Length < 1) { throw new ArgumentException("Parameter array empty."); } Hashtable hashtable = new Hashtable(param.Length); for (int i = param.Length - 1; i >= 0; i--) { CheckParameter(ref param[i], checkForNull, checkIfEmpty, checkForCommas, maxSize, paramName + "[ " + i.ToString(CultureInfo.InvariantCulture) + " ]"); if (hashtable.Contains(param[i])) { throw new ArgumentException("Parameter duplicate array element"); } hashtable.Add(param[i], param[i]); } } /// /// Checks the parameter. /// /// The param. /// if set to true [check for null]. /// if set to true [check if empty]. /// if set to true [check for commas]. /// Size of the max. /// Name of the param. internal static void CheckParameter(ref string param, bool checkForNull, bool checkIfEmpty, bool checkForCommas, int maxSize, string paramName) { if (param == null) { if (checkForNull) { throw new ArgumentNullException(paramName); } } else { param = param.Trim(); if (checkIfEmpty && (param.Length < 1)) { throw new ProviderException("Parameter can not be empty."); } if ((maxSize > 0) && (param.Length > maxSize)) { throw new ProviderException("Parameter too long."); } if (checkForCommas && param.Contains(",")) { throw new ProviderException("Parameter cannot contain comman."); } } } /// /// Checks the password parameter. /// /// The param. /// Size of the max. /// Name of the param. internal static void CheckPasswordParameter(ref string param, int maxSize, string paramName) { if (param == null) { throw new ArgumentNullException(paramName); } if (param.Length < 1) { throw new ProviderException("Parameter can not be empty"); } if ((maxSize > 0) && (param.Length > maxSize)) { throw new ProviderException("Parameter too long"); } } /// /// Gets the name of the default app. /// /// internal static string GetDefaultAppName() { try { string applicationVirtualPath = HostingEnvironment.ApplicationVirtualPath; if (string.IsNullOrEmpty(applicationVirtualPath)) { return "/"; } return applicationVirtualPath; } catch { return "/"; } } /// /// Gets the int value. /// /// The config. /// Name of the value. /// The default value. /// if set to true [zero allowed]. /// The max value allowed. /// internal static int GetIntValue(NameValueCollection config, string valueName, int defaultValue, bool zeroAllowed, int maxValueAllowed) { int num; string s = config[valueName]; if (s == null) { return defaultValue; } if (!int.TryParse(s, out num)) { if (zeroAllowed) { throw new ProviderException("Value must be non negative integer"); } throw new ProviderException("Value must be positive integer"); } if (zeroAllowed && (num < 0)) { throw new ProviderException("Value must be non negativeinteger"); } if (!zeroAllowed && (num <= 0)) { throw new ProviderException("Value must be positive integer"); } if ((maxValueAllowed > 0) && (num > maxValueAllowed)) { throw new ProviderException("Value too big"); } return num; } /// /// Validates the parameter. /// /// The param. /// if set to true [check for null]. /// if set to true [check if empty]. /// if set to true [check for commas]. /// Size of the max. /// internal static bool ValidateParameter(ref string param, bool checkForNull, bool checkIfEmpty, bool checkForCommas, int maxSize) { if (param == null) { return !checkForNull; } param = param.Trim(); return (((!checkIfEmpty || (param.Length >= 1)) && ((maxSize <= 0) || (param.Length <= maxSize))) && (!checkForCommas || !param.Contains(","))); } /// /// Validates the password parameter. /// /// The param. /// Size of the max. /// internal static bool ValidatePasswordParameter(ref string param, int maxSize) { if (param == null) { return false; } if (param.Length < 1) { return false; } if ((maxSize > 0) && (param.Length > maxSize)) { return false; } return true; } } }