wazuh-kibana-app/public/controllers/groups.js

169 lines
5.4 KiB
JavaScript
Raw Normal View History

2017-10-30 09:08:42 +00:00
let app = require('ui/modules').get('app/wazuh', []);
2017-11-07 14:13:45 +00:00
const beautifier = require('plugins/wazuh/utils/json-beautifier');
2017-10-20 19:04:22 +00:00
// Groups preview controller
app.controller('groupsPreviewController',
function ($scope, $timeout, $rootScope,$mdSidenav, $location, apiReq, Groups, GroupFiles, GroupAgents, Notifier) {
const notify = new Notifier({ location: 'Manager - Groups' });
2017-11-02 08:12:53 +00:00
$scope.searchTerm = '';
$scope.searchTermAgent = '';
2017-11-02 08:12:53 +00:00
$scope.searchTermFile = '';
$scope.load = true;
$scope.groups = Groups;
$scope.groupAgents = GroupAgents;
$scope.groupFiles = GroupFiles;
2017-10-20 19:04:22 +00:00
2017-12-11 09:15:21 +00:00
// Store a boolean variable to check if come from agents
const fromAgents = ('comeFrom' in $rootScope) && ('globalAgent' in $rootScope) && $rootScope.comeFrom === 'agents';
// If come from agents
if(fromAgents) {
let len = 0;
// Get ALL groups
apiReq.request('GET','/agents/groups/',{limit:99999})
.then(data => {
// Obtain an array with 0 or 1 element, in that case is our group
let filtered = data.data.data.items.filter(group => group.name === $rootScope.globalAgent.group);
// Store the array length, should be 0 or 1
len = filtered.length;
// If len is 1
if(len){
// First element is now our group $scope.groups.item is an array with only our group
$scope.groups.items = filtered;
// Load that our group
$scope.loadGroup(0);
$scope.lookingGroup=true
}
// Clean $rootScope
delete $rootScope.globalAgent;
delete $rootScope.comeFrom;
// Get more groups to fill the md-content with more items
return $scope.groups.nextPage('')
})
.then(() => {
// If our group was not found we need to call loadGroup after load some groups
2017-12-11 09:15:21 +00:00
if(!len) {
$scope.loadGroup(0);
$scope.lookingGroup=true
2017-12-11 09:15:21 +00:00
}
$scope.load = false;
})
.catch(error => notify.error(error.message));
// If not come from agents make as normal
} else {
// Actual execution in the controller's initialization
$scope.groups.nextPage('')
2017-12-11 09:15:21 +00:00
.then(() => {
$scope.loadGroup(0);
$scope.load = false;
})
.catch(error => notify.error(error.message));
}
2017-10-20 19:04:22 +00:00
2018-01-11 17:11:05 +00:00
$scope.toggle = () => $scope.lookingGroup=true;
2017-10-20 19:04:22 +00:00
2017-10-30 09:08:42 +00:00
$scope.showFiles = (index) => {
$scope.fileViewer = false;
$scope.groupFiles.reset();
2017-10-30 09:08:42 +00:00
$scope.groupFiles.path = `/agents/groups/${$scope.groups.items[index].name}/files`;
$scope.groupFiles.nextPage('');
2017-10-30 09:08:42 +00:00
};
2017-10-20 19:04:22 +00:00
$scope.showAgents = (index) => {
$scope.fileViewer = false;
$scope.groupAgents.reset();
2017-10-30 09:08:42 +00:00
$scope.groupAgents.path = `/agents/groups/${$scope.groups.items[index].name}`;
$scope.groupAgents.nextPage('');
};
2017-10-20 19:04:22 +00:00
$scope.showAgent = agent => {
$rootScope.globalAgent = agent.id;
$rootScope.comeFrom = 'groups';
$location.search('tab', null);
$location.path('/agents');
};
$scope.loadGroup = (index) => {
$scope.fileViewer = false;
$scope.groupAgents.reset();
$scope.groupFiles.reset();
$scope.selectedGroup = index;
$scope.showFiles(index);
$scope.showAgents(index);
};
2017-10-20 19:04:22 +00:00
// Select specific group
$scope.checkSelected = (index) => {
for(let group of $scope.groups.items){
if (group.selected) {
group = false;
}
}
$scope.groups.items[index] = true;
};
2017-10-20 19:04:22 +00:00
2018-01-11 17:11:05 +00:00
$scope.goBackToAgents = () => {
2018-01-12 15:01:24 +00:00
$scope.groupsSelectedTab = 'agents';
2018-01-11 17:11:05 +00:00
$scope.file = false;
$scope.filename = false;
if(!$scope.$$phase) $scope.$digest();
}
$scope.goBackFiles = () => {
$scope.groupsSelectedTab = 'files';
$scope.file = false;
$scope.filename = false;
if(!$scope.$$phase) $scope.$digest();
}
$scope.showFile = (index) => {
if($scope.filename) $scope.filename = '';
2017-11-06 15:48:18 +00:00
let filename = $scope.groupFiles.items[index].filename;
if(filename === '../ar.conf') filename = 'ar.conf';
$scope.fileViewer = true;
2018-01-11 17:11:05 +00:00
let tmpName = `/agents/groups/${$scope.groups.items[$scope.selectedGroup].name}`+
2017-11-06 15:48:18 +00:00
`/files/${filename}`;
apiReq.request('GET', tmpName, {})
2017-11-20 12:32:24 +00:00
.then(data => {
$scope.file = beautifier.prettyPrint(data.data.data);
$scope.filename = filename;
})
.catch(error => notify.error(error.message));
};
2017-10-20 19:04:22 +00:00
// Changing the view to overview a specific group
$scope.groupOverview = (group) => {
$scope.$parent.$parent.groupName = group;
2017-10-30 09:08:42 +00:00
$scope.$parent.$parent.groupsMenu = 'overview';
};
// Resetting the factory configuration
$scope.$on("$destroy", () => {
$scope.groups.reset();
$scope.groupFiles.reset();
$scope.groupAgents.reset();
});
2017-12-11 09:15:21 +00:00
2018-01-12 15:01:24 +00:00
$scope.$watch('lookingGroup',value => {
if(!value){
$scope.file = false;
$scope.filename = false;
}
});
2017-10-20 19:04:22 +00:00
});
app.controller('groupsController', function ($scope) {
2017-10-30 09:08:42 +00:00
$scope.groupsMenu = 'preview';
$scope.groupName = '';
});