using System;
using System.Collections.Generic;
using Sarand.Net.Standard.Base.Core;
using Sarand.Net.Standard.Base.Bus;
using Sarand.Net.Standard.Base.Utils;
namespace Sarand.Net.Standard.Base.Security
{
//=======================================================================
public class SaSecurityProfile
{
//-------------------------------------------------------------------
/// Security Group definition.
public class GroupData
{
private readonly List rights = new List();
public GroupData(SaSecurityItem secGroup, List groupRights)
{
if (secGroup.ItemType != SaSecurity.ItemType.Group) throw new NotSupportedException("SaSecurityProfile.GroupData - Invalid security item type: " + secGroup.ItemType.ToString());
Id = secGroup.Id;
Code = secGroup.Code;
Desc = secGroup.Description;
rights.AddRange(groupRights);
}
protected GroupData() { }
public SysId Id { get; }
public String Code { get; }
public String Desc { get; }
// Security rights.
public List Rights { get { return new List(rights); } }
///
/// Retrieve the list of data items for which the user is explicitly granted the
/// specified security right, or where the right is explicitly denied if the flag is false.
///
public List GetDataItemsForRight(String rightCode, bool granted = true) { return rights.GetDataItemsForRight(rightCode, granted); }
/// Is the Security Right code granted access for the Data item (if specified).
public bool IsSecurityRightGranted(String rightCode, SysId dataItemId = null) { return rights.IsSecurityRightGranted(rightCode, dataItemId); }
/// Is the Security Right code denined access for the Data item (if specified).
public bool IsSecurityRightDenied(String rightCode, SysId dataItemId = null) { return rights.IsSecurityRightDenied(rightCode, dataItemId); }
}
/// Security Right definition.
public class RightData
{
public RightData(SaSecurityItem secRight, SaSecurityLink secRightLink)
{
if (secRight.ItemType != SaSecurity.ItemType.Right) throw new NotSupportedException("SaSecurityProfile.RightData - Invalid security item type: " + secRight.ItemType.ToString());
Id = secRight.Id;
Code = secRight.Code;
Desc = secRight.Description;
DataItemId = secRightLink.ItemIdContext;
Granted = (secRightLink.Status == SaStatus.Granted);
}
protected RightData() { }
public SysId Id { get; }
public String Code { get; }
public String Desc { get; }
public SysId DataItemId { get; }
public bool Granted { get; }
}
//-------------------------------------------------------------------
// Data members.
protected readonly List securityGroups = new List();
protected readonly List securityRights = new List();
//-------------------------------------------------------------------
// Construction.
internal SaSecurityProfile(SaSecurityItem secItem, List profileGroups, List profileRights, bool systemAdmin)
{
Clear();
if (secItem != null)
{
// Security item properties.
Id = secItem.Id;
SecurityItemType = secItem.ItemType;
Code = secItem.Code;
EmailAddress = secItem.EmailAddress;
Description = secItem.Description;
Location = secItem.Location;
LastLogin = secItem.LastLogin ?? DateTime.MinValue;
}
if (profileGroups != null)
securityGroups.AddRange(profileGroups);
if (profileRights != null)
securityRights.AddRange(profileRights);
SystemAdmin = systemAdmin;
}
///
/// Protected construction - for derived classes.
/// Ref. Mobile Security Profile.
///
protected SaSecurityProfile()
{
Clear();
}
//-------------------------------------------------------------------
// Properties.
public SysId Id { get; protected set; }
public SaSecurity.ItemType SecurityItemType { get; protected set; } = SaSecurity.ItemType.Unknown;
public String Code { get; protected set; }
public String EmailAddress { get; protected set; }
public String Description { get; protected set; }
public String Location { get; protected set; }
public DateTime LastLogin { get; protected set; } = DateTime.MinValue;
public List Groups { get { return new List(securityGroups); } }
public List Rights { get { return new List(securityRights); } }
public bool SystemAdmin { get; protected set; } = false;
//-------------------------------------------------------------------
// Population.
//-------------------------------------------------------------------
// Clear profile.
public void Clear()
{
Id = new SysId();
SecurityItemType = SaSecurity.ItemType.Unknown;
Code = String.Empty;
Description = String.Empty;
Location = String.Empty;
LastLogin = DateTime.MinValue;
securityGroups.Clear();
securityRights.Clear();
SystemAdmin = false;
}
//-------------------------------------------------------------------
// Utilities.
public bool IsMemberOfSecurityGroup(SysId secGroupId)
{
return securityGroups.Exists(item => item.Id.Equals(secGroupId));
}
//-------------------------------------------------------------------
///
/// Retrieve the list of data items for which the user is explicitly granted the
/// specified security right, or where the right is explicitly denied if the flag is false.
///
public List GetDataItemsForRight(String rightCode, bool granted = true)
{
return securityGroups.GetDataItemsForRight(rightCode, granted);
}
//-------------------------------------------------------------------
/// Is the Security Right code granted access for the Data item (if specified).
public bool IsSecurityRightGranted(String rightCode, SysId dataItemId = null)
{
if (SystemAdmin) return true;
return (securityGroups.IsSecurityRightGranted(rightCode, dataItemId) || securityRights.IsSecurityRightGranted(rightCode, dataItemId));
}
//-------------------------------------------------------------------
/// Is the Security Right code denined access for the Data item (if specified).
public bool IsSecurityRightDenied(String rightCode, SysId dataItemId = null)
{
if (SystemAdmin) return false;
return (securityGroups.IsSecurityRightDenied(rightCode, dataItemId) || securityRights.IsSecurityRightDenied(rightCode, dataItemId));
}
//-------------------------------------------------------------------
}
//=======================================================================
public static class SaSecurityProfileGroupDataList
{
///
/// Retrieve the list of data items for which the user is explicitly granted the
/// specified security right, or where the right is explicitly denied if the flag is false.
///
public static List GetDataItemsForRight(this List groups, String rightCode, bool granted = true)
{
HashSet dataItemIds = new HashSet();
foreach (var securityGroup in groups)
dataItemIds.AddIds(securityGroup.GetDataItemsForRight(rightCode, granted));
return new List(dataItemIds);
}
/// Is the Security Right code granted access for the Data item (if specified).
public static bool IsSecurityRightGranted(this List groups, String rightCode, SysId dataItemId = null)
{
return groups.Exists(group => group.IsSecurityRightGranted(rightCode, dataItemId));
}
/// Is the Security Right code denined access for the Data item (if specified).
public static bool IsSecurityRightDenied(this List groups, String rightCode, SysId dataItemId = null)
{
return groups.Exists(group => group.IsSecurityRightDenied(rightCode, dataItemId));
}
}
//=======================================================================
public static class SaSecurityProfileRightDataList
{
///
/// Retrieve the list of data items for which the user is explicitly granted the
/// specified security right, or where the right is explicitly denied if the flag is false.
///
public static List GetDataItemsForRight(this List rights, String rightCode, bool granted = true)
{
HashSet dataItemIds = new HashSet();
foreach (var securityRight in rights.FindAll(x => x.Code.EqualsCI(rightCode) && x.Granted == granted))
dataItemIds.AddId(securityRight.DataItemId);
return new List(dataItemIds);
}
/// Is the Security Right code granted access for the Data item (if specified).
public static bool IsSecurityRightGranted(this List rights, String rightCode, SysId dataItemId = null)
{
foreach (var userSecurityRight in rights.FindAll(item => item.Code.EqualsCI(rightCode)))
{
if (SysId.ToInt(dataItemId) != SysId.ToInt(userSecurityRight.DataItemId)) continue;
return userSecurityRight.Granted;
}
return false;
}
/// Is the Security Right code denined access for the Data item (if specified).
public static bool IsSecurityRightDenied(this List rights, String rightCode, SysId dataItemId = null)
{
foreach (var userSecurityRight in rights.FindAll(item => item.Code.EqualsCI(rightCode)))
{
if (SysId.ToInt(dataItemId) != SysId.ToInt(userSecurityRight.DataItemId)) continue;
return !userSecurityRight.Granted;
}
return false;
}
}
//=======================================================================
}