Skip to content
This repository was archived by the owner on Aug 9, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions inc/exception/entityruleeditionexception.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

namespace GlpiPlugin\Flyvemdm\Exception;


class EntityRuleEditionException extends \Exception {}
41 changes: 40 additions & 1 deletion inc/fusioninventory.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
* ------------------------------------------------------------------------------
*/

use GlpiPlugin\Flyvemdm\Exception\FusionInventoryRuleInconsistency;
use GlpiPlugin\Flyvemdm\Exception\FusionInventoryRuleInconsistency;
use GlpiPlugin\Flyvemdm\Exception\EntityRuleEditionException;

if (!defined('GLPI_ROOT')) {
die("Sorry. You can't access this file directly");
Expand All @@ -39,6 +40,9 @@ class PluginFlyvemdmFusionInventory {

const RULE_NAME = 'Flyve MDM invitation to entity';

const LOCK_NAME = 'flyvemdm_entityRule';


/**
* Creates or updates an entity rule
*
Expand All @@ -52,6 +56,10 @@ public function addInvitationRule(PluginFlyvemdmInvitation $invitation) {
Session::addMessageAfterRedirect(__('Unable to get rule for entity', 'flyvemdm'),
true, ERROR);
return;
} catch (EntityRuleEditionException $exception) {
Session::addMessageAfterRedirect(__('Cannot get lock for entity rules edition', 'flyvemdm'),
true, ERROR);
return;
}

$ruleCriteria = new RuleCriteria();
Expand Down Expand Up @@ -117,6 +125,19 @@ public function deleteInvitationRuleCriteria(PluginFlyvemdmInvitation $invitatio
]);

$ruleId = $row[$ruleFk];

// get a lock
$attempts = 0;
$locked = 0;
do {
$locked = $DB->getLock(self::LOCK_NAME);
usleep(50000); // 50 milliseconds
$attempts++;
} while ($locked !== 1 && $attempts < 10);
if ($locked !== 1) {
return; // No lock, then give up disabling
}

$rows = $ruleCriteria->find("`$ruleFk` = '$ruleId' AND `criteria` = 'tag' AND `condition` = '0'");
if (count($rows) === 0) {
$rule = new PluginFusioninventoryInventoryRuleEntity();
Expand All @@ -125,6 +146,7 @@ public function deleteInvitationRuleCriteria(PluginFlyvemdmInvitation $invitatio
'is_active' => '0',
]);
}
$DB->releaseLock(self::LOCK_NAME);
}

/**
Expand All @@ -144,10 +166,23 @@ private function getRuleCriteriaValue(PluginFlyvemdmInvitation $invitation) {
*
* @return PluginFusioninventoryInventoryRuleEntity|null
* @throws FusionInventoryRuleInconsistency
* @throws EntityRuleEditionException
*/
private function getRule($entityId, $create = true) {
global $DB;

// get a lock
$attempts = 0;
$locked = 0;
do {
$locked = $DB->getLock(self::LOCK_NAME);
usleep(50000); // 50 milliseconds
$attempts++;
} while ($locked !== 1 && $attempts < 10);
if ($locked !== 1) {
throw new EntityRuleEditionException(__('Cannot get lock for entity rules edition'));
}

$ruleEntityTable = PluginFusioninventoryInventoryRuleEntity::getTable();
$ruleActionTable = RuleAction::getTable();
$request = [
Expand Down Expand Up @@ -175,13 +210,16 @@ private function getRule($entityId, $create = true) {
$rule = new PluginFusioninventoryInventoryRuleEntity();
$row = $result->next();
$rule->getFromDB($row['id']);
$DB->releaseLock(self::LOCK_NAME);
return $rule;
}
if ($result->count() > 1) {
$DB->releaseLock(self::LOCK_NAME);
throw new FusionInventoryRuleInconsistency(__('Import rule is not unique'));
}

if (!$create) {
$DB->releaseLock(self::LOCK_NAME);
return null;
}

Expand All @@ -203,6 +241,7 @@ private function getRule($entityId, $create = true) {
'field' => Entity::getForeignKeyField(),
'value' => $entityId,
]);
$DB->releaseLock(self::LOCK_NAME);
return $rule;
}
}