using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace Sdaleo.Systems
{
///
/// Validation Consts used throughout Validation of Parameters
///
public static class ValidationConsts
{
#region Validation Consts
// Common
public const string CharSet_CharsNum = "0123456789";
public const string CharSet_CharsAlpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
public const string CharSet_CharsAlphaNum = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
public const string CharSet_CharsSambaComputerNames = CharSet_CharsAlphaNum + " -_";
public const string CharSet_CharsIPAddress = CharSet_CharsNum + ".";
public const string CharSet_CharsSpecialKeyboardAscii = " -_!@#$%^&*()_{}\\|:\":/?.,<>~`*-+[]'";
// Not Allowed
public const string CharSet_NotAllowedInFileNames = @"\/:*?<>|" + "\"";
public const string CharSet_NotAllowedInDirectorPaths = @"/*?<>|" + "\"";
// Allowed
public const string CharSet_AllowedUserNames = CharSet_CharsAlphaNum + " -_";
public const string CharSet_AllowedPasswords = CharSet_CharsAlphaNum + CharSet_CharsSpecialKeyboardAscii;
public const string CharSet_AllowedServerNames = CharSet_CharsSambaComputerNames + CharSet_CharsIPAddress; // we allow IP addresses as computer names
public const string CharSet_AllowedInstanceNames = CharSet_CharsAlphaNum + "$-_";
public const string CharSet_AllowedDatabaseNames = CharSet_CharsAlphaNum + CharSet_CharsSpecialKeyboardAscii;
public const string CharSet_AllowedTableNames = CharSet_CharsAlphaNum + " -_";
///
/// Generic Function to use with Allowed Character Sets above
///
/// string to evaluate
/// Pass in one of the legal character consts sets declared above
/// true if valid, false otherwise
public static bool ContainsOnlyLegalChars(string TextToEvaluate, string TextToEvaluateWith)
{
foreach (char c in TextToEvaluate.ToCharArray())
{
bool bFound = (TextToEvaluateWith.IndexOf(c) >= 0);
if (!bFound)
return false;
}
return true;
}
#endregion
#region CredentialValidation
///
/// Use this to validate a credential object against a specific system
///
/// credential object to validate
/// the system to validate against
/// DBerror that either contains or doesn't contain an error
public static DBError IsCredentialValid(IConnectDb credential, DBSystem system)
{
if (credential == null)
return DBError.Create("Invalid Credential Object Passed In");
else if (credential.DBType != system)
return DBError.Create("Credential Object doesn't match system");
else if (!credential.IsValid)
return DBError.Create("Credential is invalid");
else
return new DBError();
}
#endregion
///
/// Common Generally Generic Validation Functions that should be the same across the board
/// for all DBMS systems
///
public static class Generic
{
///
/// Main Validation Function to validate Usernames
///
/// Username to validate
/// true if valid, false otherise
public static bool IsValidUserName(string strUserName)
{
if (String.IsNullOrEmpty(strUserName))
{
return false;
}
else if (strUserName.Length < 1 || strUserName.Length > 128) // limit User Name length
{
return false;
}
else if (!ContainsOnlyLegalChars(strUserName, CharSet_AllowedUserNames))
{
return false;
}
else
{
return true;
}
}
///
/// Main Validation Function to validate Passwords
///
/// Password to validate
/// true if valid, false otherise
public static bool IsValidPassword(string strPassword)
{
if (String.IsNullOrEmpty(strPassword))
{
return false;
}
else if (strPassword.Length < 1 || strPassword.Length > 128) // limit Password Length
{
return false;
}
else if (!ContainsOnlyLegalChars(strPassword, CharSet_AllowedPasswords))
{
return false;
}
else
{
return true;
}
}
///
/// Calls IsValidUser && IsValidPassword
///
/// true if valid, false otherise
internal static bool IsValidUserCredential(string strUserName, string strPassword)
{
return IsValidUserName(strUserName) && IsValidPassword(strPassword);
}
///
/// Main Validation Function to validate Server Names
///
/// Name of a Server
/// true if valid, false otherise
internal static bool IsValidServerName(string strServerName)
{
if (String.IsNullOrEmpty(strServerName))
{
return false;
}
// The maximum length for NetBIOS names is 15 characters. The maximum length for DNS computer names
// in an Active Directory environment is 24 characters
else if (strServerName.Length < 1 || strServerName.Length > 24) // limit server Name length
{
return false;
}
else if (!ContainsOnlyLegalChars(strServerName, CharSet_AllowedServerNames))
{
return false;
}
else
{
return true;
}
}
///
/// Main Validation Function to validate Instancse Names
///
/// Name of an Instance
/// true if valid, false otherise
internal static bool IsValidInstanceName(string strInstanceName)
{
if (string.IsNullOrEmpty(strInstanceName))
{
return false;
}
else if (strInstanceName.Length < 1 || strInstanceName.Length > 16) // limit Instance Name length
{
return false;
}
else if (!ContainsOnlyLegalChars(strInstanceName, CharSet_AllowedInstanceNames))
{
return false;
}
else
{
return true;
}
}
}
#region FileName and Path Validation
///
/// Check to see if the filename is valid
///
/// filename to check
/// true if valid, false otherwise
public static bool IsValidFileName(string Filename)
{
if (!String.IsNullOrEmpty(Filename))
{
bool bIsValid = !ContainsOnlyLegalChars(Filename, CharSet_NotAllowedInFileNames);
return bIsValid;
}
return false;
}
///
/// Check to see if directory path is valid
///
/// directory path
/// true if valid, false otherwise
public static bool IsValidDirectoryPath(string Path)
{
if (!String.IsNullOrEmpty(Path))
{
bool bIsValid = !ContainsOnlyLegalChars(Path, CharSet_NotAllowedInDirectorPaths);
return bIsValid;
}
return false;
}
///
/// Check to see if directory and file name are valid
///
/// filename including path information
/// true if valid, false otherwise
public static bool IsValidFileNameNPath(string FileNameNPath)
{
if(!String.IsNullOrEmpty(FileNameNPath))
{
string path = Path.GetDirectoryName(FileNameNPath);
string file = Path.GetFileName(FileNameNPath);
bool bValid = IsValidDirectoryPath(path) && IsValidFileName(file);
return bValid;
}
return false;
}
#endregion
}
}