Example #1
0
        private async Task <Collection> GetCollectionAsync(
            int collectionId, int userId, IDbTransaction transaction = null)
        {
            var basicDetails = await _artifactRepository.GetArtifactBasicDetails(collectionId, userId, transaction);

            if (basicDetails == null || basicDetails.DraftDeleted || basicDetails.LatestDeleted)
            {
                throw CollectionsExceptionHelper.NotFoundException(collectionId);
            }

            if (basicDetails.PrimitiveItemTypePredefined != (int)ItemTypePredefined.ArtifactCollection)
            {
                throw CollectionsExceptionHelper.InvalidTypeException(collectionId);
            }

            var permissions = await _artifactPermissionsRepository.GetArtifactPermissions(
                collectionId, userId, transaction : transaction);

            RolePermissions collectionPermissions;

            if (!permissions.TryGetValue(collectionId, out collectionPermissions) ||
                !collectionPermissions.HasFlag(RolePermissions.Read))
            {
                throw CollectionsExceptionHelper.NoAccessException(collectionId);
            }

            return(new Collection(
                       basicDetails.ArtifactId,
                       basicDetails.ProjectId,
                       basicDetails.LockedByUserId,
                       collectionPermissions));
        }
Example #2
0
        private async Task <ArtifactBasicDetails> GetReviewInfoAsync(int reviewId, int userId, int revisionId = int.MaxValue)
        {
            var artifactInfo = await _artifactRepository.GetArtifactBasicDetails(reviewId, userId);

            if (artifactInfo == null)
            {
                throw ReviewsExceptionHelper.ReviewNotFoundException(reviewId, revisionId);
            }

            if (revisionId == int.MaxValue && (artifactInfo.DraftDeleted || artifactInfo.LatestDeleted))
            {
                throw ReviewsExceptionHelper.ReviewNotFoundException(reviewId, revisionId);
            }

            if (artifactInfo.PrimitiveItemTypePredefined != (int)ItemTypePredefined.ArtifactReviewPackage)
            {
                throw new BadRequestException(I18NHelper.FormatInvariant(ErrorMessages.ArtifactIsNotReview, reviewId), ErrorCodes.BadRequest);
            }

            return(artifactInfo);
        }
Example #3
0
        public async Task <SearchArtifactsResult> Search(int scopeId, Pagination pagination, ScopeType scopeType, bool includeDrafts, int userId, IDbTransaction transaction = null)
        {
            var artifactBasicDetails = await _sqlArtifactRepository.GetArtifactBasicDetails(scopeId, userId, transaction);

            if (artifactBasicDetails == null)
            {
                var errorMessage = I18NHelper.FormatInvariant(ErrorMessages.ArtifactNotFound, scopeId);
                throw new ResourceNotFoundException(errorMessage, ErrorCodes.ResourceNotFound);
            }

            if (artifactBasicDetails.PrimitiveItemTypePredefined != (int)ItemTypePredefined.ArtifactCollection)
            {
                throw new NotImplementedException(ErrorMessages.NotImplementedForNonCollectionArtifact);
            }

            if (scopeType == ScopeType.Descendants)
            {
                throw new NotImplementedException(ErrorMessages.NotImplementedForDescendantsScopeType);
            }

            return(await _searchEngineRepository.GetCollectionContentSearchArtifactResults(scopeId, pagination, includeDrafts, userId, transaction));
        }
        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);
        }
Example #5
0
        public async Task <VersionControlArtifactInfo> GetVersionControlArtifactInfoAsync(int itemId, int?baselineId, int userId)
        {
            var artifactBasicDetails = await _artifactRepository.GetArtifactBasicDetails(itemId, userId);

            if (artifactBasicDetails == null)
            {
                var errorMessage = I18NHelper.FormatInvariant("Item (Id:{0}) is not found.", itemId);
                throw new ResourceNotFoundException(errorMessage, ErrorCodes.ResourceNotFound);
            }

            // Always getting permissions for the Head version of an artifact.
            // But, just in case, RevisionId and AddDrafts are available in ArtifactBasicDetails.
            var itemIdsPermissions = await _artifactPermissionsRepository.GetArtifactPermissions(Enumerable.Repeat(artifactBasicDetails.ArtifactId, 1), userId);

            if (!itemIdsPermissions.ContainsKey(artifactBasicDetails.ArtifactId) || !itemIdsPermissions[artifactBasicDetails.ArtifactId].HasFlag(RolePermissions.Read))
            {
                var errorMessage = I18NHelper.FormatInvariant("User does not have permissions for Artifact (Id:{0}).", artifactBasicDetails.ArtifactId);
                throw new AuthorizationException(errorMessage, ErrorCodes.UnauthorizedAccess);
            }

            var artifactInfo = new VersionControlArtifactInfo
            {
                Id            = artifactBasicDetails.ArtifactId,
                SubArtifactId = artifactBasicDetails.ArtifactId != artifactBasicDetails.ItemId
                    ? (int?)artifactBasicDetails.ItemId
                    : null,
                Name           = artifactBasicDetails.Name,
                ProjectId      = artifactBasicDetails.ProjectId,
                ParentId       = artifactBasicDetails.ParentId,
                OrderIndex     = artifactBasicDetails.OrderIndex,
                ItemTypeId     = artifactBasicDetails.ItemTypeId,
                Prefix         = artifactBasicDetails.Prefix,
                PredefinedType = (ItemTypePredefined)artifactBasicDetails.PrimitiveItemTypePredefined,
                Version        = artifactBasicDetails.VersionIndex != null && artifactBasicDetails.VersionIndex.Value <= 0 ? -1 : artifactBasicDetails.VersionIndex,
                VersionCount   = artifactBasicDetails.VersionsCount,
                IsDeleted      = artifactBasicDetails.DraftDeleted || artifactBasicDetails.LatestDeleted,
                HasChanges     = artifactBasicDetails.LockedByUserId != null && artifactBasicDetails.LockedByUserId.Value == userId ||
                                 artifactBasicDetails.HasDraftRelationships,
                LockedByUser = artifactBasicDetails.LockedByUserId != null
                    ? new UserGroup {
                    Id = artifactBasicDetails.LockedByUserId.Value, DisplayName = artifactBasicDetails.LockedByUserName
                }
                    : null,
                LockedDateTime = artifactBasicDetails.LockedByUserTime != null
                    ? (DateTime?)DateTime.SpecifyKind(artifactBasicDetails.LockedByUserTime.Value, DateTimeKind.Utc)
                    : null
            };

            if (artifactBasicDetails.DraftDeleted)
            {
                artifactInfo.DeletedByUser = artifactBasicDetails.UserId != null
                    ? new UserGroup {
                    Id = artifactBasicDetails.UserId.Value, DisplayName = artifactBasicDetails.UserName
                }
                    : null;
                artifactInfo.DeletedDateTime = artifactBasicDetails.LastSaveTimestamp != null
                    ? (DateTime?)DateTime.SpecifyKind(artifactBasicDetails.LastSaveTimestamp.Value, DateTimeKind.Utc)
                    : null;
            }
            else if (artifactBasicDetails.LatestDeleted)
            {
                artifactInfo.DeletedByUser = artifactBasicDetails.LatestDeletedByUserId != null
                    ? new UserGroup {
                    Id = artifactBasicDetails.LatestDeletedByUserId.Value, DisplayName = artifactBasicDetails.LatestDeletedByUserName
                }
                    : null;
                artifactInfo.DeletedDateTime = artifactBasicDetails.LatestDeletedByUserTime != null
                    ? (DateTime?)DateTime.SpecifyKind(artifactBasicDetails.LatestDeletedByUserTime.Value, DateTimeKind.Utc)
                    : null;
            }

            artifactInfo.Permissions = itemIdsPermissions[artifactBasicDetails.ArtifactId];

            if (baselineId != null)
            {
                var baselineRevisionId = await _itemInfoRepository.GetRevisionId(itemId, userId, null, baselineId.Value);

                var itemInfo = await _artifactPermissionsRepository.GetItemInfo(itemId, userId, false, baselineRevisionId);

                if (itemInfo == null)
                {
                    artifactInfo.IsNotExistsInBaseline = true;
                }

                artifactInfo.IsIncludedInBaseline = await IsArtifactInBaseline(artifactBasicDetails.ArtifactId, baselineId.Value, userId);
            }

            return(artifactInfo);
        }