Add project files.
This commit is contained in:
@@ -0,0 +1,389 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Sarand.Net.Standard.Base.Core;
|
||||
using Sarand.Net.Standard.Base.Bus;
|
||||
using Sarand.Net.Standard.Base.App;
|
||||
|
||||
namespace Sarand.Net.Standard.Base.Security
|
||||
{
|
||||
//=======================================================================
|
||||
public abstract class SaSecurityItem : SacsHierarchyItem
|
||||
{
|
||||
//-------------------------------------------------------------------
|
||||
// Columns.
|
||||
public enum Column
|
||||
{
|
||||
Pos = 0,
|
||||
Id,
|
||||
ItemType,
|
||||
ItemType_Desc,
|
||||
Code,
|
||||
EmailAddress,
|
||||
Description,
|
||||
Location,
|
||||
LastLogin_Date,
|
||||
OwnerLinkCount,
|
||||
TargetLinkCount,
|
||||
TargetLinkCount_Groups,
|
||||
OwnerLinkCount_Rights,
|
||||
Notes,
|
||||
Status,
|
||||
Status_Desc,
|
||||
Status_Ind,
|
||||
CreateOn_Date,
|
||||
UpdateOn_Date,
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------
|
||||
/// <summary>Properties other than common.</summary>
|
||||
public class DataProperties
|
||||
{
|
||||
public SaSecurity.ItemType ItemType { get; set; } = SaSecurity.ItemType.Unknown;
|
||||
public String Code { get; set; } = String.Empty;
|
||||
public String EmailAddress { get; set; } = String.Empty;
|
||||
public String Description { get; set; } = String.Empty;
|
||||
public String Location { get; set; } = String.Empty;
|
||||
public String PasswordEncrypted { get; set; } = String.Empty;
|
||||
public String PasswordSalt { get; set; } = String.Empty;
|
||||
public String PasswordHash { get; set; } = String.Empty;
|
||||
public DateTime LastLogin { get; set; } = DateTime.MinValue;
|
||||
public int DataItemType { get; set; } = -1;
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------
|
||||
// Data.
|
||||
public new class Data : SacsHierarchyItem.Data
|
||||
{
|
||||
// Data members.
|
||||
private readonly DataProperties data = new DataProperties();
|
||||
// Construction.
|
||||
public Data ()
|
||||
{
|
||||
Status = DefaultStatus;
|
||||
}
|
||||
// Properties.
|
||||
public SaSecurity.ItemType ItemType
|
||||
{
|
||||
get { return data.ItemType; }
|
||||
set { CheckChanged((int)data.ItemType, (int)value); data.ItemType = value; }
|
||||
}
|
||||
public String Code
|
||||
{
|
||||
get { return data.Code; }
|
||||
set { CheckChanged(data.Code, value); data.Code = value; }
|
||||
}
|
||||
public String EmailAddress
|
||||
{
|
||||
get { return data.EmailAddress; }
|
||||
set { CheckChanged(data.EmailAddress, value); data.EmailAddress = value; }
|
||||
}
|
||||
public String Description
|
||||
{
|
||||
get { return data.Description; }
|
||||
set { CheckChanged(data.Description, value); data.Description = value; }
|
||||
}
|
||||
public String Location
|
||||
{
|
||||
get { return data.Location; }
|
||||
set { CheckChanged(data.Location, value); data.Location = value; }
|
||||
}
|
||||
public String PasswordEncrypted
|
||||
{
|
||||
get { return data.PasswordEncrypted; }
|
||||
set { CheckChanged(data.PasswordEncrypted, value); data.PasswordEncrypted = value; }
|
||||
}
|
||||
public String PasswordSalt
|
||||
{
|
||||
get { return data.PasswordSalt; }
|
||||
set { CheckChanged(data.PasswordSalt, value); data.PasswordSalt = value; }
|
||||
}
|
||||
public String PasswordHash
|
||||
{
|
||||
get { return data.PasswordHash; }
|
||||
set { CheckChanged(data.PasswordHash, value); data.PasswordHash = value; }
|
||||
}
|
||||
public DateTime LastLogin
|
||||
{
|
||||
get { return data.LastLogin; }
|
||||
set { CheckChanged(data.LastLogin, value); data.LastLogin = value; }
|
||||
}
|
||||
public int DataItemType
|
||||
{
|
||||
get { return data.DataItemType; }
|
||||
set { CheckChanged(data.DataItemType, value); data.DataItemType = value; }
|
||||
}
|
||||
// Flags.
|
||||
public bool AuthenticatedUser
|
||||
{
|
||||
get { return GetFlag(Flag_AuthenticatedUser); }
|
||||
set { SetFlag(Flag_AuthenticatedUser, value); }
|
||||
}
|
||||
// Population.
|
||||
internal void PopulateFrom (Data data)
|
||||
{
|
||||
ItemType = data.ItemType;
|
||||
Code = data.Code;
|
||||
EmailAddress = data.EmailAddress;
|
||||
Description = data.Description;
|
||||
Location = data.Location;
|
||||
PasswordEncrypted = data.PasswordEncrypted;
|
||||
PasswordSalt = data.PasswordSalt;
|
||||
PasswordHash = data.PasswordHash;
|
||||
LastLogin = data.LastLogin;
|
||||
DataItemType = data.DataItemType;
|
||||
base.PopulateFrom(data);
|
||||
}
|
||||
public Data Clone ()
|
||||
{
|
||||
Data data = new Data();
|
||||
data.PopulateFrom(this);
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------
|
||||
// Error field - identify properties with validation error.
|
||||
public new class ErrField : SacsHierarchyItem.ErrField
|
||||
{
|
||||
// Identifies the field that has invalid data.
|
||||
public ValidValue ItemType { get; set; } = ValidValue.Valid;
|
||||
public ValidValue Code { get; set; } = ValidValue.Valid;
|
||||
public ValidValue EmailAddress { get; set; } = ValidValue.Valid;
|
||||
public ValidValue Description { get; set; } = ValidValue.Valid;
|
||||
public ValidValue Location { get; set; } = ValidValue.Valid;
|
||||
public ValidValue Password { get; set; } = ValidValue.Valid;
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------
|
||||
// Data members.
|
||||
private readonly Data data = new Data();
|
||||
// Flags.
|
||||
public static readonly Byte Flag_AuthenticatedUser = 0x01;
|
||||
|
||||
//-------------------------------------------------------------------
|
||||
// Construction.
|
||||
public SaSecurityItem (SysId id, SaSecurity.ItemType securityItemType) :
|
||||
base(id)
|
||||
{
|
||||
base.SetData(data);
|
||||
ItemType = securityItemType;
|
||||
Status = DefaultStatus;
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------
|
||||
// Status.
|
||||
private static readonly List<SaStatus> securityItemStatuses = new List<SaStatus>()
|
||||
{
|
||||
SaStatus.Enabled,
|
||||
SaStatus.Disabled,
|
||||
};
|
||||
public static SaStatus DefaultStatus { get { return EnabledStatus; } }
|
||||
public static SaStatus EnabledStatus { get { return SaStatus.Enabled; } }
|
||||
public static SaStatus DisabledStatus { get { return SaStatus.Disabled; } }
|
||||
public static List<SaStatus> ValidStatuses { get { return new List<SaStatus>(securityItemStatuses); } }
|
||||
public static DataValue.TriStateType GetStatusTriState (SaStatus status)
|
||||
{
|
||||
switch (status)
|
||||
{
|
||||
case SaStatus.Enabled:
|
||||
return DataValue.TriStateType.Green;
|
||||
case SaStatus.Disabled:
|
||||
return DataValue.TriStateType.Red;
|
||||
case SaStatus.Deleted:
|
||||
return DataValue.TriStateType.Purple;
|
||||
}
|
||||
return DataValue.TriStateType.Unknown;
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------
|
||||
// Data properties.
|
||||
public Data GetData () { return data.Clone(); }
|
||||
public void SetData (Data data)
|
||||
{
|
||||
String fn = GetType().Name + ".SetData";
|
||||
// Properties that cannot be changed if previously specified.
|
||||
if (SysId.IsValid(Id) && SysId.ToInt(Id) != SysId.ToInt(data.Id))
|
||||
throw new NotSupportedException(fn + " Id cannot be changed");
|
||||
if ((int)data.ItemType > 0 && data.ItemType != ItemType)
|
||||
throw new NotSupportedException(fn + " Item Type cannot be changed");
|
||||
// Populate individual properties to ensure that 'Changed' indicator is kept up to date.
|
||||
this.data.PopulateFrom(data);
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------
|
||||
// Properties.
|
||||
public SaSecurity.ItemType ItemType
|
||||
{
|
||||
get { return data.ItemType; }
|
||||
protected set { data.ItemType = value; }
|
||||
}
|
||||
public String Code
|
||||
{
|
||||
get { return data.Code; }
|
||||
set { data.Code = value; }
|
||||
}
|
||||
public String EmailAddress
|
||||
{
|
||||
get { return data.EmailAddress; }
|
||||
set { data.EmailAddress = value; }
|
||||
}
|
||||
public String Description
|
||||
{
|
||||
get { return data.Description; }
|
||||
set { data.Description = value; }
|
||||
}
|
||||
public String Location
|
||||
{
|
||||
get { return data.Location; }
|
||||
set { data.Location = value; }
|
||||
}
|
||||
public String PasswordEncrypted
|
||||
{
|
||||
get { return data.PasswordEncrypted; }
|
||||
set { data.PasswordEncrypted = value; }
|
||||
}
|
||||
public String PasswordSalt
|
||||
{
|
||||
get { return data.PasswordSalt; }
|
||||
set { data.PasswordSalt = value; }
|
||||
}
|
||||
public String PasswordHash
|
||||
{
|
||||
get { return data.PasswordHash; }
|
||||
set { data.PasswordHash = value; }
|
||||
}
|
||||
public DateTime LastLogin
|
||||
{
|
||||
get { return data.LastLogin; }
|
||||
set { data.LastLogin = value; }
|
||||
}
|
||||
public int DataItemType
|
||||
{
|
||||
get { return data.DataItemType; }
|
||||
set { data.DataItemType = value; }
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------
|
||||
// Flags.
|
||||
public bool AuthenticatedUser
|
||||
{
|
||||
get { return GetFlag(Flag_AuthenticatedUser); }
|
||||
set { SetFlag(Flag_AuthenticatedUser, value); }
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------
|
||||
// Password utilities.
|
||||
private static readonly Encoding Encoding_Ansi1252 = Encoding.GetEncoding(1252);
|
||||
public bool IsPasswordBlank ()
|
||||
{
|
||||
return EncryptPassword("").Equals(PasswordEncrypted);
|
||||
}
|
||||
public bool PasswordMatches (String password)
|
||||
{
|
||||
return PasswordEncrypted.Equals(EncryptPassword(password));
|
||||
}
|
||||
public static String EncryptPassword (String password)
|
||||
{
|
||||
//String fn = "SaSecurityItem.EncryptPassword";
|
||||
|
||||
// We know this is not very water-tight, but it at least
|
||||
// hides the contents of the password from prying eyes.
|
||||
String toBeEncrypted = password;
|
||||
// Password length goes in as first character:
|
||||
toBeEncrypted = ByteToString((byte)(128 - password.Length)) + toBeEncrypted;
|
||||
toBeEncrypted = string.Concat(toBeEncrypted.Reverse());
|
||||
// Pad out to fixed length (minimum 16, multiples of 8):
|
||||
int fixedLength = 16;
|
||||
if (password.Length / 8 > 2) fixedLength = 8 * (int)(password.Length / 8);
|
||||
while (toBeEncrypted.Length < fixedLength)
|
||||
{
|
||||
char charToAdd = (char)(password.Length > 0 ? password[(toBeEncrypted.Length % password.Length)] : 'A' + (toBeEncrypted.Length * 13) % 23);
|
||||
if (toBeEncrypted.Length % 2 != 0)
|
||||
toBeEncrypted += charToAdd;
|
||||
else toBeEncrypted = charToAdd + toBeEncrypted;
|
||||
}
|
||||
|
||||
// Now perform a simple algorythm on each character to hide its true value:
|
||||
byte[] encryptedChars = new byte[toBeEncrypted.Length];
|
||||
for (int i = 0; i < toBeEncrypted.Length; i++)
|
||||
{
|
||||
int iData = toBeEncrypted[i];
|
||||
iData += 17 + i;
|
||||
if (iData > 255) iData -= (255 + 32);
|
||||
encryptedChars[i] = (byte)iData;
|
||||
}
|
||||
String result = Encoding_Ansi1252.GetString(encryptedChars);
|
||||
#if DEBUG
|
||||
// Password: mgbgt
|
||||
// To encrypt: tbmggtgbgm{btggm
|
||||
// Encrypted: 133, 116, 128, 123, 124, 138, 126, 122, 128, 135, 150, 126, 145, 133, 134, 141
|
||||
//Logger.Info(fn + "\n\tPassword: " + password + "\n\tTo encrypt: " + toBeEncrypted + "\n\tEncrypted: " + encryptedChars.Select(i => i.ToString()).ToList().Concat(", "));
|
||||
//Logger.Debug(password + ": " + result);
|
||||
#endif
|
||||
return result;
|
||||
}
|
||||
private byte[] PasswordStringToBytes (String value)
|
||||
{
|
||||
byte[] values = new byte[value.Length];
|
||||
for (int idx = 0; idx < value.Length; idx++)
|
||||
{
|
||||
char c = value[idx];
|
||||
UInt32 ui = Convert.ToUInt32(c);
|
||||
SaLogger.Info(idx + ". " + ui.ToString("x8"));
|
||||
values[idx] = (byte)(c);
|
||||
}
|
||||
return values;
|
||||
}
|
||||
private static String ByteToString (byte value)
|
||||
{
|
||||
return Encoding_Ansi1252.GetString(new[] { value });
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------
|
||||
// List parameters.
|
||||
public enum StatusCat
|
||||
{
|
||||
Any,
|
||||
EnabledOnly,
|
||||
DisabledOnly,
|
||||
}
|
||||
/// <summary>
|
||||
/// Item list parameters.
|
||||
/// </summary>
|
||||
public new class Params : SacsHierarchyItem.Params
|
||||
{
|
||||
public List<SysId> IncludedIds { get; } = new List<SysId>();
|
||||
public List<SysId> ExcludedIds { get; } = new List<SysId>();
|
||||
public SaSecurity.ItemType SecurityItemType { get; set; } = SaSecurity.ItemType.Any;
|
||||
public String Code { get; set; }
|
||||
public String CodePartial { get; set; }
|
||||
public String EmailAddress { get; set; }
|
||||
public String EmailAddressPartial { get; set; }
|
||||
public String Description { get; set; }
|
||||
public String DescriptionPartial { get; set; }
|
||||
public List<SysId> IncludedGroupIds { get; } = new List<SysId>();
|
||||
public StatusCat StatusCat { get; set; } = StatusCat.EnabledOnly;
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------
|
||||
}
|
||||
|
||||
//=======================================================================
|
||||
public static class SaSecurityItemList
|
||||
{
|
||||
public static SaSecurityItem GetSecItemById (this List<SaSecurityItem> secItems, SysId secItemId)
|
||||
{
|
||||
if (!SysId.IsValid(secItemId)) return null;
|
||||
foreach (SaSecurityItem secItem in secItems)
|
||||
{
|
||||
if (secItem.Id.Equals(secItemId))
|
||||
return secItem;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
//=======================================================================
|
||||
}
|
||||
Reference in New Issue
Block a user