public static async Task <List <Role> > FindRolesAsync(this string systemID, string parentID, CancellationToken cancellationToken = default, bool updateCache = true) { if (string.IsNullOrWhiteSpace(systemID)) { return(new List <Role>()); } var filter = RoleProcessor.GetRolesFilter(systemID, parentID); var sort = Sorts <Role> .Ascending("Title"); var roles = await Role.FindAsync(filter, sort, 0, 1, Extensions.GetCacheKey(filter, sort, 0, 1), cancellationToken).ConfigureAwait(false); await roles.ForEachAsync((role, token) => role.SetAsync(updateCache, token), cancellationToken).ConfigureAwait(false); return(roles); }
internal static async Task <JObject> SearchRolesAsync(this RequestInfo requestInfo, bool isSystemAdministrator = false, CancellationToken cancellationToken = default) { // prepare var request = requestInfo.GetRequestExpando(); var query = request.Get <string>("FilterBy.Query"); var filter = request.Get <ExpandoObject>("FilterBy")?.ToFilterBy <Role>() ?? Filters <Role> .And(); if (filter is FilterBys <Role> ) { if (!string.IsNullOrWhiteSpace(query)) { var index = (filter as FilterBys <Role>).Children.FindIndex(exp => (exp as FilterBy <Role>).Attribute.IsEquals("ParentID")); if (index > -1) { (filter as FilterBys <Role>).Children.RemoveAt(index); } } else if ((filter as FilterBys <Role>).Children.FirstOrDefault(exp => (exp as FilterBy <Role>).Attribute.IsEquals("ParentID")) == null) { (filter as FilterBys <Role>).Children.Add(Filters <Role> .IsNull("ParentID")); } } var sort = string.IsNullOrWhiteSpace(query) ? request.Get <ExpandoObject>("SortBy")?.ToSortBy <Role>() ?? Sorts <Role> .Ascending("Title") : null; var pagination = request.Get <ExpandoObject>("Pagination")?.GetPagination() ?? new Tuple <long, int, int, int>(-1, 0, 20, 1); var pageSize = pagination.Item3; var pageNumber = pagination.Item4; // get organization var organizationID = filter.GetValue("SystemID") ?? requestInfo.GetParameter("SystemID") ?? requestInfo.GetParameter("x-system-id") ?? requestInfo.GetParameter("OrganizationID"); var organization = await(organizationID ?? "").GetOrganizationByIDAsync(cancellationToken).ConfigureAwait(false); if (organization == null) { throw new InformationExistedException("The organization is invalid"); } // check permission var gotRights = isSystemAdministrator || requestInfo.Session.User.IsViewer(null, null, organization, requestInfo.CorrelationID); if (!gotRights) { throw new AccessDeniedException(); } // process cache var addChildren = "true".IsEquals(requestInfo.GetHeaderParameter("x-children")); var cachedJson = string.IsNullOrWhiteSpace(query) && !addChildren ? await Utility.Cache.GetAsync <string>(Extensions.GetCacheKeyOfObjectsJson(filter, sort, pageSize, pageNumber), cancellationToken).ConfigureAwait(false) : null; if (!string.IsNullOrWhiteSpace(cachedJson)) { return(JObject.Parse(cachedJson)); } // prepare pagination var totalRecords = pagination.Item1 > -1 ? pagination.Item1 : -1; if (totalRecords < 0) { totalRecords = string.IsNullOrWhiteSpace(query) ? await Role.CountAsync(filter, Extensions.GetCacheKeyOfTotalObjects(filter, sort), cancellationToken).ConfigureAwait(false) : await Role.CountAsync(query, filter, cancellationToken).ConfigureAwait(false); } var totalPages = new Tuple <long, int>(totalRecords, pageSize).GetTotalPages(); if (totalPages > 0 && pageNumber > totalPages) { pageNumber = totalPages; } // search var objects = totalRecords > 0 ? string.IsNullOrWhiteSpace(query) ? await Role.FindAsync(filter, sort, pageSize, pageNumber, Extensions.GetCacheKey(filter, sort, pageSize, pageNumber), cancellationToken).ConfigureAwait(false) : await Role.SearchAsync(query, filter, pageSize, pageNumber, cancellationToken).ConfigureAwait(false) : new List <Role>(); // build result pagination = new Tuple <long, int, int, int>(totalRecords, totalPages, pageSize, pageNumber); if (addChildren) { await objects.Where(role => role._childrenIDs == null).ForEachAsync((role, _) => role.FindChildrenAsync(cancellationToken), cancellationToken, true, false).ConfigureAwait(false); } var response = new JObject { { "FilterBy", filter.ToClientJson(query) }, { "SortBy", sort?.ToClientJson() }, { "Pagination", pagination.GetPagination() }, { "Objects", objects.Select(role => role.ToJson(addChildren, false)).ToJArray() } }; // update cache if (string.IsNullOrWhiteSpace(query) && !addChildren) { await Utility.Cache.SetAsync(Extensions.GetCacheKeyOfObjectsJson(filter, sort, pageSize, pageNumber), response.ToString(Formatting.None), cancellationToken).ConfigureAwait(false); } // response return(response); }
internal static async Task ClearCacheAsync(this Organization organization, CancellationToken cancellationToken, string correlationID = null, bool clearObjectsCache = true, bool clearRelatedDataCache = true, bool clearRelatedHtmlCache = true, bool doRefresh = true) { // clear cache of home desktop (html) var tasks = new List <Task> { organization.ClearRelatedCacheAsync(cancellationToken, correlationID, clearRelatedDataCache, clearRelatedHtmlCache, false) }; // clear cache of objects if (clearObjectsCache) { // clear cache of expressions var expressions = await Expression.FindAsync(Filters <Expression> .And(Filters <Expression> .Equals("SystemID", organization.ID)), null, 0, 1, null, cancellationToken).ConfigureAwait(false); tasks = tasks.Concat(expressions.Select(expression => expression.ClearCacheAsync(cancellationToken, correlationID, clearRelatedDataCache, clearRelatedHtmlCache, doRefresh))).ToList(); // clear cache of roles var roles = await Role.FindAsync(Filters <Role> .And(Filters <Role> .Equals("SystemID", organization.ID)), null, 0, 1, null, cancellationToken).ConfigureAwait(false); tasks = tasks.Concat(roles.Select(role => role.ClearCacheAsync(cancellationToken, correlationID, clearRelatedDataCache))).ToList(); // clear cache of modules, content-types and business objects tasks = tasks.Concat(organization.Modules.Select(module => module.ClearCacheAsync(cancellationToken, correlationID, clearObjectsCache, clearRelatedDataCache, clearRelatedHtmlCache, doRefresh))).ToList(); // clear cache of desktops var desktops = await Desktop.FindAsync(Filters <Desktop> .And(Filters <Desktop> .Equals("SystemID", organization.ID)), null, 0, 1, null, cancellationToken).ConfigureAwait(false); tasks = tasks.Concat(desktops.Select(desktop => desktop.ClearCacheAsync(cancellationToken, correlationID, clearRelatedDataCache, clearRelatedHtmlCache, doRefresh))).ToList(); // clear cache of sites tasks = tasks.Concat(organization.Sites.Select(site => site.ClearCacheAsync(cancellationToken, correlationID, clearRelatedDataCache, clearRelatedHtmlCache, doRefresh))).ToList(); } // clear cache of the organization tasks = tasks.Concat(new[] { Utility.Cache.RemoveAsync(organization.Remove(), cancellationToken), Utility.RTUService.SendInterCommunicateMessageAsync(new CommunicateMessage(ServiceBase.ServiceComponent.ServiceName) { Type = $"{organization.GetObjectName()}#Delete", Data = organization.ToJson(), ExcludedNodeID = Utility.NodeID }, cancellationToken), Utility.WriteCacheLogs ? Utility.WriteLogAsync(correlationID, $"Clear cache of an organization [{organization.Title} - ID: {organization.ID}]", ServiceBase.ServiceComponent.CancellationToken, "Caches") : Task.CompletedTask }).ToList(); await Task.WhenAll(tasks).ConfigureAwait(false); // re-load organization & sites/modules/content-types organization = await organization.ID.GetOrganizationByIDAsync(cancellationToken).ConfigureAwait(false); await Task.WhenAll( organization.FindSitesAsync(cancellationToken, false), organization.FindModulesAsync(cancellationToken, false) ).ConfigureAwait(false); await organization.Modules.ForEachAsync(async module => { await module.FindContentTypesAsync(cancellationToken, false).ConfigureAwait(false); await Task.WhenAll ( Utility.WriteCacheLogs ? Utility.WriteLogAsync(correlationID, $"The module was reloaded when all cache were clean\r\n{module.ToJson()}", cancellationToken, "Caches") : Task.CompletedTask, Utility.WriteCacheLogs ? Utility.WriteLogAsync(correlationID, $"The content-types were reloaded when all cache were clean\r\n{module.ContentTypes.Select(contentType => contentType.ToJson().ToString(Formatting.Indented)).Join("\r\n")}", cancellationToken, "Caches") : Task.CompletedTask ).ConfigureAwait(false); }, true, false).ConfigureAwait(false); await Task.WhenAll( organization.SetAsync(false, true, cancellationToken), Task.WhenAll(organization.Sites.Select(site => site.SetAsync(false, true, cancellationToken))), Task.WhenAll(organization.Modules.Select(module => module.SetAsync(true, cancellationToken))), Task.WhenAll(organization.Modules.Select(module => Task.WhenAll(module.ContentTypes.Select(contentType => contentType.SetAsync(true, cancellationToken))))) ).ConfigureAwait(false); // re-load and refresh the home desktop var homedesktop = await Desktop.GetAsync <Desktop>(organization.HomeDesktopID, cancellationToken).ConfigureAwait(false); await Task.WhenAll( homedesktop.FindChildrenAsync(cancellationToken, false), homedesktop.FindPortletsAsync(cancellationToken, false) ).ConfigureAwait(false); await homedesktop.SetAsync(false, true, cancellationToken).ConfigureAwait(false); await Task.WhenAll ( $"{Utility.PortalsHttpURI}/~{organization.Alias}/".RefreshWebPageAsync(0, correlationID, $"Refresh the home desktop when all cache of an organization were clean [{organization.Title} - ID: {organization.ID}]"), Utility.WriteCacheLogs?Utility.WriteLogAsync(correlationID, $"The organization was reloaded when all cache were clean\r\n{organization.ToJson()}", cancellationToken, "Caches") : Task.CompletedTask ).ConfigureAwait(false); }