Small refactor for cdblist controller and views

This commit is contained in:
Jesús Ángel 2019-02-06 13:01:32 +01:00
parent 4d8543db74
commit 65308d7be6
4 changed files with 171 additions and 168 deletions

View File

@ -12,222 +12,231 @@
import * as FileSaver from '../../services/file-saver';
import { stringToObj } from '../../utils/cdblist-to-object';
export function CdbListsController(
$scope,
errorHandler,
appState,
csvReq,
wzTableFilter,
$location,
apiReq,
wazuhConfig,
rulesetHandler
) {
$scope.isObject = item => typeof item === 'object';
export class CdbListsController {
constructor(
$scope,
errorHandler,
appState,
csvReq,
wzTableFilter,
$location,
apiReq,
wazuhConfig,
rulesetHandler
) {
this.$scope = $scope
this.errorHandler = errorHandler
this.appState = appState
this.csvReq = csvReq
this.wzTableFilter = wzTableFilter
this.$location = $location
this.apiReq = apiReq
this.wazuhConfig = wazuhConfig
this.rulesetHandler = rulesetHandler
$scope.appliedFilters = [];
/**
* This performs a search with a given term
*/
$scope.search = term => {
this.appliedFilters = [];
this.searchTerm = '';
this.viewingDetail = false;
this.isArray = Array.isArray;
this.newKey = '';
this.newValue = '';
}
$onInit() {
const configuration = this.wazuhConfig.getConfig();
this.adminMode = !!(configuration || {}).admin;
// Reloading event listener
this.$scope.$on('rulesetIsReloaded', () => {
this.viewingDetail = false;
if (!this.$scope.$$phase) this.$scope.$digest();
});
this.$scope.$on('closeListView', () => {
this.closeDetailView();
});
this.$scope.$on('wazuhShowCdbList', async (ev, parameters) => {
this.currentList = parameters.cdblist;
try {
const data = await this.rulesetHandler.getCdbList(
`${this.currentList.path}/${this.currentList.name}`
);
this.currentList.list = stringToObj(data.data.data);
this.viewingDetail = true;
this.$scope.$emit('setCurrentList', { currentList: this.currentList });
} catch (error) {
this.currentList.list = [];
this.errorHandler.handle(error, '');
}
this.$scope.$broadcast('changeCdbList', { currentList: this.currentList });
if (!this.$scope.$$phase) this.$scope.$digest();
});
const currentLocation = this.$location.search();
if ((currentLocation || {}).listname) {
const incomingList = this.$location.search().listname;
this.$location.search('listname', null);
this.apiReq
.request('get', `/cdblists/${incomingList}`, {})
.then(data => {
this.currentList = data.data.data.items[0];
this.$scope.$emit('setCurrentList', { currentList: this.currentList });
if (
!(Object.keys((this.currentList || {}).details || {}) || []).length
) {
this.currentList.details = false;
}
this.viewingDetail = true;
if (!this.$scope.$$phase) this.$scope.$digest();
})
.catch(() =>
this.errorHandler.handle(
`Error fetching list: ${incomingList} from the Wazuh API`,
'CDB Lists'
)
);
}
}
search(term) {
console.log('TERM:' + term)
if (term && term.startsWith('group:') && term.split('group:')[1].trim()) {
$scope.custom_search = '';
this.custom_search = '';
const filter = { name: 'group', value: term.split('group:')[1].trim() };
$scope.appliedFilters = $scope.appliedFilters.filter(
this.appliedFilters = this.appliedFilters.filter(
item => item.name !== 'group'
);
$scope.appliedFilters.push(filter);
$scope.$broadcast('wazuhFilter', { filter });
this.appliedFilters.push(filter);
this.$scope.$broadcast('wazuhFilter', { filter });
} else if (
term &&
term.startsWith('level:') &&
term.split('level:')[1].trim()
) {
$scope.custom_search = '';
this.custom_search = '';
const filter = { name: 'level', value: term.split('level:')[1].trim() };
$scope.appliedFilters = $scope.appliedFilters.filter(
this.appliedFilters = this.appliedFilters.filter(
item => item.name !== 'level'
);
$scope.appliedFilters.push(filter);
$scope.$broadcast('wazuhFilter', { filter });
this.appliedFilters.push(filter);
this.$scope.$broadcast('wazuhFilter', { filter });
} else if (
term &&
term.startsWith('pci:') &&
term.split('pci:')[1].trim()
) {
$scope.custom_search = '';
this.custom_search = '';
const filter = { name: 'pci', value: term.split('pci:')[1].trim() };
$scope.appliedFilters = $scope.appliedFilters.filter(
this.appliedFilters = this.appliedFilters.filter(
item => item.name !== 'pci'
);
$scope.appliedFilters.push(filter);
$scope.$broadcast('wazuhFilter', { filter });
this.appliedFilters.push(filter);
this.$scope.$broadcast('wazuhFilter', { filter });
} else if (
term &&
term.startsWith('gdpr:') &&
term.split('gdpr:')[1].trim()
) {
$scope.custom_search = '';
this.custom_search = '';
const filter = { name: 'gdpr', value: term.split('gdpr:')[1].trim() };
$scope.appliedFilters = $scope.appliedFilters.filter(
this.appliedFilters = this.appliedFilters.filter(
item => item.name !== 'gdpr'
);
$scope.appliedFilters.push(filter);
$scope.$broadcast('wazuhFilter', { filter });
this.appliedFilters.push(filter);
this.$scope.$broadcast('wazuhFilter', { filter });
} else if (
term &&
term.startsWith('file:') &&
term.split('file:')[1].trim()
) {
$scope.custom_search = '';
this.custom_search = '';
const filter = { name: 'file', value: term.split('file:')[1].trim() };
$scope.appliedFilters = $scope.appliedFilters.filter(
this.appliedFilters = this.appliedFilters.filter(
item => item.name !== 'file'
);
$scope.appliedFilters.push(filter);
$scope.$broadcast('wazuhFilter', { filter });
this.appliedFilters.push(filter);
this.$scope.$broadcast('wazuhFilter', { filter });
} else {
$scope.$broadcast('wazuhSearch', { term, removeFilters: 0 });
this.$scope.$broadcast('wazuhSearch', { term, removeFilters: 0 });
}
};
}
/**
* This show us if new filter is already included in filters
* @param {String} filterName
*/
$scope.includesFilter = filterName =>
$scope.appliedFilters.map(item => item.name).includes(filterName);
includesFilter(filterName) {
return this.appliedFilters.map(item => item.name).includes(filterName);
}
/**
* Get a filter given its name
* @param {String} filterName
*/
$scope.getFilter = filterName => {
const filtered = $scope.appliedFilters.filter(
getFilter(filterName) {
const filtered = this.appliedFilters.filter(
item => item.name === filterName
);
return filtered.length ? filtered[0].value : '';
};
}
/**
* This a the filter given its name
* @param {String} filterName
*/
$scope.removeFilter = filterName => {
$scope.appliedFilters = $scope.appliedFilters.filter(
removeFilter(filterName) {
this.appliedFilters = this.appliedFilters.filter(
item => item.name !== filterName
);
return $scope.$broadcast('wazuhRemoveFilter', { filterName });
};
return this.$scope.$broadcast('wazuhRemoveFilter', { filterName });
}
//Initialization
$scope.searchTerm = '';
$scope.viewingDetail = false;
$scope.isArray = Array.isArray;
$scope.newKey = '';
$scope.newValue = '';
const configuration = wazuhConfig.getConfig();
$scope.adminMode = !!(configuration || {}).admin;
// Reloading event listener
$scope.$on('rulesetIsReloaded', () => {
$scope.viewingDetail = false;
if (!$scope.$$phase) $scope.$digest();
});
$scope.$on('closeListView', () => {
$scope.closeDetailView();
});
/**
/**
* Get full data on CSV format
*/
$scope.downloadCsv = async () => {
async downloadCsv() {
try {
errorHandler.info('Your download should begin automatically...', 'CSV');
const currentApi = JSON.parse(appState.getCurrentAPI()).id;
const output = await csvReq.fetch(
this.errorHandler.info('Your download should begin automatically...', 'CSV');
const currentApi = JSON.parse(this.appState.getCurrentAPI()).id;
const output = await this.csvReq.fetch(
'/cdblists',
currentApi,
wzTableFilter.get()
this.wzTableFilter.get()
);
const blob = new Blob([output], { type: 'text/csv' }); // eslint-disable-line
FileSaver.saveAs(blob, 'cdblists.csv');
return;
} catch (error) {
errorHandler.handle(error, 'Download CSV');
this.errorHandler.handle(error, 'Download CSV');
}
return;
};
}
/**
* This function takes back to the list but adding a filter from the detail view
*/
$scope.addDetailFilter = (name, value) => {
$scope.appliedFilters.push({ name, value });
addDetailFilter(name, value) {
this.appliedFilters.push({ name, value });
// Clear the autocomplete component
$scope.searchTerm = '';
this.searchTerm = '';
// Go back to the list
$scope.closeDetailView();
};
//listeners
$scope.$on('wazuhShowCdbList', async (ev, parameters) => {
$scope.currentList = parameters.cdblist;
try {
const data = await rulesetHandler.getCdbList(
`${$scope.currentList.path}/${$scope.currentList.name}`
);
$scope.currentList.list = stringToObj(data.data.data);
$scope.viewingDetail = true;
$scope.$emit('setCurrentList', { currentList: $scope.currentList });
} catch (error) {
$scope.currentList.list = [];
errorHandler.handle(error, '');
}
$scope.$broadcast('changeCdbList', { currentList: $scope.currentList });
if (!$scope.$$phase) $scope.$digest();
});
this.closeDetailView();
}
/**
* This function changes to the lists list view
*/
$scope.closeDetailView = clear => {
closeDetailView(clear) {
if (clear)
$scope.appliedFilters = $scope.appliedFilters.slice(
this.appliedFilters = this.appliedFilters.slice(
0,
$scope.appliedFilters.length - 1
);
$scope.viewingDetail = false;
$scope.currentList = false;
$scope.$emit('removeCurrentList');
if (!$scope.$$phase) $scope.$digest();
};
if ($location.search() && $location.search().listname) {
const incomingList = $location.search().listname;
$location.search('listname', null);
apiReq
.request('get', `/cdblists/${incomingList}`, {})
.then(data => {
$scope.currentList = data.data.data.items[0];
$scope.$emit('setCurrentList', { currentList: $scope.currentList });
if (
!(Object.keys(($scope.currentList || {}).details || {}) || []).length
) {
$scope.currentList.details = false;
}
$scope.viewingDetail = true;
if (!$scope.$$phase) $scope.$digest();
})
.catch(() =>
errorHandler.handle(
`Error fetching list: ${incomingList} from the Wazuh API`,
'CDB Lists'
)
this.appliedFilters.length - 1
);
this.viewingDetail = false;
this.currentList = false;
this.$scope.$emit('removeCurrentList');
if (!this.$scope.$$phase) this.$scope.$digest();
}
}

View File

@ -1,14 +1,14 @@
<div ng-if="!loading && viewingDetail" layout="column" class="">
<div ng-if="cdbctrl.viewingDetail" layout="column" class="">
<!-- Back button and title -->
<div layout="row" layout-align="start center">
<!-- Back button -->
<md-button class="md-icon-button" style="margin: 5px!important;" aria-label="Back to CDB Lists list"
tooltip="Back" tooltip-placement="bottom" ng-click="closeDetailView(true)"><i class="fa fa-fw fa-arrow-left"
tooltip="Back" tooltip-placement="bottom" ng-click="cdbctrl.closeDetailView(true)"><i class="fa fa-fw fa-arrow-left"
aria-hidden="true"></i></md-button>
<!-- List title -->
<div>
<!-- <h1 class="font-size-18">{{currentList.name}}</h1> -->
<h1 class="font-size-18">{{currentList.name}}</h1>
<h1 class="font-size-18">{{cdbctrl.currentList.name}}</h1>
</div>
</div>
<!-- End back button, title and status indicator -->
@ -18,13 +18,13 @@
<div layout="row" class="wz-padding-left-8 wz-padding-right-8">
<md-card flex class="wz-metric-color wz-md-card">
<md-card-content layout="row" class="wz-padding-metric">
<div flex="50" class="wz-text-truncatable">Name: <span class="wz-text-bold wz-text-link">{{currentList.name}}</span></div>
<div flex="50" class="wz-text-truncatable">Path: <span class="wz-text-bold wz-text-link">{{currentList.path}}</span></div>
<div flex="50" class="wz-text-truncatable">Name: <span class="wz-text-bold wz-text-link">{{cdbctrl.currentList.name}}</span></div>
<div flex="50" class="wz-text-truncatable">Path: <span class="wz-text-bold wz-text-link">{{cdbctrl.currentList.path}}</span></div>
</md-card-content>
</md-card>
</div>
<!-- End List information ribbon -->
<wz-list-manage ng-if="currentList" list="currentList"></wz-list-manage>
<wz-list-manage ng-if="cdbctrl.currentList" list="cdbctrl.currentList"></wz-list-manage>
<br>
</div>

View File

@ -1,41 +1,40 @@
<div ng-if="!loading && !viewingDetail" layout="column">
<div ng-if="!cdbctrl.viewingDetail" layout="column">
<div id="content" layout="row" class="md-padding ">
<input flex placeholder="Filter lists..." ng-model="custom_search" type="text" class="kuiLocalSearchInput height-40 ng-empty ng-pristine ng-scope ng-touched ng-valid"
aria-invalid="false" wz-enter="search(custom_search)">
<button type="submit" aria-label="Search" class="kuiLocalSearchButton height-40" ng-click="search(custom_search)">
<input flex placeholder="Filter lists..." ng-model="cdbctrl.custom_search" type="text" class="kuiLocalSearchInput height-40 ng-empty ng-pristine ng-scope ng-touched ng-valid" aria-invalid="false" wz-enter="cdbctrl.search(cdbctrl.custom_search)">
<button type="submit" aria-label="Search" class="kuiLocalSearchButton height-40" ng-click="cdbctrl.search(cdbctrl.custom_search)">
<span class="fa fa-search" aria-hidden="true"></span>
</button>
</div>
<md-chips class="wz-chips md-padding-h" readonly="true" ng-show="appliedFilters.length">
<md-chip class="wz-chip" ng-show="includesFilter('file')">
<span>File: {{getFilter('file')}}
<i class="fa fa-fw fa-times cursor-pointer" aria-hidden="true" ng-click="removeFilter('file')"></i>
<md-chips class="wz-chips md-padding-h" readonly="true" ng-show="cdbctrl.appliedFilters.length">
<md-chip class="wz-chip" ng-show="cdbctrl.includesFilter('file')">
<span>File: {{cdbctrl.getFilter('file')}}
<i class="fa fa-fw fa-times cursor-pointer" aria-hidden="true" ng-click="cdbctrl.removeFilter('file')"></i>
</span>
</md-chip>
<md-chip class="wz-chip" ng-show="includesFilter('path')">
<span>Path: {{getFilter('path')}}
<i class="fa fa-fw fa-times cursor-pointer" aria-hidden="true" ng-click="removeFilter('path')"></i>
<md-chip class="wz-chip" ng-show="cdbctrl.includesFilter('path')">
<span>Path: {{cdbctrl.getFilter('path')}}
<i class="fa fa-fw fa-times cursor-pointer" aria-hidden="true" ng-click="cdbctrl.removeFilter('path')"></i>
</span>
</md-chip>
<md-chip class="wz-chip" ng-show="includesFilter('level')">
<span>Level: {{getFilter('level')}}
<i class="fa fa-fw fa-times cursor-pointer" aria-hidden="true" ng-click="removeFilter('level')"></i>
<md-chip class="wz-chip" ng-show="cdbctrl.includesFilter('level')">
<span>Level: {{cdbctrl.getFilter('level')}}
<i class="fa fa-fw fa-times cursor-pointer" aria-hidden="true" ng-click="cdbctrl.removeFilter('level')"></i>
</span>
</md-chip>
<md-chip class="wz-chip" ng-show="includesFilter('group')">
<span>Group: {{getFilter('group')}}
<i class="fa fa-fw fa-times cursor-pointer" aria-hidden="true" ng-click="removeFilter('group')"></i>
<md-chip class="wz-chip" ng-show="cdbctrl.includesFilter('group')">
<span>Group: {{cdbctrl.getFilter('group')}}
<i class="fa fa-fw fa-times cursor-pointer" aria-hidden="true" ng-click="cdbctrl.removeFilter('group')"></i>
</span>
</md-chip>
<md-chip class="wz-chip" ng-show="includesFilter('pci')">
<span>PCI control: {{getFilter('pci')}}
<i class="fa fa-fw fa-times cursor-pointer" aria-hidden="true" ng-click="removeFilter('pci')"></i>
<md-chip class="wz-chip" ng-show="cdbctrl.includesFilter('pci')">
<span>PCI control: {{cdbctrl.getFilter('pci')}}
<i class="fa fa-fw fa-times cursor-pointer" aria-hidden="true" ng-click="cdbctrl.removeFilter('pci')"></i>
</span>
</md-chip>
<md-chip class="wz-chip" ng-show="includesFilter('gdpr')">
<span>GDPR: {{getFilter('gdpr')}}
<i class="fa fa-fw fa-times cursor-pointer" aria-hidden="true" ng-click="removeFilter('gdpr')"></i>
<md-chip class="wz-chip" ng-show="cdbctrl.includesFilter('gdpr')">
<span>GDPR: {{cdbctrl.getFilter('gdpr')}}
<i class="fa fa-fw fa-times cursor-pointer" aria-hidden="true" ng-click="cdbctrl.removeFilter('gdpr')"></i>
</span>
</md-chip>
</md-chips>
@ -43,12 +42,11 @@
<div layout="row">
<md-card flex class="wz-md-card _md flex md-margin-h">
<md-card-content>
<wz-table implicit-filter="appliedFilters" flex path="'/lists/files'" keys="['name', 'path']"
allow-click="true" row-sizes="[15,13,11]">
<wz-table implicit-filter="cdbctrl.appliedFilters" flex path="'/lists/files'" keys="['name', 'path']" allow-click="true" row-sizes="[15,13,11]">
</wz-table>
<div layout="row" class="wz-margin-top-10 md-padding-h">
<span flex></span>
<a class="small" id="btnDownload" ng-click="downloadCsv()">Formatted <i aria-hidden="true" class="fa fa-download"></i></a>
<a class="small" id="btnDownload" ng-click="cdbctrl.downloadCsv()">Formatted <i aria-hidden="true" class="fa fa-download"></i></a>
</div>
</md-card-content>
</md-card>

View File

@ -1,6 +1,2 @@
<div flex ng-if="!loading && mctrl.globalRuleSet == 'ruleset' && mctrl.globalRulesetTab == 'cdblists'" class="" ng-controller="cdbListsController"
<div flex ng-if="mctrl.globalRuleSet == 'ruleset' && mctrl.globalRulesetTab == 'cdblists'" class="" ng-controller="cdbListsController as cdbctrl"
layout="column" id="rulesContainer" layout-align="start space-around">
<div class='uil-ring-css' ng-show="loading">
<div></div>
</div>