public async Task <Guid> CreateFolderAsync(Guid userId, string slug, Folder folder, CancellationToken cancellationToken) { if (Guid.Empty == userId) { throw new ArgumentOutOfRangeException(nameof(userId)); } if (string.IsNullOrEmpty(slug)) { throw new ArgumentOutOfRangeException(nameof(slug)); } var now = _systemClock.UtcNow.UtcDateTime; var groupId = await _groupCommand.GetGroupIdForSlugAsync(slug, cancellationToken); if (!groupId.HasValue) { _logger.LogError($"Error: CreateFolderAsync - Group not found for slug:{0}", slug); throw new KeyNotFoundException("Error: Group not found for slug"); } var userCanPerformAction = await _permissionsService.UserCanPerformActionAsync(userId, groupId.Value, AddFolderRole, cancellationToken); if (!userCanPerformAction) { _logger.LogError($"Error: CreateFolderAsync - User:{0} does not have access to group:{1}", userId, slug); throw new SecurityException($"Error: User does not have access"); } var folderDto = new FolderDto() { Name = folder.Name, Description = folder.Description, CreatedAtUTC = now, CreatedBy = userId, ModifiedBy = null, ModifiedAtUTC = null, ParentFolder = null, GroupId = groupId.Value, IsDeleted = false }; var validator = new FolderValidator(_folderCommand); var validationResult = await validator.ValidateAsync(folderDto, cancellationToken); if (validationResult.Errors.Count > 0) { throw new ValidationException(validationResult); } return(await _folderCommand.CreateFolderAsync(userId, groupId.Value, folderDto, cancellationToken)); }
public async Task UpdateFolderAsync(Guid userId, string slug, Guid folderId, Folder folder, byte[] rowVersion, CancellationToken cancellationToken) { if (Guid.Empty == userId) { throw new ArgumentOutOfRangeException(nameof(userId)); } if (Guid.Empty == folderId) { throw new ArgumentOutOfRangeException(nameof(userId)); } if (string.IsNullOrEmpty(slug)) { throw new ArgumentOutOfRangeException(nameof(slug)); } var now = _systemClock.UtcNow.UtcDateTime; var groupId = await _groupCommand.GetGroupIdForSlugAsync(slug, cancellationToken); if (!groupId.HasValue) { _logger.LogError($"Error: UpdateFolderAsync - Group not found for slug:{0}", slug); throw new KeyNotFoundException("Error: Group not found for slug"); } var userCanEditFolder = await _permissionsService.UserCanPerformActionAsync(userId, slug, EditFolderRole, cancellationToken); var databaseFolderDto = await _folderCommand.GetFolderAsync(folderId, cancellationToken); if (databaseFolderDto.CreatedBy != userId && !userCanEditFolder) { _logger.LogError($"Forbidden: UpdateFolderAsync - User:{0} does not have permission to edit folder:{1}", userId, folderId); throw new ForbiddenException("Forbidden: User does not have permission to edit this folder"); } if (!databaseFolderDto.RowVersion.SequenceEqual(rowVersion)) { _logger.LogError($"Precondition Failed: UpdateFolderAsync - Folder:{0} has changed prior to submission ", folderId); throw new PreconditionFailedExeption("Precondition Failed: Folder has changed prior to submission"); } var folderDto = new FolderDto() { Id = folderId, Name = folder.Name, Description = folder.Description, ModifiedBy = userId, ModifiedAtUTC = now, ParentFolder = databaseFolderDto.ParentFolder, GroupId = groupId.Value, }; var validator = new FolderValidator(_folderCommand); var validationResult = await validator.ValidateAsync(folderDto, cancellationToken); if (validationResult.Errors.Count > 0) { throw new ValidationException(validationResult); } await _folderCommand.UpdateFolderAsync(userId, folderDto, rowVersion, cancellationToken); }