using Sarand.Net.Standard.Base.Core; using Sarand.Net.Standard.Base.Utils; using System; using System.Collections.Generic; using System.Linq; namespace Sarand.Net.Standard.Base.Security { //======================================================================= /// Definition of security right as required by application public class SaSecurityRight { //------------------------------------------------------------------- // Construction. public SaSecurityRight(int type, String code, String desc, int dataItemType = 0) { Type = type; Code = code; Desc = desc; DataItemType = dataItemType; } //------------------------------------------------------------------- // Properties. public int Type { get; } public String Code { get; } public String Desc { get; } public int DataItemType { get; } //------------------------------------------------------------------- } //======================================================================= public static class SaSecurityRightList { // Conversion by type. public static String GetCodeForType(this List securityRights, int type, String defaultValue = "") { return securityRights.FirstOrDefault(item => item.Type == type)?.Code ?? defaultValue; } public static String GetCodeForType(this List values, T type, String defaultValue = "") where T : struct, IConvertible { if (!(type is Enum)) throw new ArgumentException("SaSecurityRightList.GetCodeForType parameter must be an enumerated type. Type supplied: " + type.GetType().Name); return values.GetCodeForType((int)(type as IConvertible), defaultValue); } public static String GetDescForType(this List securityRights, int type) { return securityRights.FirstOrDefault(item => item.Type == type)?.Desc ?? ""; } public static String GetDescForType(this List values, T type, String defaultValue = "") where T : struct, IConvertible { if (!(type is Enum)) throw new ArgumentException("SaSecurityRightList.GetDescForType parameter must be an enumerated type. Type supplied: " + type.GetType().Name); return values.GetDescForType((int)(type as IConvertible), defaultValue); } // Conversion by code. public static int GetTypeForCode(this List securityRights, String code) { return securityRights.FirstOrDefault(item => item.Code.EqualsCI(code))?.Type ?? 0; } public static String GetDescForCode(this List securityRights, String code) { return securityRights.FirstOrDefault(item => item.Code.EqualsCI(code))?.Desc ?? ""; } // Population. public static void Add(this List securityRights, int type, String code, String desc, int dataItemType = 0) { securityRights.Add(new SaSecurityRight(type, code, desc, dataItemType)); } } //======================================================================= }