Skip to content

Commit

Permalink
#2125 Allow observable datatype update in bullk edit modal
Browse files Browse the repository at this point in the history
  • Loading branch information
nadouani committed Oct 6, 2021
1 parent c88f3e9 commit 7625676
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 81 deletions.
8 changes: 7 additions & 1 deletion frontend/app/scripts/controllers/case/CaseObservablesCtrl.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(function () {
'use strict';
angular.module('theHiveControllers').controller('CaseObservablesCtrl',
function ($scope, $q, $state, $stateParams, $filter, $uibModal, SecuritySrv, ModalUtilsSrv, FilteringSrv, StreamSrv, CaseTabsSrv, PaginatedQuerySrv, CaseArtifactSrv, NotificationSrv, AnalyzerSrv, CortexSrv, VersionSrv) {
function ($scope, $q, $state, $stateParams, $filter, $uibModal, SecuritySrv, ModalUtilsSrv, FilteringSrv, StreamSrv, CaseTabsSrv, PaginatedQuerySrv, ObservableTypeSrv, CaseArtifactSrv, NotificationSrv, AnalyzerSrv, CortexSrv, VersionSrv) {

CaseTabsSrv.activateTab($state.current.data.tab);

Expand Down Expand Up @@ -241,6 +241,12 @@
resolve: {
selection: function () {
return $scope.selection.artifacts;
},
dataTypes: function () {
return ObservableTypeSrv.list()
.then(function (response) {
return response.data;
});
}
}
});
Expand Down
142 changes: 88 additions & 54 deletions frontend/app/scripts/controllers/case/ObservableUpdateCtrl.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,49 @@
(function() {
(function () {
'use strict';
angular.module('theHiveControllers').controller('ObservableUpdateCtrl',
function($scope, $uibModalInstance, TagSrv, TaxonomyCacheSrv, selection) {
function ($scope, $uibModalInstance, TagSrv, TaxonomyCacheSrv, selection, dataTypes) {
var self = this;

this.selection = selection;
this.state = {
all: false,
enableTlp: false,
enableIoc: false,
enableSighted: false,
enableIgnoreSimilarity: false,
enableAddTags: false,
enableRemoveTags: false
};
this.dataTypes = _.pluck(_.filter(dataTypes, function (item) {
return !item.isAttachment
}), 'name').sort();

this.$onInit = function () {
this.state = {
all: false,
enableDataType: false,
enableTlp: false,
enableIoc: false,
enableSighted: false,
enableIgnoreSimilarity: false,
enableAddTags: false,
enableRemoveTags: false
};

this.activeTlp = 'active';
this.params = {
ioc: false,
sighted: false,
ignoreSimilarity: false,
tlp: 2,
addTagNames: '',
removeTagNames: ''
};
this.activeTlp = 'active';
this.params = {
dataType: null,
ioc: false,
sighted: false,
ignoreSimilarity: false,
tlp: 2,
addTagNames: '',
removeTagNames: ''
};

var selectionTypes = _.unique(_.pluck(this.selection, 'dataType'));

this.toggleAll = function() {
this.canUpdateDataType = selectionTypes.length === 1 && this.dataTypes.indexOf(selectionTypes[0]) !== -1;
};

this.toggleAll = function () {
this.state.all = !this.state.all;

if (this.canUpdateDataType) {
this.state.enableDataType = this.state.all;
}

this.state.enableTlp = this.state.all;
this.state.enableIoc = this.state.all;
this.state.enableSighted = this.state.all;
Expand All @@ -37,14 +52,14 @@
this.state.enableRemoveTags = this.state.all;
};

this.categorizeObservables = function() {
this.categorizeObservables = function () {
var data = {
withTags: [],
withoutTags: []
};

_.each(this.selection, function(item) {
if(item.tags.length > 0) {
_.each(this.selection, function (item) {
if (item.tags.length > 0) {
data.withTags.push(item);
} else {
data.withoutTags.push(item);
Expand All @@ -54,11 +69,17 @@
return data;
};

this.buildOperations = function(postData) {
var flags = _.pick(postData, 'ioc', 'sighted', 'ignoreSimilarity', 'tlp');
this.buildOperations = function (postData) {
var flags = {};

if (this.canUpdateDataType) {
flags = _.pick(postData, 'dataType', 'ioc', 'sighted', 'ignoreSimilarity', 'tlp');
} else {
flags = _.pick(postData, 'ioc', 'sighted', 'ignoreSimilarity', 'tlp');
}

// Handle updates without tag changes
if(!postData.addTags && !postData.removeTags) {
if (!postData.addTags && !postData.removeTags) {
return [
{
ids: _.pluck(this.selection, '_id'),
Expand All @@ -70,28 +91,28 @@
// Handle update with tag changes
var input = this.categorizeObservables();
var operations = [];
if(input.withoutTags.length > 0) {
var tags = (postData.addTags || []).filter(function(i) {
if (input.withoutTags.length > 0) {
var tags = (postData.addTags || []).filter(function (i) {
return (postData.removeTags || []).indexOf(i) === -1;
});

operations.push({
ids: _.pluck(input.withoutTags, '_id'),
patch: _.extend({}, flags ,{
patch: _.extend({}, flags, {
tags: _.unique(tags)
})
});
}

if(input.withTags.length > 0) {
_.each(input.withTags, function(observable) {
tags = observable.tags.concat(postData.addTags || []).filter(function(i) {
if (input.withTags.length > 0) {
_.each(input.withTags, function (observable) {
tags = observable.tags.concat(postData.addTags || []).filter(function (i) {
return (postData.removeTags || []).indexOf(i) === -1;
});

operations.push({
ids: [observable._id],
patch: _.extend({}, flags ,{
patch: _.extend({}, flags, {
tags: _.unique(tags)
})
});
Expand All @@ -101,49 +122,53 @@
return operations;
};

this.save = function() {
this.save = function () {

var postData = {};

if(this.state.enableIoc) {
if (this.state.enableDataType && this.canUpdateDataType) {
postData.dataType = this.params.dataType;
}

if (this.state.enableIoc) {
postData.ioc = this.params.ioc;
}

if(this.state.enableSighted) {
if (this.state.enableSighted) {
postData.sighted = this.params.sighted;
}

if(this.state.enableIgnoreSimilarity) {
if (this.state.enableIgnoreSimilarity) {
postData.ignoreSimilarity = this.params.ignoreSimilarity;
}

if(this.state.enableTlp) {
if (this.state.enableTlp) {
postData.tlp = this.params.tlp;
}

if(this.state.enableAddTags) {
if (this.state.enableAddTags) {
postData.addTags = _.pluck(this.params.addTags, 'text');
}

if(this.state.enableRemoveTags) {
if (this.state.enableRemoveTags) {
postData.removeTags = _.pluck(this.params.removeTags, 'text');
}

$uibModalInstance.close(this.buildOperations(postData));
};

this.cancel = function() {
this.cancel = function () {
$uibModalInstance.dismiss();
};

this.getTags = function(query) {
this.getTags = function (query) {
return TagSrv.autoComplete(query);
};

self.fromTagLibrary = function(field) {
self.fromTagLibrary = function (field) {
TaxonomyCacheSrv.openTagLibrary()
.then(function(tags){
if(field === 'add') {
.then(function (tags) {
if (field === 'add') {
self.params.addTags = (self.params.addTags || []).concat(tags);
self.toggleAddTags();
} else if (field === 'remove') {
Expand All @@ -153,40 +178,49 @@
})
};

this.toogleIoc = function() {
this.toggleDataType = function () {
if (!this.params.dataType || this.params.dataType.length === 0) {
this.params.dataType = null;
this.state.enableDataType = false;
} else {
this.state.enableDataType = true;
}
};

this.toogleIoc = function () {
this.params.ioc = !this.params.ioc;
this.state.enableIoc = true;
};

this.toogleSighted = function() {
this.toogleSighted = function () {
this.params.sighted = !this.params.sighted;
this.state.enableSighted = true;
};

this.toogleIgnoreSimilarity = function() {
this.toogleIgnoreSimilarity = function () {
this.params.ignoreSimilarity = !this.params.ignoreSimilarity;
this.state.enableIgnoreSimilarity = true;
};

this.toggleTlp = function(value) {
this.toggleTlp = function (value) {
this.params.tlp = value;
this.activeTlp = 'active';
this.state.enableTlp = true;
};

this.toggleAddTags = function() {
this.toggleAddTags = function () {
this.state.enableAddTags = true;
};

this.toggleRemoveTags = function() {
this.toggleRemoveTags = function () {
this.state.enableRemoveTags = true;
};

$scope.$watchCollection('$dialog.params.addTags', function(value) {
$scope.$watchCollection('$dialog.params.addTags', function (value) {
self.params.addTagNames = _.pluck(value, 'text').join(',');
});

$scope.$watchCollection('$dialog.params.removeTags', function(value) {
$scope.$watchCollection('$dialog.params.removeTags', function (value) {
self.params.removeTagNames = _.pluck(value, 'text').join(',');
});
}
Expand Down
Loading

0 comments on commit 7625676

Please sign in to comment.