Skip to content
This repository has been archived by the owner on Dec 13, 2022. It is now read-only.

Commit

Permalink
fix(filter): handle name when saving criterias hg/sg (#10330)
Browse files Browse the repository at this point in the history
* fix(filter): handle name when saving criterias hg/sg

* fix unit tests

* centreon-bot: fix feedbacks

* Update src/Centreon/Domain/Monitoring/HostGroup/HostGroupService.php

Co-authored-by: alaunois <alaunois@centreon.com>

* Update src/Centreon/Domain/Monitoring/ServiceGroup/Interfaces/ServiceGroupRepositoryInterface.php

Co-authored-by: Laurent Calvet <lcalvet@centreon.com>

* Update src/Centreon/Domain/Monitoring/ServiceGroup/Interfaces/ServiceGroupServiceInterface.php

Co-authored-by: Laurent Calvet <lcalvet@centreon.com>

* Update src/Centreon/Domain/Monitoring/ServiceGroup/Interfaces/ServiceGroupRepositoryInterface.php

Co-authored-by: Laurent Calvet <lcalvet@centreon.com>

* Update src/Centreon/Domain/Monitoring/HostGroup/Interfaces/HostGroupServiceInterface.php

Co-authored-by: Laurent Calvet <lcalvet@centreon.com>

* Update src/Centreon/Domain/Monitoring/HostGroup/Interfaces/HostGroupServiceInterface.php

Co-authored-by: Laurent Calvet <lcalvet@centreon.com>

* Update src/Centreon/Domain/Monitoring/HostGroup/Interfaces/HostGroupRepositoryInterface.php

Co-authored-by: Laurent Calvet <lcalvet@centreon.com>

* Update src/Centreon/Domain/Monitoring/HostGroup/Interfaces/HostGroupRepositoryInterface.php

Co-authored-by: Laurent Calvet <lcalvet@centreon.com>

* Update src/Centreon/Domain/Monitoring/ServiceGroup/Interfaces/ServiceGroupServiceInterface.php

* code-review: take kdus feedback into account

* try to fix API acceptance test

Co-authored-by: alaunois <alaunois@centreon.com>
Co-authored-by: Laurent Calvet <lcalvet@centreon.com>
  • Loading branch information
3 people committed Oct 22, 2021
1 parent f92dc8f commit b14eb23
Show file tree
Hide file tree
Showing 11 changed files with 240 additions and 11 deletions.
8 changes: 4 additions & 4 deletions src/Centreon/Domain/Filter/FilterService.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,10 @@ public function checkCriterias(array $criterias): void
if ($criteria->getType() === 'multi_select' && is_array($criteria->getValue())) {
switch ($criteria->getObjectType()) {
case 'host_groups':
$hostGroupIds = array_column($criteria->getValue(), 'id');
$hostGroupNames = array_column($criteria->getValue(), 'name');
$hostGroups = $this->hostGroupService
->filterByContact($this->contact)
->findHostGroupsByIds($hostGroupIds);
->findHostGroupsByNames($hostGroupNames);
$criteria->setValue(array_map(
function ($hostGroup) {
return [
Expand All @@ -148,10 +148,10 @@ function ($hostGroup) {
));
break;
case 'service_groups':
$serviceGroupIds = array_column($criteria->getValue(), 'id');
$serviceGroupNames = array_column($criteria->getValue(), 'name');
$serviceGroups = $this->serviceGroupService
->filterByContact($this->contact)
->findServiceGroupsByIds($serviceGroupIds);
->findServiceGroupsByNames($serviceGroupNames);
$criteria->setValue(array_map(
function ($serviceGroup) {
return [
Expand Down
12 changes: 12 additions & 0 deletions src/Centreon/Domain/Monitoring/HostGroup/HostGroupService.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,16 @@ public function findHostGroupsByIds(array $hostGroupIds): array
throw new HostGroupException(_('Error when searching hostgroups'), 0, $e);
}
}

/**
* @inheritDoc
*/
public function findHostGroupsByNames(array $hostGroupNames): array
{
try {
return $this->hostGroupRepository->findHostGroupsByNames($hostGroupNames);
} catch (\Throwable $e) {
throw new HostGroupException(_('Error when searching hostgroups'), 0, $e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,19 @@ interface HostGroupRepositoryInterface
/**
* Retrieve hostgroups from their ids
*
* @param array $hostGroupIds
* @param int[] $hostGroupIds
* @return HostGroup[]
*/
public function findHostGroupsByIds(array $hostGroupIds): array;

/**
* Retrieve hostgroups from their names
*
* @param string[] $hostGroupNames
* @return HostGroup[]
*/
public function findHostGroupsByNames(array $hostGroupNames): array;

/**
* @param ContactInterface $contact
* @return HostGroupRepositoryInterface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,18 @@ public function filterByContact($contact): HostGroupServiceInterface;
/**
* Retrieve hostgroups from their ids
*
* @param array $hostGroupIds
* @param int[] $hostGroupIds
* @return HostGroup[]
* @throws HostGroupException
*/
public function findHostGroupsByIds(array $hostGroupIds): array;

/**
* Retrieve hostgroups from their names
*
* @param string[] $hostGroupNames
* @return HostGroup[]
* @throws HostGroupException
*/
public function findHostGroupsByNames(array $hostGroupNames): array;
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,19 @@ interface ServiceGroupRepositoryInterface
/**
* Retrieve servicegroups from their ids
*
* @param array $serviceGroupIds
* @param int[] $serviceGroupIds
* @return ServiceGroup[]
*/
public function findServiceGroupsByIds(array $serviceGroupIds): array;

/**
* Retrieve servicegroups from their names
*
* @param string[] $serviceGroupNames
* @return ServiceGroup[]
*/
public function findServiceGroupsByNames(array $serviceGroupNames): array;

/**
* @param ContactInterface $contact
* @return ServiceGroupRepositoryInterface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,18 @@ public function filterByContact($contact): ServiceGroupServiceInterface;
/**
* Retrieve servicegroups from their ids
*
* @param array $serviceGroupIds
* @param int[] $serviceGroupIds
* @return ServiceGroup[]
* @throws ServiceGroupException
*/
public function findServiceGroupsByIds(array $serviceGroupIds): array;

/**
* Retrieve servicegroups from their ids
*
* @param string[] $serviceGroupNames
* @return ServiceGroup[]
* @throws ServiceGroupException
*/
public function findServiceGroupsByNames(array $serviceGroupNames): array;
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,16 @@ public function findServiceGroupsByIds(array $serviceGroupIds): array
throw new ServiceGroupException(_('Error when searching servicegroups'), 0, $e);
}
}

/**
* @inheritDoc
*/
public function findServiceGroupsByNames(array $serviceGroupNames): array
{
try {
return $this->serviceGroupRepository->findServiceGroupsByNames($serviceGroupNames);
} catch (\Throwable $e) {
throw new ServiceGroupException(_('Error when searching servicegroups'), 0, $e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,80 @@ public function filterByAccessGroups(?array $accessGroups): HostGroupRepositoryI
return $this;
}

/**
* @inheritDoc
*/
public function findHostGroupsByNames(array $hostGroupNames): array
{
$hostGroups = [];

if ($this->hasNotEnoughRightsToContinue() || empty($hostGroupNames)) {
return $hostGroups;
}

$bindValues = [];
$subRequest = '';
if (!$this->isAdmin()) {
$bindValues[':contact_id'] = [\PDO::PARAM_INT => $this->contact->getId()];

// Not an admin, we must to filter on contact
$subRequest .=
' INNER JOIN `:db`.acl_resources_hg_relations hgr
ON hgr.hg_hg_id = hg.hostgroup_id
INNER JOIN `:db`.acl_resources res
ON res.acl_res_id = hgr.acl_res_id
AND res.acl_res_activate = \'1\'
INNER JOIN `:db`.acl_res_group_relations rgr
ON rgr.acl_res_id = res.acl_res_id
INNER JOIN `:db`.acl_groups grp
ON grp.acl_group_id IN ('
. $this->accessGroupIdToString($this->accessGroups)
. ') AND grp.acl_group_activate = \'1\'
AND grp.acl_group_id = rgr.acl_group_id
LEFT JOIN `:db`.acl_group_contacts_relations gcr
ON gcr.acl_group_id = grp.acl_group_id
LEFT JOIN `:db`.acl_group_contactgroups_relations gcgr
ON gcgr.acl_group_id = grp.acl_group_id
LEFT JOIN `:db`.contactgroup_contact_relation cgcr
ON cgcr.contactgroup_cg_id = gcgr.cg_cg_id
AND cgcr.contact_contact_id = :contact_id
OR gcr.contact_contact_id = :contact_id';
}

$request = 'SELECT SQL_CALC_FOUND_ROWS DISTINCT hg.* FROM `:dbstg`.`hostgroups` hg ' . $subRequest;
$request = $this->translateDbName($request);

$bindHostGroupNames = [];
foreach ($hostGroupNames as $index => $hostGroupName) {
$bindHostGroupNames[':host_group_name_' . $index] = [\PDO::PARAM_STR => $hostGroupName];
}
$bindValues = array_merge($bindValues, $bindHostGroupNames);
$request .= ' WHERE hg.name IN (' . implode(',', array_keys($bindHostGroupNames)) . ')';

// Sort
$request .= ' ORDER BY hg.name ASC';

$statement = $this->db->prepare($request);

// We bind extra parameters according to access rights
foreach ($bindValues as $key => $data) {
$type = key($data);
$value = $data[$type];
$statement->bindValue($key, $value, $type);
}

$statement->execute();

while (false !== ($result = $statement->fetch(\PDO::FETCH_ASSOC))) {
$hostGroups[] = EntityCreator::createEntityByArray(
HostGroup::class,
$result
);
}

return $hostGroups;
}

/**
* @inheritDoc
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,80 @@ public function findServiceGroupsByIds(array $serviceGroupIds): array
return $serviceGroups;
}

/**
* @inheritDoc
*/
public function findServiceGroupsByNames(array $serviceGroupNames): array
{
$serviceGroups = [];

if ($this->hasNotEnoughRightsToContinue() || empty($serviceGroupNames)) {
return $serviceGroups;
}

$bindValues = [];
$subRequest = '';
if (!$this->isAdmin()) {
$bindValues[':contact_id'] = [\PDO::PARAM_INT => $this->contact->getId()];

// Not an admin, we must to filter on contact
$subRequest .=
' INNER JOIN `:db`.acl_resources_sg_relations sgr
ON sgr.sg_id = sg.servicegroup_id
INNER JOIN `:db`.acl_resources res
ON res.acl_res_id = sgr.acl_res_id
AND res.acl_res_activate = \'1\'
INNER JOIN `:db`.acl_res_group_relations rgr
ON rgr.acl_res_id = res.acl_res_id
INNER JOIN `:db`.acl_groups grp
ON grp.acl_group_id IN ('
. $this->accessGroupIdToString($this->accessGroups)
. ') AND grp.acl_group_activate = \'1\'
AND grp.acl_group_id = rgr.acl_group_id
LEFT JOIN `:db`.acl_group_contacts_relations gcr
ON gcr.acl_group_id = grp.acl_group_id
LEFT JOIN `:db`.acl_group_contactgroups_relations gcgr
ON gcgr.acl_group_id = grp.acl_group_id
LEFT JOIN `:db`.contactgroup_contact_relation cgcr
ON cgcr.contactgroup_cg_id = gcgr.cg_cg_id
AND cgcr.contact_contact_id = :contact_id
OR gcr.contact_contact_id = :contact_id';
}

$request = 'SELECT SQL_CALC_FOUND_ROWS DISTINCT sg.* FROM `:dbstg`.`servicegroups` sg ' . $subRequest;
$request = $this->translateDbName($request);

$bindServiceGroupNames = [];
foreach ($serviceGroupNames as $index => $serviceGroupName) {
$bindServiceGroupNames[':service_group_name_' . $index] = [\PDO::PARAM_STR => $serviceGroupName];
}
$bindValues = array_merge($bindValues, $bindServiceGroupNames);
$request .= ' WHERE sg.name IN (' . implode(',', array_keys($bindServiceGroupNames)) . ')';

// Sort
$request .= ' ORDER BY sg.name ASC';

$statement = $this->db->prepare($request);

// We bind extra parameters according to access rights
foreach ($bindValues as $key => $data) {
$type = key($data);
$value = $data[$type];
$statement->bindValue($key, $value, $type);
}

$statement->execute();

while (false !== ($result = $statement->fetch(\PDO::FETCH_ASSOC))) {
$serviceGroups[] = EntityCreator::createEntityByArray(
ServiceGroup::class,
$result
);
}

return $serviceGroups;
}

/**
* Check if the contact is admin
*
Expand Down
25 changes: 24 additions & 1 deletion tests/api/Context/UserFilterContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,33 @@ public function iAddAfilterLinkedToHostgroup(string $hostgroupName): void
*/
public function iUpdateTheFilterWithTheCreationValues(): void
{
$response = $this->iSendARequestTo(
'GET',
'/api/v21.10/monitoring/hostgroups'
);
$decodedResponse = json_decode($response->getBody()->__toString(), true);
$hostgroupId = $decodedResponse['result'][0]['id'];
$hostgroupName = $decodedResponse['result'][0]['name'];

$requestBody = '{
"name":"my filter1",
"criterias":[{
"name": "host_groups",
"type": "multi_select",
"value": [
{
"id": ' . $hostgroupId . ',
"name": "' . $hostgroupName . '"
}
],
"object_type": "host_groups"
}]
}';

$this->iSendARequestToWithBody(
'PUT',
'/api/v21.10/users/filters/events-view/1',
$this->requestBody
$requestBody
);
}
}
4 changes: 2 additions & 2 deletions tests/php/Centreon/Domain/Filter/FilterServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public function testCheckCriteriasRenamedObjects()
->method('filterByContact')
->willReturn($this->hostGroupService);
$this->hostGroupService->expects($this->once())
->method('findHostGroupsByIds')
->method('findHostGroupsByNames')
->willReturn([$renamedHostGroup]);

$filterService = new FilterService(
Expand Down Expand Up @@ -151,7 +151,7 @@ public function testCheckCriteriasDeletedObjects()
->method('filterByContact')
->willReturn($this->serviceGroupService);
$this->serviceGroupService->expects($this->once())
->method('findServiceGroupsByIds')
->method('findServiceGroupsByNames')
->willReturn([]);

$filterService = new FilterService(
Expand Down

0 comments on commit b14eb23

Please sign in to comment.