Files
Common-VS2019/SacsStandardBase/Base/Security/SaSecurityRight.cs
T
2024-11-19 09:48:13 +00:00

76 lines
2.9 KiB
C#

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
{
//=======================================================================
/// <summary>Definition of security right as required by application</summary>
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<SaSecurityRight> securityRights, int type, String defaultValue = "")
{
return securityRights.FirstOrDefault(item => item.Type == type)?.Code ?? defaultValue;
}
public static String GetCodeForType<T>(this List<SaSecurityRight> 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<SaSecurityRight> securityRights, int type)
{
return securityRights.FirstOrDefault(item => item.Type == type)?.Desc ?? "";
}
public static String GetDescForType<T>(this List<SaSecurityRight> 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<SaSecurityRight> securityRights, String code)
{
return securityRights.FirstOrDefault(item => item.Code.EqualsCI(code))?.Type ?? 0;
}
public static String GetDescForCode(this List<SaSecurityRight> securityRights, String code)
{
return securityRights.FirstOrDefault(item => item.Code.EqualsCI(code))?.Desc ?? "";
}
// Population.
public static void Add(this List<SaSecurityRight> securityRights, int type, String code, String desc, int dataItemType = 0)
{
securityRights.Add(new SaSecurityRight(type, code, desc, dataItemType));
}
}
//=======================================================================
}