public AppUserSecurityContext(CalculatorAppContext calculatorAppContext, ApiMemoryCache memoryCache)
        {
            ODSContext            = calculatorAppContext.ODSContext;
            MemoryCache           = memoryCache.Cache;
            UserAuthorizationDims = new List <UserAuthorizationDim>();

            string userEmailAddress = string.Empty;
            var    userContext      = calculatorAppContext.UserContext;

            if (userContext != null && userContext.HttpContext != null)
            {
                var identities = userContext.HttpContext.User.Identities.ToList();//TODO : handle null exceptions
                if (identities.Any())
                {
                    userEmailAddress = identities.FirstOrDefault().Name;
                }
            }

            if (!string.IsNullOrEmpty(userEmailAddress))
            {
                string cacheKey = "UserAuthorizationDims:" + userEmailAddress;
                List <UserAuthorizationDim> _userAuthorizationDims;
                if (MemoryCache.TryGetValue(cacheKey, out _userAuthorizationDims))
                {
                    UserAuthorizationDims = _userAuthorizationDims;
                }
                else
                {
                    UserAuthorizationDims = (from uad in ODSContext.UserAuthorizationDims.AsNoTracking()
                                             join ud in ODSContext.UserDims.AsNoTracking()
                                             on uad.UserKey equals ud.UserKey
                                             where ud.UserEmail.Equals(userEmailAddress)
                                             select new UserAuthorizationDim
                    {
                        UserKey = uad.UserKey,
                        UserScope = uad.UserScope,
                        SectionPermission = uad.SectionPermission,
                        StudentPermission = uad.StudentPermission,
                        SchoolPermission = uad.SchoolPermission,
                        DistrictId = uad.DistrictId
                    })
                                            .ToList();

                    var cacheEntryOptions = new MemoryCacheEntryOptions()
                                            .SetSize(20)//Size amount
                                            .SetPriority(CacheItemPriority.High)
                                            .SetSlidingExpiration(TimeSpan.FromMinutes(15))
                                            .SetAbsoluteExpiration(TimeSpan.FromMinutes(60));
                    MemoryCache.Set(cacheKey, UserAuthorizationDims, cacheEntryOptions);
                }
            }
        }
 public CalculatorAppContext(ODSContext oDSContext,
                             ApiMemoryCache memoryCache,
                             IHttpContextAccessor contextAccessor,
                             IOptions <AppConfiguration> appConfiguration,
                             IStudentDataRepository dataRepository,
                             IGradingScaleRepository gradingScaleRepository)
 {
     MemoryCache            = memoryCache.Cache;
     ODSContext             = oDSContext;
     UserContext            = contextAccessor;
     AppConfiguration       = appConfiguration;
     StudentDataRepository  = dataRepository;
     GradingScaleRepository = gradingScaleRepository;
 }
Example #3
0
 public GradingScaleRepository(ODSContext oDSContext,
                               ApiMemoryCache memoryCache)
 {
     _context     = oDSContext;
     _memoryCache = memoryCache.Cache;
 }
 public StudentDataRepository(ODSContext oDSContext,
                              ApiMemoryCache memoryCache)
 {
     _context     = oDSContext;
     _memoryCache = memoryCache.Cache;
 }