Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[frontend/backend] Marking definitions for uploaded files (#5823) #6735

Merged
merged 6 commits into from
May 2, 2024
Merged
3 changes: 2 additions & 1 deletion opencti-platform/opencti-front/lang/front/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -2102,6 +2102,7 @@
"Select all nodes": "Alle Knoten auswählen",
"Select by entity type": "Auswahl nach Entitätstyp",
"Select destination": "Ziel auswählen",
"Select file marking définitions": "Definitionen der Dateikennzeichnung auswählen",
ValentinBouzinFiligran marked this conversation as resolved.
Show resolved Hide resolved
"Select options": "Optionen auswählen",
"Select your file": "Wählen Sie Ihre Datei",
"selected": "ausgewählte",
Expand Down Expand Up @@ -2721,4 +2722,4 @@
"Zoom": "Vergrößern",
"Zoom in": "Vergrößern",
"Zoom out": "Verkleinern"
}
}
3 changes: 2 additions & 1 deletion opencti-platform/opencti-front/lang/front/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -2102,6 +2102,7 @@
"Select all nodes": "Select all nodes",
"Select by entity type": "Select by entity type",
"Select destination": "Select destination",
"Select file marking définitions": "Select file marking définitions",
ValentinBouzinFiligran marked this conversation as resolved.
Show resolved Hide resolved
"Select options": "Select options",
"Select your file": "Select your file",
"selected": "selected",
Expand Down Expand Up @@ -2721,4 +2722,4 @@
"Zoom": "Zoom",
"Zoom in": "Zoom in",
"Zoom out": "Zoom out"
}
}
3 changes: 2 additions & 1 deletion opencti-platform/opencti-front/lang/front/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -2102,6 +2102,7 @@
"Select all nodes": "Séleccionar todos los nodos",
"Select by entity type": "Séleccionar por tipo",
"Select destination": "Seleccionar destino",
"Select file marking définitions": "Seleccionar definiciones de marcado de archivos",
ValentinBouzinFiligran marked this conversation as resolved.
Show resolved Hide resolved
"Select options": "Seleccionar opciones",
"Select your file": "Seleccionar otro fichero",
"selected": "seleccionado(s)",
Expand Down Expand Up @@ -2721,4 +2722,4 @@
"Zoom": "Zoom",
"Zoom in": "Ampliar",
"Zoom out": "Alejar"
}
}
3 changes: 2 additions & 1 deletion opencti-platform/opencti-front/lang/front/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -2102,6 +2102,7 @@
"Select all nodes": "Sélectionner tous les noeuds",
"Select by entity type": "Sélectionner par type",
"Select destination": "Sélectionner la destination",
"Select file marking définitions": "Sélectionner les définitions de marquage de fichiers",
ValentinBouzinFiligran marked this conversation as resolved.
Show resolved Hide resolved
"Select options": "Sélectionner les options",
"Select your file": "Selectionner votre fichier",
"selected": "sélectionné(s)",
Expand Down Expand Up @@ -2721,4 +2722,4 @@
"Zoom": "Zoom",
"Zoom in": "Zoomer",
"Zoom out": "Zoom arrière"
}
}
3 changes: 2 additions & 1 deletion opencti-platform/opencti-front/lang/front/ja.json
Original file line number Diff line number Diff line change
Expand Up @@ -2102,6 +2102,7 @@
"Select all nodes": "すべてのノードを選択",
"Select by entity type": "エンティティ種別を指定して選択",
"Select destination": "送信先の選択",
"Select file marking définitions": "ファイルマーキングの定義を選択",
ValentinBouzinFiligran marked this conversation as resolved.
Show resolved Hide resolved
"Select options": "オプションの選択",
"Select your file": "ファイルを選択してください",
"selected": "選択中: ",
Expand Down Expand Up @@ -2721,4 +2722,4 @@
"Zoom": "ズーム",
"Zoom in": "ズームイン",
"Zoom out": "ズームアウト"
}
}
3 changes: 2 additions & 1 deletion opencti-platform/opencti-front/lang/front/zh.json
Original file line number Diff line number Diff line change
Expand Up @@ -2102,6 +2102,7 @@
"Select all nodes": "选择所有节点",
"Select by entity type": "按实体类型选择",
"Select destination": "选择目的地",
"Select file marking définitions": "选择文件标识定义",
ValentinBouzinFiligran marked this conversation as resolved.
Show resolved Hide resolved
"Select options": "选择选项",
"Select your file": "选择你的文件",
"selected": "已选",
Expand Down Expand Up @@ -2721,4 +2722,4 @@
"Zoom": "放大",
"Zoom in": "放大",
"Zoom out": "缩小"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1074,6 +1074,7 @@ const ContainerHeader = (props) => {
instanceId={container.id}
instanceType={container.entity_type}
instanceName={getMainRepresentative(container)}
instanceMarkings={container.objectMarking.map(({ id }) => id)}
type="container"
/>
)}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { Formik } from 'formik';
import Dialog from '@mui/material/Dialog';
import DialogTitle from '@mui/material/DialogTitle';
import DialogContent from '@mui/material/DialogContent';
import DialogActions from '@mui/material/DialogActions';
import React from 'react';
import ObjectMarkingField from '@components/common/form/ObjectMarkingField';
import Button from '@mui/material/Button';
import { Option } from '@components/common/form/ReferenceField';
import { useFormatter } from '../../../../components/i18n';
import { fieldSpacingContainerStyle } from '../../../../utils/field';

type FileImportMarkingSelectionPopupProps = {
closePopup: () => void;
handleUpload: (fileMarkings: string[]) => void;
isOpen: boolean
};

export type SubmittedMarkingsType = {
fileMarkings: Option[];
};

const FileImportMarkingSelectionPopup = ({ closePopup, handleUpload, isOpen }: FileImportMarkingSelectionPopupProps) => {
const { t_i18n } = useFormatter();

const handleSubmit = (values: SubmittedMarkingsType) => {
const fileMarkings = values.fileMarkings.map(({ value }) => value);
closePopup();
handleUpload(fileMarkings);
};

return (
<div>
ValentinBouzinFiligran marked this conversation as resolved.
Show resolved Hide resolved
<Formik
enableReinitialize={true}
initialValues={{
fileMarkings: [],
}}
onSubmit={handleSubmit}
>
{({ resetForm, submitForm }) => (
<Dialog open={isOpen} fullWidth={true} PaperProps={{ elevation: 1 }} onClose={() => {
resetForm();
closePopup();
}}
>
<DialogTitle>{t_i18n('Select file marking définitions')}</DialogTitle>
ValentinBouzinFiligran marked this conversation as resolved.
Show resolved Hide resolved
<DialogContent>
<ObjectMarkingField
name="fileMarkings"
label={t_i18n('File marking definition levels')}
style={fieldSpacingContainerStyle}
/>
</DialogContent>
<DialogActions>
<Button onClick={() => {
resetForm();
closePopup();
}}
>
{t_i18n('Cancel')}
</Button>
<Button
color="secondary"
onClick={submitForm}
>
{t_i18n('Validate')}
</Button>
</DialogActions>
</Dialog>
)}
</Formik>
</div>
);
};

export default FileImportMarkingSelectionPopup;
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,36 @@ import { commitMutation, MESSAGING$ } from '../../../../relay/environment';
import { useFormatter } from '../../../../components/i18n';
import { FileUploaderEntityMutation$data } from './__generated__/FileUploaderEntityMutation.graphql';
import { FileUploaderGlobalMutation$data } from './__generated__/FileUploaderGlobalMutation.graphql';
import FileImportMarkingSelectionPopup from './FileImportMarkingSelectionPopup';

const fileUploaderGlobalMutation = graphql`
mutation FileUploaderGlobalMutation($file: Upload!) {
uploadImport(file: $file) {
id
...FileLine_file
mutation FileUploaderGlobalMutation($file: Upload!, $fileMarkings: [String]) {
uploadImport(file: $file, fileMarkings: $fileMarkings) {
id
...FileLine_file
}
ValentinBouzinFiligran marked this conversation as resolved.
Show resolved Hide resolved
}
}
`;

const fileUploaderEntityMutation = graphql`
mutation FileUploaderEntityMutation($id: ID!, $file: Upload!) {
stixCoreObjectEdit(id: $id) {
importPush(file: $file) {
id
...FileLine_file
metaData {
entity {
... on StixObject {
id
}
... on StixDomainObject {
...PictureManagementViewer_entity
mutation FileUploaderEntityMutation($id: ID!, $file: Upload!, $fileMarkings: [String]) {
ValentinBouzinFiligran marked this conversation as resolved.
Show resolved Hide resolved
stixCoreObjectEdit(id: $id) {
importPush(file: $file, fileMarkings: $fileMarkings) {
id
...FileLine_file
metaData {
entity {
... on StixObject {
id
}
... on StixDomainObject {
...PictureManagementViewer_entity
}
}
}
}
}
}
}
}
}
`;

interface FileUploaderProps {
Expand All @@ -56,20 +57,22 @@ const FileUploader: FunctionComponent<FileUploaderProps> = ({
nameInCallback,
}) => {
const { t_i18n } = useFormatter();

const uploadRef = useRef<HTMLInputElement | null>(null);
const [upload, setUpload] = useState<string | null>(null);

const [selectedFile, setSelectedFile] = useState<File>();
const handleOpenUpload = () => uploadRef.current?.click();

const handleUpload = (file: File) => {
const closeFileImportMarkingSelectionPopup = () => setSelectedFile(undefined);

const handleUpload = (fileMarkings: string[]) => {
if (!selectedFile) return;
commitMutation({
mutation: entityId
? fileUploaderEntityMutation
: fileUploaderGlobalMutation,
variables: { file, id: entityId },
variables: { file: selectedFile, fileMarkings, id: entityId },
optimisticUpdater: () => {
setUpload(file.name);
setUpload(selectedFile.name);
},
onCompleted: (
result:
Expand All @@ -94,10 +97,12 @@ const FileUploader: FunctionComponent<FileUploaderProps> = ({
updater: undefined,
optimisticResponse: undefined,
onError: undefined,
setSubmitting: undefined,
setSubmitting: true,
});
};

const hasSelectedFile = !!selectedFile;

return (
<React.Fragment>
{accept ? (
Expand All @@ -107,9 +112,7 @@ const FileUploader: FunctionComponent<FileUploaderProps> = ({
style={{ display: 'none' }}
onChange={({ target: { validity, files } }) => {
const file = files?.item(0);
if (file && validity.valid) {
handleUpload(file);
}
if (file && validity.valid) setSelectedFile(file);
}}
accept={accept}
/>
Expand All @@ -120,12 +123,17 @@ const FileUploader: FunctionComponent<FileUploaderProps> = ({
style={{ display: 'none' }}
onChange={({ target: { validity, files } }) => {
const file = files?.item(0);
if (file && validity.valid) {
handleUpload(file);
}
if (file && validity.valid) setSelectedFile(file);
}}
/>
)}
{hasSelectedFile && (
<FileImportMarkingSelectionPopup
isOpen={hasSelectedFile}
handleUpload={handleUpload}
closePopup={closeFileImportMarkingSelectionPopup}
/>
)}
{upload ? (
<Tooltip
title={`Uploading ${upload}`}
Expand Down