/// <summary> /// Creates a dictionary of scopes information. /// </summary> /// <param name="filePath">The path of the file from Github.</param> /// <param name="cacheEntry">An optional cache entry param.</param> /// <returns>A dictionary of scopes information.</returns> private IDictionary <string, IDictionary <string, ScopeInformation> > CreateScopesInformationTables(string scopesInfoJson) { if (string.IsNullOrEmpty(scopesInfoJson)) { return(null); } ScopesInformationList scopesInformationList = JsonConvert.DeserializeObject <ScopesInformationList>(scopesInfoJson); var _delegatedScopesInfoTable = new Dictionary <string, ScopeInformation>(); var _applicationScopesInfoTable = new Dictionary <string, ScopeInformation>(); foreach (ScopeInformation delegatedScopeInfo in scopesInformationList.DelegatedScopesList) { _delegatedScopesInfoTable.Add(delegatedScopeInfo.ScopeName, delegatedScopeInfo); } foreach (ScopeInformation applicationScopeInfo in scopesInformationList.ApplicationScopesList) { _applicationScopesInfoTable.Add(applicationScopeInfo.ScopeName, applicationScopeInfo); } return(new Dictionary <string, IDictionary <string, ScopeInformation> > { { Delegated, _delegatedScopesInfoTable }, { Application, _applicationScopesInfoTable } }); }
/// <summary> /// Gets or creates the localized permissions descriptions from the cache. /// </summary> /// <param name="locale">The locale of the permissions decriptions file.</param> /// <returns>The localized instance of permissions descriptions.</returns> private async Task <IDictionary <string, IDictionary <string, ScopeInformation> > > GetOrCreatePermissionsDescriptionsAsync(string locale = DefaultLocale) { var scopesInformationDictionary = await _permissionsCache.GetOrCreateAsync($"ScopesInfoList_{locale}", async cacheEntry => { /* Localized copy of permissions descriptions * is to be seeded by only one executing thread. */ lock (_scopesLock) { /* Check whether a previous thread already seeded an * instance of the localized permissions descriptions * during the lock. */ var seededScopesInfoDictionary = _permissionsCache.Get <IDictionary <string, IDictionary <string, ScopeInformation> > >($"ScopesInfoList_{locale}"); if (seededScopesInfoDictionary == null) { var _delegatedScopesInfoTable = new Dictionary <string, ScopeInformation>(); var _applicationScopesInfoTable = new Dictionary <string, ScopeInformation>(); string relativeScopesInfoPath = FileServiceHelper.GetLocalizedFilePathSource(_permissionsContainerName, _scopesInformation, locale); string scopesInfoJson = _fileUtility.ReadFromFile(relativeScopesInfoPath).GetAwaiter().GetResult(); if (string.IsNullOrEmpty(scopesInfoJson)) { return(null); } ScopesInformationList scopesInformationList = JsonConvert.DeserializeObject <ScopesInformationList>(scopesInfoJson); foreach (ScopeInformation delegatedScopeInfo in scopesInformationList.DelegatedScopesList) { _delegatedScopesInfoTable.Add(delegatedScopeInfo.ScopeName, delegatedScopeInfo); } foreach (ScopeInformation applicationScopeInfo in scopesInformationList.ApplicationScopesList) { _applicationScopesInfoTable.Add(applicationScopeInfo.ScopeName, applicationScopeInfo); } cacheEntry.AbsoluteExpirationRelativeToNow = TimeSpan.FromHours(_defaultRefreshTimeInHours); return(new Dictionary <string, IDictionary <string, ScopeInformation> > { { Delegated, _delegatedScopesInfoTable }, { Application, _applicationScopesInfoTable } }); } /* Fetch the localized cached permissions descriptions * already seeded by previous thread. */ return(seededScopesInfoDictionary); } }); return(scopesInformationDictionary); }
/// <summary> /// Populates the delegated and application scopes information tables. /// </summary> private void SeedScopesInfoTables() { string scopesInfoJson = _fileUtility.ReadFromFile(_scopesInformation).GetAwaiter().GetResult(); if (!string.IsNullOrEmpty(scopesInfoJson)) { ScopesInformationList scopesInformationList = JsonConvert.DeserializeObject <ScopesInformationList>(scopesInfoJson); foreach (ScopeInformation delegatedScopeInfo in scopesInformationList.DelegatedScopesList) { _delegatedScopesInfoTable.Add(delegatedScopeInfo.ScopeName, delegatedScopeInfo); } foreach (ScopeInformation applicationScopeInfo in scopesInformationList.ApplicationScopesList) { _applicationScopesInfoTable.Add(applicationScopeInfo.ScopeName, applicationScopeInfo); } } }