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

Commit

Permalink
feat(message): remove queing of mqtt messages and grouping
Browse files Browse the repository at this point in the history
  • Loading branch information
btry authored and ajsb85 committed Jan 26, 2018
1 parent d966a17 commit a167169
Show file tree
Hide file tree
Showing 16 changed files with 132 additions and 432 deletions.
93 changes: 37 additions & 56 deletions inc/agent.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,7 @@ class PluginFlyvemdmAgent extends CommonDBTM implements PluginFlyvemdmNotifiable
/**
*
* Returns the minimum version of the agent accepted by the backend
*
* @param string $mdmType the type of the agent.
*
* @return string the minimum version of the agent depending on its type
*/
private function getMinVersioForType($mdmType) {
Expand Down Expand Up @@ -201,8 +199,8 @@ static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $withtem
}

/**
* Show form for edition
* @param integer $ID
* Shows form for edition
* @param integer $ID Id of the agent
* @param array $options
*/
public function showForm($ID, array $options = []) {
Expand Down Expand Up @@ -248,7 +246,7 @@ public function showForm($ID, array $options = []) {
}

/**
* Print the computer's operating system form
* Prints the computer's operating system form
*
* @param PluginFlyvemdmAgent $item
*
Expand Down Expand Up @@ -292,7 +290,7 @@ public static function showDangerZone(PluginFlyvemdmAgent $item) {
}

/**
* Display the agents according the fleet
* Displays the agents according the fleet
* @param PluginFlyvemdmFleet $item
* @return string an html with the agents
*/
Expand Down Expand Up @@ -354,9 +352,6 @@ public static function displayTabContentForComputer(CommonDBTM $item) {
]);
}

/**
* @see CommonDBTM::canViewItem()
*/
public function canViewItem() {
// Check the active profile
$config = Config::getConfigurationValues('flyvemdm', ['guest_profiles_id']);
Expand All @@ -379,7 +374,7 @@ public function canViewItem() {
}

/**
* Send a wipe command to the agent
* Sends a wipe command to the agent
*/
protected function sendWipeQuery() {
$topic = $this->getTopic();
Expand All @@ -390,7 +385,7 @@ protected function sendWipeQuery() {
}

/**
* Send a lock command to the agent
* Sends a lock command to the agent
*/
protected function sendLockQuery() {
$topic = $this->getTopic();
Expand All @@ -401,7 +396,7 @@ protected function sendLockQuery() {
}

/**
* Send a lock command to the agent
* Sends a lock command to the agent
*/
protected function sendUnlockQuery() {
$topic = $this->getTopic();
Expand All @@ -412,7 +407,7 @@ protected function sendUnlockQuery() {
}

/**
* Send unenrollment command to the agent
* Sends unenrollment command to the agent
*/
protected function sendUnenrollQuery() {
$topic = $this->getTopic();
Expand All @@ -422,10 +417,6 @@ protected function sendUnenrollQuery() {
}
}

/**
* @param array $input
* @return array|bool
*/
public function prepareInputForAdd($input) {
// Get the maximum quantity of devices allowed for the current entity
$entityConfig = new PluginFlyvemdmEntityconfig();
Expand Down Expand Up @@ -466,10 +457,6 @@ public function prepareInputForAdd($input) {
return $input;
}

/**
* @param array $input
* @return array|bool
*/
public function prepareInputForUpdate($input) {
if (isset($input['plugin_flyvemdm_fleets_id'])) {
// Update MQTT ACL for the fleet
Expand Down Expand Up @@ -552,17 +539,11 @@ public function prepareInputForUpdate($input) {
return $input;
}

/**
* @see CommonDBTM::post_addItem()
*/
public function post_addItem() {
// Notify the agent about its fleets
$this->updateSubscription();
}

/**
* @see CommonDBTM::post_getFromDB()
*/
public function post_getFromDB() {
// set Topic after getting an item
// Useful for post_purgeItem
Expand Down Expand Up @@ -662,17 +643,19 @@ public function pre_deleteItem() {
public function post_updateItem($history = 1) {
if (in_array('plugin_flyvemdm_fleets_id', $this->updates)) {
// create tasks for the agent from already applied policies
$fleetId = $this->fields['plugin_flyvemdm_fleets_id'];
$newFleet = new PluginFlyvemdmFleet();
if ($newFleet->getFromDB($this->fields['plugin_flyvemdm_fleets_id'])) {
if ($newFleet->getFromDB($fleetId)) {
// Create task status for the agent and the applied policies
$task = new PluginFlyvemdmTask();

// get groups of policies where a policy applies
$groups = $task->getGroupsOfAppliedPolicies($newFleet);
foreach ($groups as $groupName) {
// get policies per group
$policiesToApply = $task->getGroupOfPolicies($groupName, $newFleet);
// create task statuses for a single agent
$task->createTaskStatus($this, $policiesToApply);
$tasks = $task->find("`plugin_flyvemdm_fleets_id` = '$fleetId'");
foreach($tasks as $row) {
$taskStatus = new PluginFlyvemdmTaskstatus();
$taskStatus->add([
'plugin_flyvemdm_agents_id' => $this->getID(),
'plugin_flyvemdm_tasks_id' => $row['id'],
'status' => 'pending',
]);
}
}

Expand Down Expand Up @@ -726,9 +709,6 @@ public function post_purgeItem() {
$this->cleanupSubtopics();
}

/**
* @return array
*/
public function getSearchOptionsNew() {
$tab = parent::getSearchOptionsNew();

Expand Down Expand Up @@ -848,7 +828,7 @@ public function getSearchOptionsNew() {
}

/**
* Limit search for agents of guest user
* Limits search for agents of guest user
*/
public static function addDefaultJoin() {
$join = '';
Expand Down Expand Up @@ -1132,7 +1112,7 @@ protected function enrollByInvitationToken($input) {
$user = new User();
$condition = "`glpi_users`.`id`='" . $invitation->getField('users_id') . "'";
if ($user->getFromDBbyEmail($email, $condition) === false) {
$event = __('wrong email address', 'flyvemdm');
$event = __('Wrong email address', 'flyvemdm');
$this->filterMessages($event);
$this->logInvitationEvent($invitation, $event);
return false;
Expand Down Expand Up @@ -1404,21 +1384,22 @@ public function cleanupSubtopics() {
* @return string[]
*/
public static function getTopicsToCleanup() {
return [
"Command/Subscribe",
"Command/Ping",
"Command/Geolocate",
"Command/Inventory",
"Command/Lock",
"Command/Wipe",
"Command/Unenroll",
"Configuration",
"application",
"file",
"policies",
"encryption",
"camera",
"connectivity",
$policy = new PluginFlyvemdmPolicy();
$rows = $policy->find();

// get all policies sub topics
$topics = [];
foreach ($rows as $row) {
$topics[] = 'Policy/' . $row['symbol'];
}
return $topics + [
'Command/Subscribe',
'Command/Ping',
'Command/Geolocate',
'Command/Inventory',
'Command/Lock',
'Command/Wipe',
'Command/Unenroll',
];
}

Expand Down
4 changes: 1 addition & 3 deletions inc/file.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -348,17 +348,15 @@ public function post_updateItem($history = 1) {
$task = new PluginFlyvemdmTask();
$taskCol = $task->find("`itemtype`='$itemtype' AND `items_id`='$itemId'");
$fleet = new PluginFlyvemdmFleet();
$policyFactory = new PluginFlyvemdmPolicyFactory();
foreach ($taskCol as $taskId => $taskRow) {
$fleetId = $taskRow['plugin_flyvemdm_fleets_id'];
if ($fleet->getFromDB($fleetId)) {
Toolbox::logInFile('php-errors',
"Plugin Flyvemdm : Could not find fleet id = '$fleetId'");
continue;
}
$policy = $policyFactory->createFromDBByID($taskRow['plugin_flyvemdm_policies_id']);
if ($task->getFromDB($taskId)) {
$task->updateQueue($fleet, $policy->getGroup());
$task->publishPolicy($fleet);
}
}
}
Expand Down
16 changes: 7 additions & 9 deletions inc/fleet.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,13 +214,6 @@ public function pre_deleteItem() {
return false;
}

$mqttQueue = new PluginFlyvemdmMqttupdatequeue();
if (!$mqttQueue->deleteByCriteria(['plugin_flyvemdm_fleets_id' => $fleetId])) {
Session::addMessageAfterRedirect(__('Could not delete message queue on the fleet', 'flyvemdm'));
// Do not fail yet. We need a CRON purge feature on this itemtype
//return false;
}

return true;
}

Expand Down Expand Up @@ -310,8 +303,12 @@ public function getTopic() {
*/
public function post_addItem() {
// Generate default policies for groups of policies
$task = new PluginFlyvemdmTask();
$task->publishPolicies($this, ['camera', 'connectivity', 'encryption', 'policies']);
$policy = new PluginFlyvemdmPolicy();
foreach ($policy->find() as $row) {
$policyName = $row['symbol'];
$topic = $this->getTopic();
$this->notify("$topic/Policy/$policyName", null, 0, 1);
}
}

/**
Expand Down Expand Up @@ -499,6 +496,7 @@ public function notify($topic, $mqttMessage, $qos = 0, $retain = 0) {
$mqttClient = PluginFlyvemdmMqttclient::getInstance();
$mqttClient->publish($topic, $mqttMessage, $qos, $retain);
}

/**
* create folders and initial setup of the entity related to MDM
* @param CommonDBTM $item
Expand Down
Loading

0 comments on commit a167169

Please sign in to comment.