public async Task <PartialViewResult> Search(PlatformConstant[] platform, int[] environment, PciScopeConstant[] securityClass,
                                              NodeComplianceSearchTypeConstant[] compliance, NodeSearchTypeConstant searchType, string search, bool?hideProductExclusions, int?pageIndex, bool?showButtons)
 {
     return(PartialView(await _nodeFactory.Search(platform, environment, securityClass, compliance, searchType, search, hideProductExclusions.GetValueOrDefault(), UserSecurity, pageIndex.GetValueOrDefault(), showButtons.GetValueOrDefault())));
 }
Exemplo n.º 2
0
        public async Task <PartialViewResult> Search(PlatformConstant[] platform, int[] environment, PciScopeConstant[] securityClass,
                                                     NodeComplianceSearchTypeConstant[] compliance, NodeSearchTypeConstant searchType, string search, bool?hideProductExclusions, int?pageIndex, bool?showButtons)
        {
            var timeZoneCookie = Request.Cookies["_timeZoneOffset"];

            if (timeZoneCookie != null)
            {
                ViewBag.TimeOffset = Convert.ToInt32(timeZoneCookie);
            }

            return(PartialView(await _nodeFactory.Search(platform, environment, securityClass, compliance, searchType, search, hideProductExclusions.GetValueOrDefault(), UserSecurity, pageIndex.GetValueOrDefault(), showButtons.GetValueOrDefault())));
        }
        public async Task <NodeSearchResults> Search(PlatformConstant[] platform,
                                                     int[] environment, PciScopeConstant[] pciScope,
                                                     NodeComplianceSearchTypeConstant[] compliance, NodeSearchTypeConstant searchType, string query,
                                                     bool hideProductExclusions, IUserSecurity employeeSecurity, int pageIndex, bool showButtons)
        {
            var       valid = !string.IsNullOrWhiteSpace(query);
            const int take  = 30;
            var       q     = _ctx.Nodes.AsNoTracking().Active();

            if (hideProductExclusions)
            {
                q = q.ProductIsNotExlcuded();
            }

            if (platform?.Length > 0)
            {
                q = q.ByPlatforms(platform);

                valid = true;
            }

            if (environment?.Length > 0)
            {
                q     = q.ForEnvironments(environment);
                valid = true;
            }

            if (pciScope?.Length > 0)
            {
                q     = q.ByPciScopes(pciScope);
                valid = true;
            }

            var complianceFlags = compliance.ConvertToFlag();

            if (complianceFlags.HasValue)
            {
                var cf = complianceFlags.Value;

                var unassigned         = cf.HasFlag(NodeComplianceSearchTypeConstant.Unassigned);
                var assigned           = cf.HasFlag(NodeComplianceSearchTypeConstant.Assigned);
                var complianceStatuses = new List <ComplianceStatusConstant>();

                if (cf.HasFlag(NodeComplianceSearchTypeConstant.NotReporting))
                {
                    complianceStatuses.Add(ComplianceStatusConstant.NotFound);
                }
                if (cf.HasFlag(NodeComplianceSearchTypeConstant.Failing))
                {
                    complianceStatuses.Add(ComplianceStatusConstant.Failed);
                }
                if (cf.HasFlag(NodeComplianceSearchTypeConstant.Passing))
                {
                    complianceStatuses.Add(ComplianceStatusConstant.Succeeded);
                }

                if (complianceStatuses.Any())
                {
                    assigned = true;
                    q        = q.ByComplianceStatuses(complianceStatuses.ToArray());
                }

                if (unassigned && !assigned)
                {
                    q = q.Unassigned();
                }
                else if (assigned && !unassigned)
                {
                    q = q.Assigned();
                }

                valid = true;
            }

            switch (searchType)
            {
            case NodeSearchTypeConstant.Mine:
                if (!string.IsNullOrWhiteSpace(query))
                {
                    q = q.Where(p => EF.Functions.Like(p.Fqdn, $"{query}%"));
                }
                q     = q.Where(p => p.OwnerEmployeeId == employeeSecurity.EmployeeId).OrderBy(p => p.Fqdn);
                valid = true;
                break;

            case NodeSearchTypeConstant.Fqdn:
                if (!string.IsNullOrWhiteSpace(query))
                {
                    q = q.Where(p => EF.Functions.Like(p.Fqdn, $"{query}%"));
                }

                q = q.OrderBy(p => p.Fqdn);
                break;

            case NodeSearchTypeConstant.Owner:
                if (!string.IsNullOrWhiteSpace(query))
                {
                    var empIds = await q.Select(p => p.Owner).Search(query).Select(p => p.Id).Distinct()
                                 .ToArrayAsync();

                    q = q.Where(p => empIds.Contains(p.OwnerEmployeeId));
                }

                q = q.OrderBy(p => p.Owner.PreferredName ?? p.Owner.FirstName).ThenBy(p => p.Owner.LastName)
                    .ThenBy(p => p.Fqdn);
                break;

            case NodeSearchTypeConstant.Director:
                if (!string.IsNullOrWhiteSpace(query))
                {
                    var empIds =
                        (await q.Where(p => p.Owner.ReportingDirectorId != null)
                         .Select(p => p.Owner.ReportingDirector).Search(query).Select(p => p.Id).Distinct()
                         .ToArrayAsync()).Cast <long?>().ToArray();
                    q = q.Where(p => empIds.Contains(p.Owner.ReportingDirectorId));
                }

                q = q.OrderBy(p => p.Owner.PreferredName ?? p.Owner.FirstName).ThenBy(p => p.Owner.LastName)
                    .ThenBy(p => p.Fqdn);
                break;

            case NodeSearchTypeConstant.Product:
                if (!string.IsNullOrWhiteSpace(query))
                {
                    var sw = $"{query}%";
                    q = q.Where(p =>
                                EF.Functions.Like(p.Product.Name, sw) || EF.Functions.Like(p.ProductCode, sw) ||
                                EF.Functions.Like(p.Product.Name, sw) || EF.Functions.Like(p.ProductCode, sw) ||
                                EF.Functions.Like(p.Function.Name, sw));
                }

                q = q.OrderBy(p => p.Product.Name).ThenBy(p => p.Function.Name).ThenBy(p => p.Fqdn);
                break;

            case NodeSearchTypeConstant.OsSpec:
                q = q.Where(p => p.BuildSpecificationId.HasValue);
                if (!string.IsNullOrWhiteSpace(query))
                {
                    var contains = $"%{query}%";
                    q = q.Where(p => EF.Functions.Like(p.BuildSpecification.Parent.Name, contains));
                }

                q = q.OrderBy(p => p.BuildSpecification.Name).ThenBy(p => p.Fqdn);
                break;

            case NodeSearchTypeConstant.AppSpec:
                q = q.Where(p => p.BuildSpecificationId.HasValue);
                if (!string.IsNullOrWhiteSpace(query))
                {
                    var contains = $"%{query}%";
                    q = q.Where(p => EF.Functions.Like(p.BuildSpecification.Name, contains));
                }

                q = q.OrderBy(p => p.BuildSpecification.Name).ThenBy(p => p.Fqdn);
                break;

            default:
                throw new ArgumentException(@"NodeSearchType not supported.", nameof(searchType));
            }

            if (!valid)
            {
                q = q.Where(p => 1 == 2);
            }

            var matchCount = await q.CountAsync();

            var skip    = take * pageIndex;
            var results = matchCount > 0 ? await ToResult(q.Skip(skip).Take(take), showButtons) : new NodeSearchResult[0];

            var displayCount = skip + results.Length;

            return(new NodeSearchResults(results, matchCount, displayCount));
        }