mirror of
https://dev.azure.com/TeilRad/sharee.bike%20App/_git/Code
synced 2024-12-27 01:26:26 +01:00
168 lines
5.5 KiB
C#
168 lines
5.5 KiB
C#
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
|
|||
|
namespace TINK.Model.User.Account
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
/// Holds the state of mail and password and information about some invalid parts if there are.
|
|||
|
/// </summary>
|
|||
|
public class State
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
/// Consider state to be invalid after construction.
|
|||
|
/// </summary>
|
|||
|
private Elements m_eElements = Elements.None;
|
|||
|
|
|||
|
private Dictionary<Elements, string> m_oDescription = new Dictionary<Elements, string>();
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Constructs object to state all entries are valid.
|
|||
|
/// </summary>
|
|||
|
public State()
|
|||
|
{
|
|||
|
m_eElements = Elements.Account;
|
|||
|
|
|||
|
m_oDescription = new Dictionary<Elements, string>
|
|||
|
{
|
|||
|
{ Elements.None, string.Empty },
|
|||
|
{ Elements.Account, string.Empty }
|
|||
|
};
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Constructs object to state some/ all elements are invalid.
|
|||
|
/// </summary>
|
|||
|
/// <param name="p_oValidParts">Specifies the parts which are invalid.</param>
|
|||
|
/// <param name="p_oDescription">Description of invalid parts.</param>
|
|||
|
public State(Elements p_oValidParts, Dictionary<Elements, string> p_oDescription)
|
|||
|
{
|
|||
|
m_eElements = p_oValidParts;
|
|||
|
|
|||
|
m_oDescription = p_oDescription ?? new Dictionary<Elements, string>();
|
|||
|
|
|||
|
// 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);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// True if account is valid.
|
|||
|
/// </summary>
|
|||
|
public bool IsValid { get { return ValidElement == Elements.Account; } }
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Specifies if both mail and password are valid, one of them or none.
|
|||
|
/// </summary>
|
|||
|
public Elements ValidElement
|
|||
|
{
|
|||
|
get
|
|||
|
{
|
|||
|
return m_eElements;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Holds the message about invalid elements.
|
|||
|
/// </summary>
|
|||
|
public Dictionary<Elements, string> Description
|
|||
|
{
|
|||
|
get
|
|||
|
{
|
|||
|
var l_oUserFriendlyDescription = new Dictionary<Elements, string>();
|
|||
|
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 ;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Verifies if a password is valid or not.
|
|||
|
/// </summary>
|
|||
|
/// <param name="p_strMail"></param>
|
|||
|
/// <param name="p_strPassword"></param>
|
|||
|
/// <returns></returns>
|
|||
|
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<Elements, string>();
|
|||
|
|
|||
|
// 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);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
}
|