public async Task <FilesInfo> GetAttachmentsAndDocumentReferences( int artifactId, int?versionId = null, int?subArtifactId = null, bool addDrafts = true, int?baselineId = null) { if (artifactId < 1 || (subArtifactId.HasValue && subArtifactId.Value < 1) || (versionId != null && baselineId != null)) { throw new BadRequestException(); } if (addDrafts && versionId != null) { addDrafts = false; } var userId = Session.UserId; var itemId = subArtifactId.HasValue ? subArtifactId.Value : artifactId; var isDeleted = await ArtifactVersionsRepository.IsItemDeleted(itemId); var itemInfo = isDeleted && (versionId != null || baselineId != null) ? (await ArtifactVersionsRepository.GetDeletedItemInfo(itemId)) : (await ArtifactPermissionsRepository.GetItemInfo(itemId, userId, addDrafts)); if (itemInfo == null) { throw new ResourceNotFoundException("You have attempted to access an item that does not exist or you do not have permission to view.", subArtifactId.HasValue ? ErrorCodes.SubartifactNotFound : ErrorCodes.ArtifactNotFound); } if (subArtifactId.HasValue && itemInfo.ArtifactId != artifactId) { throw new BadRequestException("Please provide a proper subartifact Id"); } var result = await AttachmentsRepository.GetAttachmentsAndDocumentReferences(artifactId, userId, versionId, subArtifactId, addDrafts, baselineId); var artifactIds = new List <int> { artifactId }; foreach (var documentReference in result.DocumentReferences) { artifactIds.Add(documentReference.ArtifactId); } var permissions = await ArtifactPermissionsRepository.GetArtifactPermissions(artifactIds, userId); if (!SqlArtifactPermissionsRepository.HasPermissions(artifactId, permissions, RolePermissions.Read)) { throw new AuthorizationException("You do not have permission to access the artifact"); } var docRef = result.DocumentReferences.ToList(); foreach (var documentReference in docRef) { if (!SqlArtifactPermissionsRepository.HasPermissions(documentReference.ArtifactId, permissions, RolePermissions.Read)) { result.DocumentReferences.Remove(documentReference); } } return(result); }
public async Task <RelationshipResultSet> GetRelationships( int artifactId, int userId, int?subArtifactId = null, bool addDrafts = true, bool allLinks = false, int?versionId = null, int?baselineId = null) { var revisionId = await _itemInfoRepository.GetRevisionId(artifactId, userId, versionId, baselineId); var itemId = subArtifactId ?? artifactId; var types = new List <int> { (int)LinkType.Manual, (int)LinkType.Association, (int)LinkType.ActorInheritsFrom, (int)LinkType.DocumentReference }; if (allLinks) { types.AddRange(new[] { (int)LinkType.ParentChild, (int)LinkType.Reuse }); } if (baselineId != null) { addDrafts = false; } var results = (await GetLinkInfo(itemId, userId, addDrafts, revisionId, types)).ToList(); var manualLinks = results.Where(a => a.LinkType == LinkType.Manual).ToList(); // filter out Parent/Child links between artifact and its subartifact if exist var excludeParentChildLinks = results.Where(link => link.LinkType == LinkType.ParentChild && ((link.SourceArtifactId != link.SourceItemId || link.DestinationArtifactId != link.DestinationItemId)) || ////internal links (link.SourceItemId == link.SourceProjectId)) ////to artifact's project .ToList(); // get reuse links to to modify them separaratly. var reuseLinks = results.Where(a => a.LinkType == LinkType.Reuse).ToList(); // get collection of other links exept exclude parent/child links and reuse links var otherLinks = results.Except(excludeParentChildLinks).Except(reuseLinks).Where(link => link.LinkType != LinkType.Manual).ToList(); // modify reuse links by combining matching pais (source match destination on other) and add them back to coolection of otherlinks otherLinks.AddRange(UpdateReuseLinks(reuseLinks, itemId)); var manualTraceRelationships = GetManualTraceRelationships(manualLinks, itemId); var otherTraceRelationships = new List <Relationship>(); foreach (var otherLink in otherLinks) { var traceDirection = otherLink.SourceItemId == itemId ? TraceDirection.To : TraceDirection.From; Relationship relationship = null; if (otherLink.LinkType == LinkType.ActorInheritsFrom) { var itemInfo = await _artifactPermissionsRepository.GetItemInfo(otherLink.DestinationArtifactId, userId, addDrafts, revisionId); if (itemInfo != null) { relationship = ComposeRelationship(otherLink, traceDirection); } } else if (otherLink.LinkType == LinkType.Reuse) { traceDirection = TraceDirection.TwoWay; var itemInfo = await _artifactPermissionsRepository.GetItemInfo(otherLink.DestinationArtifactId, userId, addDrafts, revisionId); if (itemInfo != null) { relationship = ComposeRelationship(otherLink, traceDirection); } } else { relationship = ComposeRelationship(otherLink, traceDirection); } if (relationship != null) { otherTraceRelationships.Add(relationship); } } var distinctItemIds = new HashSet <int>(); foreach (var result in results) { distinctItemIds.Add(result.SourceArtifactId); distinctItemIds.Add(result.DestinationArtifactId); distinctItemIds.Add(result.SourceItemId); distinctItemIds.Add(result.DestinationItemId); distinctItemIds.Add(result.SourceProjectId); distinctItemIds.Add(result.DestinationProjectId); } var itemDetailsDictionary = (await _itemInfoRepository.GetItemsDetails(userId, distinctItemIds, true, revisionId)).ToDictionary(a => a.HolderId); var itemLabelsDictionary = (await _itemInfoRepository.GetItemsLabels(userId, distinctItemIds, true, revisionId)).ToDictionary(a => a.ItemId); PopulateRelationshipInfos(manualTraceRelationships, itemDetailsDictionary, itemLabelsDictionary); PopulateRelationshipInfos(otherTraceRelationships, itemDetailsDictionary, itemLabelsDictionary); return(new RelationshipResultSet { RevisionId = revisionId, ManualTraces = manualTraceRelationships, OtherTraces = otherTraceRelationships }); }
public async Task <DiscussionResultSet> GetDiscussions(int artifactId, int?subArtifactId = null) { ValidateRequestParameters(artifactId, subArtifactId); var userId = Session.UserId; var itemId = subArtifactId.HasValue ? subArtifactId.Value : artifactId; var revisionId = int.MaxValue; var isDeleted = await _artifactVersionsRepository.IsItemDeleted(itemId); var itemInfo = isDeleted ? await _artifactVersionsRepository.GetDeletedItemInfo(itemId) : await _artifactPermissionsRepository.GetItemInfo(itemId, userId, false); if (itemInfo == null) { throw new ResourceNotFoundException("You have attempted to access an item that does not exist or you do not have permission to view.", subArtifactId.HasValue ? ErrorCodes.SubartifactNotFound : ErrorCodes.ArtifactNotFound); } if (subArtifactId.HasValue && itemInfo.ArtifactId != artifactId) { throw new BadRequestException("Please provide a proper subartifact Id"); } if (isDeleted) { revisionId = ((DeletedItemInfo)itemInfo).VersionId; } var permissions = await _artifactPermissionsRepository.GetArtifactPermissions(new[] { artifactId }, userId, false, revisionId); var projectPermissions = await _artifactPermissionsRepository.GetProjectPermissions(itemInfo.ProjectId); RolePermissions permission = RolePermissions.None; if (!permissions.TryGetValue(artifactId, out permission) || !permission.HasFlag(RolePermissions.Read)) { throw new AuthorizationException("You do not have permission to access the artifact"); } var discussions = await _discussionsRepository.GetDiscussions(itemId, itemInfo.ProjectId); foreach (var discussion in discussions) { discussion.CanDelete = !projectPermissions.HasFlag(ProjectPermissions.CommentsDeletionDisabled) && permissions.TryGetValue(artifactId, out permission) && (permission.HasFlag(RolePermissions.DeleteAnyComment) || (permission.HasFlag(RolePermissions.Comment) && discussion.UserId == userId)); discussion.CanEdit = !projectPermissions.HasFlag(ProjectPermissions.CommentsModificationDisabled) && permissions.TryGetValue(artifactId, out permission) && (permission.HasFlag(RolePermissions.Comment) && discussion.UserId == userId); } var availableStatuses = await _discussionsRepository.GetThreadStatusCollection(itemInfo.ProjectId); var result = new DiscussionResultSet { CanDelete = !projectPermissions.HasFlag(ProjectPermissions.CommentsDeletionDisabled) && permission.HasFlag(RolePermissions.DeleteAnyComment) && revisionId == int.MaxValue, CanCreate = permission.HasFlag(RolePermissions.Comment) && revisionId == int.MaxValue, Discussions = discussions, EmailDiscussionsEnabled = await _discussionsRepository.AreEmailDiscussionsEnabled(itemInfo.ProjectId), ThreadStatuses = availableStatuses }; return(result); }
public async Task <RelationshipResultSet> GetRelationships( int artifactId, int?subArtifactId = null, bool addDrafts = true, int?versionId = null, int?baselineId = null, bool?allLinks = null) { var userId = Session.UserId; if (artifactId < 1 || (subArtifactId.HasValue && subArtifactId.Value < 1) || versionId.HasValue && versionId.Value < 1 || (versionId != null && baselineId != null)) { throw new BadRequestException(); } if (addDrafts && versionId != null) { addDrafts = false; } var itemId = subArtifactId ?? artifactId; var isDeleted = await _artifactVersionsRepository.IsItemDeleted(itemId); var itemInfo = isDeleted && (versionId != null || baselineId != null) ? await _artifactVersionsRepository.GetDeletedItemInfo(itemId) : await _artifactPermissionsRepository.GetItemInfo(itemId, userId, addDrafts); if (itemInfo == null) { throw new ResourceNotFoundException("You have attempted to access an item that does not exist or you do not have permission to view.", subArtifactId.HasValue ? ErrorCodes.SubartifactNotFound : ErrorCodes.ArtifactNotFound); } if (subArtifactId.HasValue && itemInfo.ArtifactId != artifactId) { throw new BadRequestException("Please provide a proper subartifact Id"); } // We do not need drafts for historical artifacts var effectiveAddDraft = !versionId.HasValue && addDrafts; var result = await _relationshipsRepository.GetRelationships(artifactId, userId, subArtifactId, effectiveAddDraft, allLinks.GetValueOrDefault(), versionId, baselineId); var artifactIds = new List <int> { artifactId }; artifactIds = artifactIds.Union(result.ManualTraces.Select(a => a.ArtifactId)).Union(result.OtherTraces.Select(a => a.ArtifactId)).Distinct().ToList(); var permissions = await _artifactPermissionsRepository.GetArtifactPermissions(artifactIds, userId); if (!SqlArtifactPermissionsRepository.HasPermissions(artifactId, permissions, RolePermissions.Read)) { throw new AuthorizationException("You do not have permission to access the artifact"); } ApplyRelationshipPermissions(permissions, result.ManualTraces); ApplyRelationshipPermissions(permissions, result.OtherTraces); result.CanEdit = SqlArtifactPermissionsRepository.HasPermissions(artifactId, permissions, RolePermissions.Trace) && SqlArtifactPermissionsRepository.HasPermissions(artifactId, permissions, RolePermissions.Edit); return(result); }
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); }