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

fix(filter): handle name when saving criterias hg/sg #10330

Merged
merged 14 commits into from
Oct 20, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 $hostGroupIds): array
{
try {
return $this->hostGroupRepository->findHostGroupsByNames($hostGroupIds);
adr-mo marked this conversation as resolved.
Show resolved Hide resolved
} 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 array<int, int> $hostGroupIds
adr-mo marked this conversation as resolved.
Show resolved Hide resolved
* @return HostGroup[]
*/
public function findHostGroupsByIds(array $hostGroupIds): array;

/**
* Retrieve hostgroups from their names
*
* @param array<int, string> $hostGroupNames
adr-mo marked this conversation as resolved.
Show resolved Hide resolved
* @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 array<int, int> $hostGroupIds
adr-mo marked this conversation as resolved.
Show resolved Hide resolved
* @return HostGroup[]
* @throws HostGroupException
*/
public function findHostGroupsByIds(array $hostGroupIds): array;

/**
* Retrieve hostgroups from their names
*
* @param array<int, string> $hostGroupNames
adr-mo marked this conversation as resolved.
Show resolved Hide resolved
* @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 array<int, int> $serviceGroupIds
adr-mo marked this conversation as resolved.
Show resolved Hide resolved
* @return ServiceGroup[]
*/
public function findServiceGroupsByIds(array $serviceGroupIds): array;

/**
* Retrieve servicegroups from their names
*
* @param array<int, string> $serviceGroupNames
adr-mo marked this conversation as resolved.
Show resolved Hide resolved
* @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 array<int, int> $serviceGroupIds
adr-mo marked this conversation as resolved.
Show resolved Hide resolved
* @return ServiceGroup[]
* @throws ServiceGroupException
*/
public function findServiceGroupsByIds(array $serviceGroupIds): array;

/**
* Retrieve servicegroups from their ids
*
* @param array<int, string> $serviceGroupNames
adr-mo marked this conversation as resolved.
Show resolved Hide resolved
* @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 @@ -65,6 +65,76 @@ public function filterByAccessGroups(?array $accessGroups): HostGroupRepositoryI
$this->accessGroups = $accessGroups;
return $this;
}
public function findHostGroupsByNames(array $hostGroupNames): array
adr-mo marked this conversation as resolved.
Show resolved Hide resolved
{
$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
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