mirror of
https://github.com/tribufu/ServerManagers
synced 2026-05-06 15:17:34 +00:00
source code checkin
This commit is contained in:
parent
5f8fb2c825
commit
7e57b72e35
675 changed files with 168433 additions and 0 deletions
|
|
@ -0,0 +1,33 @@
|
|||
using System;
|
||||
using System.Linq;
|
||||
using System.Windows.Controls;
|
||||
|
||||
namespace ServerManagerTool.Common.ValidationRules
|
||||
{
|
||||
public class IdListValidationRule : ValidationRule
|
||||
{
|
||||
public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
|
||||
{
|
||||
var strValue = (string)value;
|
||||
|
||||
if (!String.IsNullOrWhiteSpace(strValue))
|
||||
{
|
||||
// check if there are any spaces
|
||||
if (strValue.Contains(" "))
|
||||
{
|
||||
return new ValidationResult(false, "Spaces are not permitted");
|
||||
}
|
||||
|
||||
// check for valid ids
|
||||
var entries = strValue.Split(',');
|
||||
|
||||
if (entries.FirstOrDefault(e => !Int64.TryParse(e, out long throwaway)) != null)
|
||||
{
|
||||
return new ValidationResult(false, "Must be a comma-separated list of ids");
|
||||
}
|
||||
}
|
||||
|
||||
return new ValidationResult(true, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
62
src/ServerManager.Common/ValidationRules/IpValidationRule.cs
Normal file
62
src/ServerManager.Common/ValidationRules/IpValidationRule.cs
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Windows.Controls;
|
||||
|
||||
namespace ServerManagerTool.Common.ValidationRules
|
||||
{
|
||||
public class IpValidationRule : ValidationRule
|
||||
{
|
||||
public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
|
||||
{
|
||||
if (IpValidationRule.ValidateIP((string)value))
|
||||
{
|
||||
return ValidationResult.ValidResult;
|
||||
}
|
||||
else
|
||||
{
|
||||
return new ValidationResult(false, "Invalid IP address or host name");
|
||||
}
|
||||
}
|
||||
|
||||
private static bool ValidateIP(string source)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(source))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
IPAddress ipAddress;
|
||||
if (IPAddress.TryParse(source, out ipAddress))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Try DNS resolution
|
||||
try
|
||||
{
|
||||
var addresses = Dns.GetHostAddresses(source);
|
||||
var ip4Address = addresses.FirstOrDefault(a => a.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork);
|
||||
if (ip4Address != null)
|
||||
{
|
||||
Debug.WriteLine($"Resolved address {source} to {ip4Address.ToString()}");
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,37 @@
|
|||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Windows.Controls;
|
||||
|
||||
namespace ServerManagerTool.Common.ValidationRules
|
||||
{
|
||||
public class ProfileNameValidationRule : ValidationRule
|
||||
{
|
||||
/// <summary>
|
||||
/// When overridden in a derived class, performs validation checks on a value.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// A <see cref="T:System.Windows.Controls.ValidationResult"/> object.
|
||||
/// </returns>
|
||||
/// <param name="value">The value from the binding target to check.</param><param name="cultureInfo">The culture to use in this rule.</param>
|
||||
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
|
||||
{
|
||||
var strValue = (string)value;
|
||||
|
||||
var invalidFolderChars = Path.GetInvalidPathChars();
|
||||
var invalidFileChars = Path.GetInvalidFileNameChars();
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(strValue))
|
||||
{
|
||||
// check for invalid folder characters
|
||||
if (strValue.IndexOfAny(invalidFolderChars) >= 0)
|
||||
return new ValidationResult(false, "Contains an invalid folder character");
|
||||
|
||||
// check for invalid file characters
|
||||
if (strValue.IndexOfAny(invalidFileChars) >= 0)
|
||||
return new ValidationResult(false, "Contains an invalid file character");
|
||||
}
|
||||
|
||||
return new ValidationResult(true, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
using System.Globalization;
|
||||
using System.Windows.Controls;
|
||||
|
||||
namespace ServerManagerTool.Common.ValidationRules
|
||||
{
|
||||
public class StringNoSpacesValidationRule : ValidationRule
|
||||
{
|
||||
/// <summary>
|
||||
/// When overridden in a derived class, performs validation checks on a value.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// A <see cref="T:System.Windows.Controls.ValidationResult"/> object.
|
||||
/// </returns>
|
||||
/// <param name="value">The value from the binding target to check.</param><param name="cultureInfo">The culture to use in this rule.</param>
|
||||
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
|
||||
{
|
||||
var strValue = (string)value;
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(strValue))
|
||||
{
|
||||
// check if there are any spaces
|
||||
if (strValue.Contains(" "))
|
||||
return new ValidationResult(false, "Spaces are not permitted");
|
||||
}
|
||||
|
||||
return new ValidationResult(true, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,43 @@
|
|||
using System;
|
||||
using System.Windows.Controls;
|
||||
|
||||
namespace ServerManagerTool.Common.ValidationRules
|
||||
{
|
||||
public class TimeValidationRule : ValidationRule
|
||||
{
|
||||
public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
|
||||
{
|
||||
string strTime = (string)value;
|
||||
|
||||
var split = strTime.Split(':');
|
||||
if (split.Length != 2)
|
||||
{
|
||||
return new ValidationResult(false, "Invalid time. Time must be formatted as hh:mm");
|
||||
}
|
||||
|
||||
int hours;
|
||||
if(!Int32.TryParse(split[0], out hours))
|
||||
{
|
||||
return new ValidationResult(false, "Invalid hours. Time must be formatted as hh:mm");
|
||||
}
|
||||
|
||||
if(hours < 0 || hours > 23)
|
||||
{
|
||||
return new ValidationResult(false, "Hours must be a value from 00 to 23");
|
||||
}
|
||||
|
||||
int minutes;
|
||||
if(!Int32.TryParse(split[1], out minutes))
|
||||
{
|
||||
return new ValidationResult(false, "Invalid hours. Time must be formatted as hh:mm");
|
||||
}
|
||||
|
||||
if(minutes < 0 || minutes > 59)
|
||||
{
|
||||
return new ValidationResult(false, "Miunutes must be a value from 00 to 59");
|
||||
}
|
||||
|
||||
return new ValidationResult(true, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue