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

Commit

Permalink
fix(inventory): invited devices are always created in root entity
Browse files Browse the repository at this point in the history
Signed-off-by: Thierry Bugier <tbugier@teclib.com>
  • Loading branch information
btry authored and ajsb85 committed May 4, 2018
1 parent ae212e5 commit df2ba3f
Show file tree
Hide file tree
Showing 7 changed files with 407 additions and 10 deletions.
4 changes: 4 additions & 0 deletions front/invitation.form.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@
$_POST['_notify'] = '';
$invitation->update($_POST);
Html::back();
} else if (isset($_POST['purge'])) {
$invitation->check($_POST['id'], PURGE);
$invitation->delete($_POST, 1);
$invitation->redirectToList();
} else {
$invitation->check($_GET['id'], READ);
Html::header(
Expand Down
9 changes: 9 additions & 0 deletions hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,12 @@ function plugin_flyvemdm_hook_computer_purge(CommonDBTM $item) {
$agent = new PluginFlyvemdmAgent();
$agent->hook_computer_purge($item);
}

function plugin_flyvemdm_hook_pre_invitation_purge(CommonDBTM $item) {
$success = true;

$invitation = new PluginFlyvemdmInvitation();
$success = $success && $invitation->hook_pre_invitation_purge($item);

return $success;
}
6 changes: 6 additions & 0 deletions inc/exception/fusioninventoryruleinconsistency.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

namespace GlpiPlugin\Flyvemdm\Exception;


class FusionInventoryRuleInconsistency extends \Exception {}
198 changes: 198 additions & 0 deletions inc/fusioninventory.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
<?php
/**
* LICENSE
*
* Copyright © 2016-2018 Teclib'
* Copyright © 2010-2018 by the FusionInventory Development Team.
*
* This file is part of Flyve MDM Plugin for GLPI.
*
* Flyve MDM Plugin for GLPI is a subproject of Flyve MDM. Flyve MDM is a mobile
* device management software.
*
* Flyve MDM Plugin for GLPI is free software: you can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* Flyve MDM Plugin for GLPI is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
* You should have received a copy of the GNU Affero General Public License
* along with Flyve MDM Plugin for GLPI. If not, see http://www.gnu.org/licenses/.
* ------------------------------------------------------------------------------
* @author Thierry Bugier
* @copyright Copyright © 2018 Teclib
* @license AGPLv3+ http://www.gnu.org/licenses/agpl.txt
* @link https://github.com/flyve-mdm/glpi-plugin
* @link https://flyve-mdm.com/
* ------------------------------------------------------------------------------
*/

use GlpiPlugin\Flyvemdm\Exception\FusionInventoryRuleInconsistency;

if (!defined('GLPI_ROOT')) {
die("Sorry. You can't access this file directly");
}

class PluginFlyvemdmFusionInventory {

const RULE_NAME = 'Flyve MDM invitation to entity';

/**
* Creates or updates an entity rule
*
* @param PluginFlyvemdmInvitation $invitation invitation to handle
*/
public function addInvitationRule(PluginFlyvemdmInvitation $invitation) {
$entityId = $invitation->getField(Entity::getForeignKeyField());
$rule = $this->getRule($entityId);

$ruleCriteria = new RuleCriteria();
$ruleCriteria->add([
Rule::getForeignKeyField() => $rule->getID(),
'criteria' => 'tag',
'condition' => '0',
'pattern' => $this->getRuleCriteriaValue($invitation),
]);

// Activate the rule
$rule->update([
'id' => $rule->getID(),
'is_active' => '1',
]);
}

/**
* Deletes a rule criteria for an entity assignment rule
*
* @param PluginFlyvemdmInvitation $invitation invitation to handle
*/
public function deleteInvitationRuleCriteria(PluginFlyvemdmInvitation $invitation) {
global $DB;

$entityId = $invitation->getField(Entity::getForeignKeyField());
$ruleFk = PluginFusioninventoryInventoryRuleEntity::getForeignKeyField();
$request = [
'SELECT' => RuleCriteria::getTable() . '.*',
'FROM' => RuleCriteria::getTable(),
'INNER JOIN' => [
PluginFusioninventoryInventoryRuleEntity::getTable() => [
'FKEY' => [
PluginFusioninventoryInventoryRuleEntity::getTable() => 'id',
RuleCriteria::getTable() => $ruleFk,
]
],
RuleAction::getTable() => [
'FKEY' => [
PluginFusioninventoryInventoryRuleEntity::getTable() => 'id',
RuleAction::getTable() => $ruleFk,
]
]
],
'WHERE' => [
'AND' => [
'pattern' => $this->getRuleCriteriaValue($invitation),
'criteria' => 'tag',
]
]
];
$result = $DB->request($request);
if ($result->count() !== 1) {
return;
}

$row = $result->next();
$ruleCriteria = new RuleCriteria();
$ruleCriteria->delete([
'id' => $row['id']
]);

$ruleId = $row[$ruleFk];
$rows = $ruleCriteria->find("`$ruleFk` = '$ruleId' AND `criteria` = 'tag' AND `condition` = '0'");
if (count($rows) === 0) {
$rule = new PluginFusioninventoryInventoryRuleEntity();
$rule->update([
'id' => $row[$ruleFk],
'is_active' => '0',
]);
}
}

/**
* gets a entity identification tag derivated from an invitation
*/
private function getRuleCriteriaValue(PluginFlyvemdmInvitation $invitation) {
return 'invitation_' . $invitation->getField('invitation_token');
}

/**
* Finds a rule
*
* @param integer $entityId ID of the entity assigned by the rule
* @param boolean $create If the rule does not exists, create it
*
* @return PluginFusioninventoryInventoryRuleEntity|null
*/
private function getRule($entityId, $create = true) {
global $DB;

$request = [
'SELECT' => PluginFusioninventoryInventoryRuleEntity::getTable() . '.*',
'FROM' => PluginFusioninventoryInventoryRuleEntity::getTable(),
'INNER JOIN' => [
RuleAction::getTable() => [
'FKEY' => [
PluginFusioninventoryInventoryRuleEntity::getTable() => 'id',
RuleAction::getTable() => PluginFusioninventoryInventoryRuleEntity::getForeignKeyField()
],
]
],
'WHERE' => [
'AND' => [
PluginFusioninventoryInventoryRuleEntity::getTable() . '.name'
=> self::RULE_NAME . " $entityId",
'sub_type' => PluginFusioninventoryInventoryRuleEntity::class,
'action_type' => 'assign',
'field' => Entity::getForeignKeyField(),
'value' => $entityId,
]
]
];

$result = $DB->request($request);
if ($result->count() === 1) {
$rule = new PluginFusioninventoryInventoryRuleEntity();
$row = $result->next();
$rule->getFromDB($row['id']);
return $rule;
}
if ($result->count() > 1) {
throw new FusionInventoryRuleInconsistency('Import to entity rule is not unique');
}

if (!$create) {
return null;
}

$rule = new PluginFusioninventoryInventoryRuleEntity();
$rule->add([
'sub_type' => PluginFusioninventoryInventoryRuleEntity::class,
'entities_id' => '0',
'is_recursive' => '0',
'name' => self::RULE_NAME . " $entityId",
'description' => 'Automatically generated by an invitation to enrol. Do not change its name',
'is_active' => '0',
'condition' => '0',
'match' => Rule::OR_MATCHING,
]);
$ruleAction = new RuleAction();
$ruleAction->add([
Rule::getForeignKeyField() => $rule->getID(),
'action_type' => 'assign',
'field' => Entity::getForeignKeyField(),
'value' => $entityId,
]);
return $rule;
}
}
19 changes: 10 additions & 9 deletions inc/invitation.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,20 +229,20 @@ protected function setInvitationToken() {
return $token;
}

/**
*
* @see CommonDBTM::pre_deleteItem()
*/
public function pre_deleteItem() {
$invitationLog = new PluginFlyvemdmInvitationlog();
return $invitationLog->deleteByCriteria(['plugin_flyvemdm_invitations_id' => $this->getID()]);
}

/**
* @see CommonDBTM::post_addItem()
*/
public function post_addItem() {
// Create / update import to entity rule
$fi = new PluginFlyvemdmFusionInventory();
$fi->addInvitationRule($this);

// Generate QR code
$this->createQRCodeDocument();

// Sent invitation email
$this->sendInvitation();
}

Expand All @@ -251,9 +251,10 @@ public function post_addItem() {
* @param CommonDBTM $item
* @return bool
*/
public static function hook_pre_self_purge(CommonDBTM $item) {
public function hook_pre_invitation_purge(CommonDBTM $item) {
$fi = new PluginFlyvemdmFusionInventory();
$fi->deleteInvitationRuleCriteria($item);
$document = new Document();
$document->getFromDB($item->getField('documents_id'));
return $document->delete([
'id' => $item->getField('documents_id'),
], 1);
Expand Down
2 changes: 1 addition & 1 deletion setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ function plugin_flyvemdm_addHooks() {
Computer::class => 'plugin_flyvemdm_hook_computer_purge',
];
$PLUGIN_HOOKS['pre_item_purge']['flyvemdm'] = [
PluginFlyvemdmInvitation::class => [PluginFlyvemdmInvitation::class, 'hook_pre_self_purge'],
PluginFlyvemdmInvitation::class => 'plugin_flyvemdm_hook_pre_invitation_purge',
Document::class => [PluginFlyvemdmInvitation::class, 'hook_pre_document_purge'],
Profile_User::class => 'plugin_flyvemdm_hook_pre_profileuser_purge',
];
Expand Down
Loading

0 comments on commit df2ba3f

Please sign in to comment.