using System;
using System.Collections.Generic;
using System.Linq;
namespace TINK.Model.User.Account
{
///
/// Holds the state of mail and password and information about some invalid parts if there are.
///
public class State
{
///
/// Consider state to be invalid after construction.
///
private Elements m_eElements = Elements.None;
private Dictionary m_oDescription = new Dictionary();
///
/// Constructs object to state all entries are valid.
///
public State()
{
m_eElements = Elements.Account;
m_oDescription = new Dictionary
{
{ Elements.None, string.Empty },
{ Elements.Account, string.Empty }
};
}
///
/// Constructs object to state some/ all elements are invalid.
///
/// Specifies the parts which are invalid.
/// Description of invalid parts.
public State(Elements p_oValidParts, Dictionary p_oDescription)
{
m_eElements = p_oValidParts;
m_oDescription = p_oDescription ?? new Dictionary();
// Ensure consistency
foreach (Elements l_oElement in Enum.GetValues(typeof(Elements)))
{
if (!m_oDescription.ContainsKey(l_oElement))
{
switch (l_oElement)
{
case Elements.Account:
case Elements.None:
continue;
}
m_oDescription.Add(l_oElement, string.Empty);
}
}
}
///
/// True if account is valid.
///
public bool IsValid { get { return ValidElement == Elements.Account; } }
///
/// Specifies if both mail and password are valid, one of them or none.
///
public Elements ValidElement
{
get
{
return m_eElements;
}
}
///
/// Holds the message about invalid elements.
///
public Dictionary Description
{
get
{
var l_oUserFriendlyDescription = new Dictionary();
foreach (Elements l_oElement in Enum.GetValues(typeof(Elements)))
{
switch (l_oElement)
{
case Elements.Account:
case Elements.None:
continue;
}
l_oUserFriendlyDescription.Add(
l_oElement,
m_oDescription.ContainsKey(l_oElement) ? m_oDescription[l_oElement] : string.Empty);
}
l_oUserFriendlyDescription.Add(
Elements.Account,
string.Join(";", l_oUserFriendlyDescription.Where(x => x.Value.Length > 0).Select(x => x.Value).ToArray()));
return l_oUserFriendlyDescription ;
}
}
}
///
/// Verifies if a password is valid or not.
///
///
///
///
public delegate State PasswordValidator(string p_strMail, string p_strPassword);
public static class Validator
{
public static State ValidateMailAndPasswordDelegate(string p_strMail, string p_strPassword)
{
var l_oElements = Elements.None;
var l_oDescription = new Dictionary();
// Validate mail address.
if (string.IsNullOrEmpty(p_strMail))
{
l_oDescription.Add(Elements.Mail, "Email Addresse darf nicht leer sein.");
}
else if (p_strMail.ToString().Split('@').Length < 2)
{
l_oDescription.Add(Elements.Mail, "Email Adresse mit Zeichen \"@\" enthalten.");
}
else if (p_strMail.ToString().Split('@')[0].Length <= 0)
{
l_oDescription.Add(Elements.Mail, "Benutzername in Email Adresse darf nicht leer sein.");
}
else if (p_strMail.ToString().Split('@')[1].Length <= 0)
{
// Data has been entered
l_oDescription.Add(Elements.Mail, "Domain- Name in Email Adresse darf nicht leer sein.");
}
else
{
// Input mail address is ok
l_oElements = Elements.Mail;
l_oDescription.Add(Elements.Mail, string.Empty);
}
// Validate password.
if (string.IsNullOrEmpty(p_strPassword) || p_strPassword.Length < 8)
{
// Data has been entered
l_oDescription.Add(Elements.Password, "Passwort is zu kurz.");
}
else
{
// Password is ok
l_oElements |= Elements.Password;
l_oDescription.Add(Elements.Password, string.Empty);
}
return new State(l_oElements, l_oDescription);
}
}
}