Skip to content

Commit

Permalink
[frontend] fix remove relation ref from Grouping Knowledge graph (#6482)
Browse files Browse the repository at this point in the history
  • Loading branch information
Archidoit committed Mar 26, 2024
1 parent b1ee60f commit 398b056
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import GroupingKnowledgeGraphBar from './GroupingKnowledgeGraphBar';
import { groupingMutationFieldPatch } from './GroupingEditionOverview';
import {
groupingKnowledgeGraphMutationRelationDeleteMutation,
groupingKnowledgeGraphQueryStixObjectDeleteMutation,
groupingKnowledgeGraphQueryStixRelationshipDeleteMutation,
groupingKnowledgeGraphtMutationRelationAddMutation,
} from './GroupingKnowledgeGraphQuery';
Expand Down Expand Up @@ -93,6 +94,63 @@ const groupingKnowledgeGraphCheckRelationQuery = graphql`
}
`;

const groupingKnowledgeGraphCheckObjectQuery = graphql`
query GroupingKnowledgeGraphCheckObjectQuery($id: String!) {
stixObjectOrStixRelationship(id: $id) {
... on BasicObject {
id
}
... on StixCoreObject {
is_inferred
parent_types
reports {
edges {
node {
id
}
}
}
}
... on BasicRelationship {
id
}
... on StixCoreRelationship {
is_inferred
parent_types
reports {
edges {
node {
id
}
}
}
}
... on StixRefRelationship {
is_inferred
parent_types
reports {
edges {
node {
id
}
}
}
}
... on StixSightingRelationship {
is_inferred
parent_types
reports {
edges {
node {
id
}
}
}
}
}
}
`;

const groupingKnowledgeGraphStixCoreObjectQuery = graphql`
query GroupingKnowledgeGraphStixCoreObjectQuery($id: String!) {
stixCoreObject(id: $id) {
Expand Down Expand Up @@ -969,7 +1027,7 @@ class GroupingKnowledgeGraphComponent extends Component {
});
}

async handleDeleteSelected() {
async handleDeleteSelected(deleteObject = false) {
// Remove selected links
const selectedLinks = Array.from(this.selectedLinks);
const selectedLinksIds = R.map((n) => n.id, selectedLinks);
Expand All @@ -980,7 +1038,8 @@ class GroupingKnowledgeGraphComponent extends Component {
.toPromise()
.then(async (data) => {
if (
!data.stixRelationship.is_inferred
deleteObject
&& !data.stixRelationship.is_inferred
&& data.stixRelationship.groupings.edges.length === 1
) {
commitMutation({
Expand Down Expand Up @@ -1033,14 +1092,33 @@ class GroupingKnowledgeGraphComponent extends Component {
});
}, relationshipsToRemove);
R.forEach((n) => {
commitMutation({
mutation: groupingKnowledgeGraphMutationRelationDeleteMutation,
variables: {
id: this.props.grouping.id,
toId: n.id,
relationship_type: 'object',
},
});
fetchQuery(groupingKnowledgeGraphCheckObjectQuery, {
id: n.id,
})
.toPromise()
.then(async (data) => {
if (
deleteObject
&& !data.stixObjectOrStixRelationship.is_inferred
&& data.stixObjectOrStixRelationship.groupings.edges.length === 1
) {
commitMutation({
mutation: groupingKnowledgeGraphQueryStixObjectDeleteMutation,
variables: {
id: n.id,
},
});
} else {
commitMutation({
mutation: groupingKnowledgeGraphMutationRelationDeleteMutation,
variables: {
id: this.props.grouping.id,
toId: n.id,
relationship_type: 'object',
},
});
}
});
}, selectedNodes);
this.selectedNodes.clear();
this.graphData = buildGraphData(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,14 @@ import { ResponsiveContainer, Scatter, ScatterChart, YAxis, ZAxis } from 'rechar
import Badge from '@mui/material/Badge';
import Dialog from '@mui/material/Dialog';
import DialogContent from '@mui/material/DialogContent';
import DialogContentText from '@mui/material/DialogContentText';
import DialogActions from '@mui/material/DialogActions';
import Button from '@mui/material/Button';
import Slide from '@mui/material/Slide';
import Typography from '@mui/material/Typography';
import Alert from '@mui/material/Alert';
import AlertTitle from '@mui/material/AlertTitle';
import FormGroup from '@mui/material/FormGroup';
import FormControlLabel from '@mui/material/FormControlLabel';
import inject18n from '../../../../components/i18n';
import ContainerAddStixCoreObjects from '../../common/containers/ContainerAddStixCoreObjects';
import StixCoreRelationshipCreation from '../../common/stix_core_relationships/StixCoreRelationshipCreation';
Expand Down Expand Up @@ -99,6 +103,7 @@ class GroupingKnowledgeGraphBar extends Component {
openEditDomainObject: false,
openEditObservable: false,
displayRemove: false,
deleteObject: false,
};
}

Expand All @@ -113,7 +118,11 @@ class GroupingKnowledgeGraphBar extends Component {
}

handleCloseRemove() {
this.setState({ displayRemove: false });
this.setState({ displayRemove: false, deleteObject: false });
}

handleToggleDeleteObject() {
this.setState({ deleteObject: !this.state.deleteObject });
}

handleOpenStixCoreObjectsTypes(event) {
Expand Down Expand Up @@ -357,6 +366,7 @@ class GroupingKnowledgeGraphBar extends Component {
openEditDomainObject,
openEditObservable,
openEditNested,
deleteObject,
} = this.state;
const isInferred = selectedNodes.filter((n) => n.inferred).length > 0
|| selectedLinks.filter((n) => n.inferred).length > 0;
Expand Down Expand Up @@ -1053,11 +1063,33 @@ class GroupingKnowledgeGraphBar extends Component {
onClose={this.handleCloseRemove.bind(this)}
>
<DialogContent>
<DialogContentText>
<Typography variant="body">
{t(
'Do you want to remove these elements from this grouping?',
)}
</DialogContentText>
</Typography>
<Alert
severity="warning"
variant="outlined"
style={{ marginTop: 20 }}
>
<AlertTitle>{t('Cascade delete')}</AlertTitle>
<FormGroup>
<FormControlLabel
control={
<Checkbox
checked={deleteObject}
onChange={this.handleToggleDeleteObject.bind(
this,
)}
/>
}
label={t(
'Delete the element if no other containers contain it',
)}
/>
</FormGroup>
</Alert>
</DialogContent>
<DialogActions>
<Button onClick={this.handleCloseRemove.bind(this)}>
Expand All @@ -1066,7 +1098,7 @@ class GroupingKnowledgeGraphBar extends Component {
<Button
onClick={() => {
this.handleCloseRemove();
handleDeleteSelected();
handleDeleteSelected(deleteObject);
}}
color="secondary"
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,11 @@ export const groupingKnowledgeGraphQueryStixRelationshipDeleteMutation = graphql
}
}
`;

export const groupingKnowledgeGraphQueryStixObjectDeleteMutation = graphql`
mutation GroupingKnowledgeGraphQueryStixCoreObjectDeleteMutation($id: ID!) {
stixCoreObjectEdit(id: $id) {
delete
}
}
`;

0 comments on commit 398b056

Please sign in to comment.