using System; using System.Collections.Generic; using System.Linq; using Step.Model.DatabaseModels; using Step.Model.DTOModels; using static CMS_CORE_Library.DataStructures; namespace Step.Database.Controllers { public class FunctionsAccessController : IDisposable { private DatabaseContext dbCtx; public FunctionsAccessController() { // Initialize database context dbCtx = new DatabaseContext(); } public void Dispose() { // Clear database context dbCtx.Dispose(); } public FunctionAccessModel FindEnabledFunctionByName(string functionName) { return dbCtx .FunctionsAccess .Where(x => x.Name == functionName && x.Enabled == true) // Find by name and enabled functions .FirstOrDefault(); } public List GetFunctionAccess(int roleLevel) { return dbCtx .FunctionsAccess .Select(f => new DTOFunctionAccessModel() // Convert from database model to data transfer model { Id = f.FunctionAccessId, Name = f.Name, Area = f.Area, Enabled = f.Enabled, CanRead = f.ReadLevelMin < roleLevel, CanWrite = f.WriteLevelMin < roleLevel }) .ToList(); } public List GetFunctionsMappedWithPlc(List functionalityList) { return dbCtx .FunctionsAccess .ToList() // Find all function access .Select(f => new DTORuntimeFunctionAccessModel() { Id = f.FunctionAccessId, Name = f.Name, Area = f.Area, Enabled = GetIfFunctionalityIsActive(functionalityList, f.PlcId, f.Enabled) // Get new enabled data }) .ToList(); } private bool GetIfFunctionalityIsActive(List functionalityList, int id, bool functionAccessIsEnabled) { // If id is not mapped or function is false by database config return if (id == 0 || !functionAccessIsEnabled) return functionAccessIsEnabled; // Return PLC data return functionalityList[id - 1].isActive; } } }