Beispiel #1
0
        private async Task <Dictionary <int, string> > GetProjectNamesForUserMapping(HashSet <int> projectIds, int?userId)
        {
            var projectNameIdDictionary = (await _artifactRepository.GetProjectNameByIdsAsync(projectIds)).ToDictionary(x => x.ItemId, x => x.Name);

            if (!userId.HasValue)
            {
                return(projectNameIdDictionary);
            }

            var projectIdPermissions = new List <KeyValuePair <int, RolePermissions> >();

            int iterations = (int)Math.Ceiling((double)projectIds.Count / 50);

            for (int i = 0; i < iterations; i++)
            {
                var chunkProjectIds = projectIds.Skip(i * 50).Take(50);
                var newDictionary   = await _artifactPermissionsRepository.GetArtifactPermissions(chunkProjectIds, userId.Value);

                projectIdPermissions.AddRange(newDictionary.ToList());
            }

            var projectIdPermissionsDictionary = projectIdPermissions.ToDictionary(x => x.Key, x => x.Value);

            foreach (int projectId in projectIds)
            {
                if (!projectIdPermissionsDictionary.ContainsKey(projectId) || !projectIdPermissionsDictionary[projectId].HasFlag(RolePermissions.Read))
                {
                    projectNameIdDictionary[projectId] = ServiceConstants.NoPermissions;
                }
            }

            return(projectNameIdDictionary);
        }
        public async Task <SuggestionsSearchResult> GetSemanticSearchSuggestions(
            SemanticSearchSuggestionParameters parameters,
            GetSemanticSearchSuggestionsAsyncDelegate getSuggestionsAsyncDelegate)
        {
            var artifactId = parameters.ArtifactId;
            var userId     = parameters.UserId;

            if (artifactId <= 0)
            {
                throw new BadRequestException("Please specify a valid artifact id");
            }

            var artifactDetails = await _artifactRepository.GetArtifactBasicDetails(artifactId, userId);

            if (artifactDetails == null)
            {
                throw new ResourceNotFoundException(
                          I18NHelper.FormatInvariant("Artifact Id {0} is not found", artifactId), ErrorCodes.ArtifactNotFound);
            }
            if (artifactDetails.LatestDeleted || artifactDetails.DraftDeleted)
            {
                throw new ResourceNotFoundException(
                          I18NHelper.FormatInvariant("Artifact Id {0} is deleted", artifactId), ErrorCodes.ArtifactNotFound);
            }

            var itemTypePredefined = (ItemTypePredefined)artifactDetails.PrimitiveItemTypePredefined;

            if (isInvalidSemanticSearchArtifactType(itemTypePredefined))
            {
                throw new BadRequestException(
                          I18NHelper.FormatInvariant(
                              $"Artifact type '{itemTypePredefined}' is not supported for semantic search"));
            }

            if (artifactDetails.ArtifactId != artifactId && artifactDetails.ItemId == artifactId)
            {
                throw new BadRequestException("Subartifacts are not supported for semantic search");
            }

            var currentProject =
                (await _artifactRepository.GetProjectNameByIdsAsync(new[] { artifactDetails.ProjectId }))
                .FirstOrDefault();

            var permissions = await _artifactPermissionsRepository.GetArtifactPermissions(new[] { artifactId }, userId);

            RolePermissions permission;

            if (!permissions.TryGetValue(artifactId, out permission) || !permission.HasFlag(RolePermissions.Read))
            {
                throw new AuthorizationException("User is not authorized to view artifact");
            }

            var suggestionsSearchResult = new SuggestionsSearchResult();

            suggestionsSearchResult.SourceId          = artifactId;
            suggestionsSearchResult.SourceProjectName = currentProject?.Name;

            var isInstanceAdmin = await _usersRepository.IsInstanceAdmin(false, userId);

            var accessibleProjectIds = isInstanceAdmin
                ? new List <int>()
                : await _semanticSearchRepository.GetAccessibleProjectIds(userId);

            var searchEngineParameters = new SearchEngineParameters(artifactId, userId, isInstanceAdmin,
                                                                    accessibleProjectIds.ToHashSet());

            var suggestedArtifactResults = await getSuggestionsAsyncDelegate(searchEngineParameters);

            var artifactIds = suggestedArtifactResults.Select(s => s.Id);

            var resultArtifactPermissions = await _artifactPermissionsRepository.GetArtifactPermissions(artifactIds, userId);

            suggestedArtifactResults.ForEach((artifact) =>
            {
                if (resultArtifactPermissions.ContainsKey(artifact.Id))
                {
                    artifact.HasReadPermission = resultArtifactPermissions[artifact.Id].HasFlag(RolePermissions.Read);
                }
            });

            // Get list of some basic artifact details from the list of returned ids.
            suggestionsSearchResult.Items = suggestedArtifactResults;

            return(suggestionsSearchResult);
        }