Skip to content

Commit

Permalink
Merge pull request #290 from IQSS/272-move-filepreview-permissions
Browse files Browse the repository at this point in the history
272 move filepreview permissions
  • Loading branch information
GPortas authored Feb 14, 2024
2 parents 4e949d3 + 460304c commit 6c4400c
Show file tree
Hide file tree
Showing 44 changed files with 225 additions and 847 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,18 @@ Note that both Storybook instances are also published to Chromatic:
- [Dataverse Frontend](https://www.chromatic.com/builds?appId=646f68aa9beb01b35c599acd)
- [Dataverse Design System](https://www.chromatic.com/builds?appId=646fbe232a8d3b501a1943f3)

### Cypress Testing Tool

The project includes [@cypress/grep](https://www.npmjs.com/package/@cypress/grep) for running specific tests.
To run the tests, use --env grep="your test name" in the command line.

To run a specific test multiple times, use --env burn=10 in the command line.
Running tests multilpe times is useful for detecting flaky tests.

For example:

`npx cypress run --spec tests/e2e-integration/e2e/sections/dataset/Dataset.spec.tsx --env grep="loads the restricted files when the user is logged in as owner",burn=10`

## Local development environment

A containerized environment, oriented to local development, is available to be run from the repository.
Expand Down
52 changes: 52 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@
"@auto-it/released": "10.46.0",
"@csstools/postcss-cascade-layers": "3.0.1",
"@cypress/code-coverage": "3.10.7",
"@cypress/grep": "4.0.1",
"@storybook/addon-a11y": "7.0.2",
"@storybook/addon-essentials": "7.0.2",
"@storybook/addon-interactions": "7.0.2",
Expand Down
4 changes: 2 additions & 2 deletions src/files/domain/models/File.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { DatasetVersion } from '../../../dataset/domain/models/Dataset'
import { FileMetadata } from './FileMetadata'
import { FileVersion } from './FileVersion'
import { FileAccess } from './FileAccess'
import { FileUserPermissions } from './FileUserPermissions'
import { FilePermissions } from './FilePermissions'
import { FileIngest } from './FileIngest'

export interface File {
Expand All @@ -12,7 +12,7 @@ export interface File {
access: FileAccess
datasetVersion: DatasetVersion
citation: string
permissions: FileUserPermissions
permissions: FilePermissions
metadata: FileMetadata
ingest: FileIngest
}
3 changes: 3 additions & 0 deletions src/files/domain/models/FilePermissions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface FilePermissions {
canDownloadFile: boolean
}
2 changes: 2 additions & 0 deletions src/files/domain/models/FilePreview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { FileMetadata } from './FileMetadata'
import { FileVersion } from './FileVersion'
import { FileIngest } from './FileIngest'
import { FileAccess } from './FileAccess'
import { FilePermissions } from './FilePermissions'

export interface FilePreview {
id: number
Expand All @@ -10,4 +11,5 @@ export interface FilePreview {
access: FileAccess
ingest: FileIngest
metadata: FileMetadata
permissions: FilePermissions
}
10 changes: 0 additions & 10 deletions src/files/domain/models/FileUserPermissions.ts

This file was deleted.

2 changes: 0 additions & 2 deletions src/files/domain/repositories/FileRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { FileDownloadMode } from '../models/FileMetadata'
import { File } from '../models/File'
import { FileCriteria } from '../models/FileCriteria'
import { FilesCountInfo } from '../models/FilesCountInfo'
import { FileUserPermissions } from '../models/FileUserPermissions'
import { DatasetVersion, DatasetVersionNumber } from '../../../dataset/domain/models/Dataset'
import { FilePaginationInfo } from '../models/FilePaginationInfo'
import { FilePreview } from '../models/FilePreview'
Expand All @@ -24,7 +23,6 @@ export interface FileRepository {
datasetVersionNumber: DatasetVersionNumber,
criteria?: FileCriteria
) => Promise<number>
getUserPermissionsById: (id: number) => Promise<FileUserPermissions>
getById: (id: number) => Promise<File | undefined>
getMultipleFileDownloadUrl: (ids: number[], downloadMode: FileDownloadMode) => string
getFileDownloadUrl: (id: number, downloadMode: FileDownloadMode) => string
Expand Down
23 changes: 0 additions & 23 deletions src/files/domain/useCases/checkFileDownloadPermission.ts

This file was deleted.

11 changes: 0 additions & 11 deletions src/files/domain/useCases/checkFileEditDatasetPermission.ts

This file was deleted.

29 changes: 15 additions & 14 deletions src/files/infrastructure/FileJSDataverseRepository.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { FileRepository } from '../domain/repositories/FileRepository'
import { FileDownloadMode } from '../domain/models/FileMetadata'
import { FilesCountInfo } from '../domain/models/FilesCountInfo'
import { FileUserPermissions } from '../domain/models/FileUserPermissions'

import {
File as JSFile,
FileDataTable as JSFileTabularData,
Expand All @@ -23,8 +23,9 @@ import { FileMother } from '../../../tests/component/files/domain/models/FileMot
import { FilePaginationInfo } from '../domain/models/FilePaginationInfo'
import { BASE_URL } from '../../config'
import { FilePreview } from '../domain/models/FilePreview'
import { JSFileUserPermissionsMapper } from './mappers/JSFileUserPermissionsMapper'
import { JSFilesCountInfoMapper } from './mappers/JSFilesCountInfoMapper'
import { FilePermissions } from '../domain/models/FilePermissions'
import { JSFilePermissionsMapper } from './mappers/JSFilePermissionsMapper'

const includeDeaccessioned = true

Expand Down Expand Up @@ -52,15 +53,17 @@ export class FileJSDataverseRepository implements FileRepository {
jsFiles,
FileJSDataverseRepository.getAllDownloadCount(jsFiles),
FileJSDataverseRepository.getAllThumbnails(jsFiles),
FileJSDataverseRepository.getAllWithPermissions(jsFiles),
FileJSDataverseRepository.getAllTabularData(jsFiles)
])
)
.then(([jsFiles, downloadCounts, thumbnails, jsTabularData]) =>
.then(([jsFiles, downloadCounts, thumbnails, permissions, jsTabularData]) =>
jsFiles.map((jsFile, index) =>
JSFileMapper.toFile(
jsFile,
datasetVersion,
downloadCounts[index],
permissions[index],
thumbnails[index],
jsTabularData[index]
)
Expand Down Expand Up @@ -90,6 +93,15 @@ export class FileJSDataverseRepository implements FileRepository {
)
)
}
private static getAllWithPermissions(files: JSFile[]): Promise<FilePermissions[]> {
return Promise.all(files.map((jsFile) => this.getPermissionsById(jsFile.id)))
}

private static getPermissionsById(id: number): Promise<FilePermissions> {
return getFileUserPermissions
.execute(id)
.then((jsFilePermissions) => JSFilePermissionsMapper.toFilePermissions(jsFilePermissions))
}

private static getAllThumbnails(jsFiles: JSFile[]): Promise<(string | undefined)[]> {
return Promise.all(jsFiles.map((jsFile) => this.getThumbnailById(jsFile.id)))
Expand Down Expand Up @@ -149,17 +161,6 @@ export class FileJSDataverseRepository implements FileRepository {
})
}

getUserPermissionsById(id: number): Promise<FileUserPermissions> {
return getFileUserPermissions
.execute(id)
.then((jsFileUserPermissions) =>
JSFileUserPermissionsMapper.toFileUserPermissions(id, jsFileUserPermissions)
)
.catch((error: ReadError) => {
throw new Error(error.message)
})
}

// eslint-disable-next-line unused-imports/no-unused-vars
getById(id: number): Promise<File> {
return new Promise((resolve) => {
Expand Down
5 changes: 4 additions & 1 deletion src/files/infrastructure/mappers/JSFileMapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import { JSFileMetadataMapper } from './JSFileMetadataMapper'
import { JSFileVersionMapper } from './JSFileVersionMapper'
import { JSFileAccessMapper } from './JSFileAccessMapper'
import { JSFileIngestMapper } from './JSFileIngestMapper'
import { FilePermissions } from '../../domain/models/FilePermissions'

export class JSFileMapper {
static toFile(
jsFile: JSFile,
datasetVersion: DatasetVersion,
downloadsCount: number,
permissions: FilePermissions,
thumbnail?: string,
jsTabularData?: JSFileTabularData[]
): FilePreview {
Expand All @@ -32,7 +34,8 @@ export class JSFileMapper {
downloadsCount,
thumbnail,
jsTabularData
)
),
permissions: permissions
}
}

Expand Down
10 changes: 10 additions & 0 deletions src/files/infrastructure/mappers/JSFilePermissionsMapper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { FilePermissions } from '../../domain/models/FilePermissions'
import { FileUserPermissions as JSFilePermissions } from '@iqss/dataverse-client-javascript'

export class JSFilePermissionsMapper {
static toFilePermissions(jsFileUserPermissions: JSFilePermissions): FilePermissions {
return {
canDownloadFile: jsFileUserPermissions.canDownloadFile
}
}
}
17 changes: 0 additions & 17 deletions src/files/infrastructure/mappers/JSFileUserPermissionsMapper.ts

This file was deleted.

25 changes: 11 additions & 14 deletions src/sections/dataset/DatasetFactory.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { FileJSDataverseRepository } from '../../files/infrastructure/FileJSData
import { MetadataBlockInfoProvider } from './metadata-block-info/MetadataBlockProvider'
import { MetadataBlockInfoJSDataverseRepository } from '../../metadata-block-info/infrastructure/repositories/MetadataBlockInfoJSDataverseRepository'
import { SettingJSDataverseRepository } from '../../settings/infrastructure/SettingJSDataverseRepository'
import { FilePermissionsProvider } from '../file/file-permissions/FilePermissionsProvider'
import { SettingsProvider } from '../settings/SettingsProvider'
import { DatasetProvider } from './DatasetProvider'
import { MultipleFileDownloadProvider } from '../file/multiple-file-download/MultipleFileDownloadProvider'
Expand All @@ -24,19 +23,17 @@ export class DatasetFactory {
static create(): ReactElement {
return (
<MultipleFileDownloadProvider repository={fileRepository}>
<FilePermissionsProvider repository={fileRepository}>
<SettingsProvider repository={settingRepository}>
<NotImplementedModalProvider>
<MetadataBlockInfoProvider repository={metadataBlockInfoRepository}>
<AnonymizedProvider>
<AlertProvider>
<DatasetWithSearchParams />
</AlertProvider>
</AnonymizedProvider>
</MetadataBlockInfoProvider>
</NotImplementedModalProvider>
</SettingsProvider>
</FilePermissionsProvider>
<SettingsProvider repository={settingRepository}>
<NotImplementedModalProvider>
<MetadataBlockInfoProvider repository={metadataBlockInfoRepository}>
<AnonymizedProvider>
<AlertProvider>
<DatasetWithSearchParams />
</AlertProvider>
</AnonymizedProvider>
</MetadataBlockInfoProvider>
</NotImplementedModalProvider>
</SettingsProvider>
</MultipleFileDownloadProvider>
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,19 @@ import { FileOptionsMenu } from './file-options-menu/FileOptionsMenu'
import { ButtonGroup } from '@iqss/dataverse-design-system'
import { useTranslation } from 'react-i18next'
import { FilePublishingStatus } from '../../../../../../../files/domain/models/FileVersion'
import { useFileDownloadPermission } from '../../../../../../file/file-permissions/useFileDownloadPermission'

interface FileActionButtonsProps {
file: FilePreview
}
export function FileActionButtons({ file }: FileActionButtonsProps) {
const { t } = useTranslation('files')
const { sessionUserHasFileDownloadPermission } = useFileDownloadPermission(file)

return (
<ButtonGroup aria-label={t('actions.buttons')}>
<AccessFileMenu
id={file.id}
access={file.access}
userHasDownloadPermission={sessionUserHasFileDownloadPermission}
userHasDownloadPermission={file.permissions.canDownloadFile}
metadata={file.metadata}
isDeaccessioned={file.version.publishingStatus === FilePublishingStatus.DEACCESSIONED}
ingestInProgress={file.ingest.isInProgress}
Expand Down
Loading

0 comments on commit 6c4400c

Please sign in to comment.