Skip to content
This repository has been archived by the owner on Aug 9, 2021. It is now read-only.

Commit

Permalink
fix(policies): update code for deploy and remove applications
Browse files Browse the repository at this point in the history
Signed-off-by: Domingo Oropeza <doropeza@teclib.com>
  • Loading branch information
DIOHz0r committed Mar 26, 2018
1 parent d3eec7d commit 566c7a4
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 211 deletions.
9 changes: 7 additions & 2 deletions inc/policybase.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@
abstract class PluginFlyvemdmPolicyBase implements PluginFlyvemdmPolicyInterface {

/**
* @var bool $unicityRequired if true The policy cannot be applied more than once to a fleet
* @var bool $canApply if true the policy can be applied
*/
protected $canApply = true;

/**
* @var bool $unicityRequired if true the policy cannot be applied more than once to a fleet
*/
protected $unicityRequired = true;

Expand Down Expand Up @@ -122,7 +127,7 @@ protected function jsonDecodeProperties($properties, array $defaultProperties) {
* @return bool
*/
public function canApply(PluginFlyvemdmFleet $fleet, $value, $itemtype, $itemId) {
return true;
return $this->canApply;
}

/**
Expand Down
5 changes: 3 additions & 2 deletions inc/policydeployapplication.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ public function unicityCheck($value, $itemtype, $itemId, PluginFlyvemdmFleet $fl
$fleetId = $fleet->getID();
$task = new PluginFlyvemdmTask();
$rows = $task->find("`plugin_flyvemdm_fleets_id` = '$fleetId'
AND `itemtype` = '$itemtype' AND `items_id` = '$itemId'", '', '1');
AND `plugin_flyvemdm_policies_id` = '" . $this->policyData->getID() . "'
AND `items_id` = '$itemId'", '', '1');
if (count($rows) > 0) {
return false;
}
Expand Down Expand Up @@ -195,7 +196,7 @@ public function unapply(PluginFlyvemdmFleet $fleet, $value, $itemtype, $itemId)
if ($package->getFromDB($itemId)) {
$policyFactory = new PluginFlyvemdmPolicyFactory();
$removeApp = $policyFactory->createFromPolicy($policyData);
$removeApp->apply($fleet, '', $package, $itemId);
$removeApp->apply($fleet, $package->getField('package_name'), '', 0);
}

return true;
Expand Down
34 changes: 6 additions & 28 deletions inc/policyremoveapplication.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ public function unicityCheck($value, $itemtype, $itemId, PluginFlyvemdmFleet $fl
$fleetId = $fleet->getID();
$task = new PluginFlyvemdmTask();
$rows = $task->find("`plugin_flyvemdm_fleets_id` = '$fleetId'
AND `itemtype` = '' AND `items_id` = '0' AND `value` = '$value'", "", "1");
AND `plugin_flyvemdm_policies_id` = '" . $this->policyData->getID() . "'
AND `value` = '$value'", "", "1");
return (count($rows) == 0);
}

Expand All @@ -90,12 +91,12 @@ public function unicityCheck($value, $itemtype, $itemId, PluginFlyvemdmFleet $fl
* @return array|bool
*/
public function getMqttMessage($value, $itemtype, $itemId) {
if (! $this->integrityCheck($value, $itemtype, $itemId)) {
if (!$this->integrityCheck($value, $itemtype, $itemId)) {
return false;
}

$array = [
$this->symbol => $value
$this->symbol => $value,
];
return $array;
}
Expand All @@ -108,41 +109,18 @@ public function getMqttMessage($value, $itemtype, $itemId) {
* @return bool
*/
public function apply(PluginFlyvemdmFleet $fleet, $value, $itemtype, $itemId) {
// force the itemtype for packages
if (!($itemtype instanceof PluginFlyvemdmPackage)) {
$itemtype = new PluginFlyvemdmPackage();
}

// the itemId wasn't send and the itemtype is new
if ($itemtype->isNewItem() && !$itemId) {
Session::addMessageAfterRedirect(__('An application ID is required', 'flyvemdm'),
false, ERROR);
return false;
}

// the itemid is send but the itemtype is new, lest's try to load the info of the package
if ($itemtype->isNewItem() && $itemId) {
if (!$itemtype->getFromDB($itemId)) {
Session::addMessageAfterRedirect(__('The application does not exists', 'flyvemdm'),
false, ERROR);
return false;
}
}
$this->canApply = false;

// the package is loaded, let's create the task
$packageName = $itemtype->getField('package_name');
$packageName = ($packageName) ? $packageName : $itemtype->getField('name');
$value = json_encode(['package' => $packageName, 'id' => $itemtype->getID()]);
$task = new PluginFlyvemdmTask();
if (!$task->add([
'plugin_flyvemdm_fleets_id' => $fleet->getID(),
'plugin_flyvemdm_policies_id' => $this->getPolicyData()->getID(),
'value' => $value,
'_silent' => true,
])) {
return false;
}

$this->canApply = true;
return true;
}

Expand Down
7 changes: 0 additions & 7 deletions inc/task.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,13 +224,6 @@ public function prepareInputForAdd($input) {
return false;
}

if (!$this->policy->apply($this->fleet, $input['value'], $input['itemtype'],
$input['items_id'])) {
Session::addMessageAfterRedirect(__('Failed to apply the policy', 'flyvemdm'), false,
ERROR);
return false;
}

return $input;
}

Expand Down
122 changes: 0 additions & 122 deletions tests/suite-integration/PluginFlyvemdmPolicyRemoveApplication.php

This file was deleted.

Loading

0 comments on commit 566c7a4

Please sign in to comment.