using System; using System.Collections.Generic; using System.Linq; using System.Web; using Weee.DAL; using Weee.Models; using System.Data.Entity; using System.Data.Entity.Infrastructure; using NLog; namespace Weee.Service { //business logic Service Layer public class WeeeDataAuthorizeService { protected WeeeDataContext _db; private bool isInitialized { get { return !(CurrentCompany == null); } } protected string loggedInUserId = null; protected bool loggedInUserCompanyAdmin = false; //private Guid? _loggedInCompanyUid = null; protected Company CurrentCompany = null; protected UserAccountType UserAccountType = null;//CFT-54 protected List AuthorizedFabs { get { if (CurrentCompany.CompanyType == typeof(CertificationCompany)) return new List(); return _db.Fabs.Where(x => x.CompanyID == CurrentCompany.ID).Select(x => x.ID).ToList(); } } protected List AuthorizedProducts { get { if (CurrentCompany.CompanyType == typeof(CertificationCompany)) return new List(); return _db.Products.Where(x => x.CompanyID == CurrentCompany.ID).Select(x => x.ID).ToList(); } } protected List AuthorizedLCAs { get { if (CurrentCompany.CompanyType == typeof(CertificationCompany)) return _db.LCAs.Where(x => x.VerifierCompanyID == CurrentCompany.ID).Select(x => x.ID).ToList(); return _db.LCAs.Where(x => x.OwnerID == CurrentCompany.ID).Select(x => x.ID).ToList(); } } public WeeeDataAuthorizeService(WeeeDataContext db) { _db = db; } public WeeeDataContext GetDbContext() { return _db; } public void Initialize(string UserId) { Logger log = NLog.LogManager.GetCurrentClassLogger(); try { if (isInitialized) return; loggedInUserId = UserId; var qry = (from a in _db.Users where a.Id == UserId select a.IsCompanyAdmin).FirstOrDefault(); if (qry != null) loggedInUserCompanyAdmin = qry; //log.Info($"WeeeDataAuthorizeService Initialize UserId={UserId}"); CurrentCompany = GetUserContext().Company; var Id = GetUserContext().Id; var userTypeInfo = _db.UserAccountType.FirstOrDefault(ut => ut.UserId == Id && ut.Enabled == true); UserAccountType = userTypeInfo; } catch(Exception ex) { log.Error(ex); log.Error(CScommon.Exceptions.inner(ex)); log.Error(ex.StackTrace); } } public void DisableProxyAndLazyLoad() { _db.Configuration.ProxyCreationEnabled = false; _db.Configuration.LazyLoadingEnabled = false; } //public void Initialize(Guid CompanyUid,string UserName) //{ // if (isInitialized) return; // _loggedInCompanyUid = CompanyUid; // CurrentCompany = _db.Companies.Where(x => x.UID == CompanyUid).Single(); //} public User GetUserContext() { return _db.Users.Include(x=>x.Company).AsNoTracking().First(x => x.Id == loggedInUserId); } public void Dispose() { _db.Dispose(); } } }