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

Commit

Permalink
feat(agent): updated code to import computer inventory with F.I.
Browse files Browse the repository at this point in the history
  • Loading branch information
DIOHz0r committed Jan 3, 2018
1 parent 93c2bf9 commit 098b992
Show file tree
Hide file tree
Showing 2 changed files with 240 additions and 28 deletions.
55 changes: 28 additions & 27 deletions inc/agent.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -640,12 +640,6 @@ public function pre_deleteItem() {
}
}

// Delete the computer associated to the agent
/*if (!$computer->delete(['id' => $computerId], true)) {
Session::addMessageAfterRedirect(__('Failed to delete the device', 'flyvemdm'));
return false;
}*/

// Delete documents associated to the agent
$document_Item = new Document_Item();
$success = $document_Item->deleteByCriteria([
Expand Down Expand Up @@ -1038,6 +1032,7 @@ protected function enrollByInvitationToken($input) {
$lastname = isset($input['lastname']) ? $input['lastname'] : null;
$version = isset($input['version']) ? $input['version'] : null;
$mdmType = isset($input['type']) ? $input['type'] : null;
$inventory = isset($input['inventory']) ? $input['inventory'] : null;
$systemPermission = isset($input['has_system_permission']) ? $input['has_system_permission'] : 0;
// For non-android agents, system permssion might be forced to 1 depending on the lack of such cosntraint

Expand Down Expand Up @@ -1066,6 +1061,13 @@ protected function enrollByInvitationToken($input) {
return false;
}

if (empty($inventory)) {
$event = __('Device inventory XML is mandatory', 'flyvemdm');
$this->filterMessages($event);
$this->logInvitationEvent($invitation, $event);
return false;
}

if (empty($version)) {
$event = __('Agent version missing', 'flyvemdm');
$this->filterMessages($event);
Expand Down Expand Up @@ -1149,16 +1151,7 @@ protected function enrollByInvitationToken($input) {
}
$userId = $user->getId();

// Check the serial does not exists in the entity
$computer = new Computer();
$entityId = $invitation->getField('entities_id');
$rows = $computer->find("`entities_id`='$entityId' AND `serial`='$serial'");
if (count($rows) > 0) {
$event = __('The serial already exists', 'flyvemdm');
$this->filterMessages($event);
$this->logInvitationEvent($invitation, $event);
return false;
}

//create agent user account
$agentAccount = new User();
Expand All @@ -1178,6 +1171,7 @@ protected function enrollByInvitationToken($input) {
$this->logInvitationEvent($invitation, $event);
return false;
}

$agentToken = User::getToken($agentAccount->getID(), 'api_token');
if ($agentToken === false) {
$event = __('Cannot create the API token for the agent', 'flyvemdm');
Expand Down Expand Up @@ -1230,17 +1224,25 @@ protected function enrollByInvitationToken($input) {
}

// Create the device
$computer = new Computer();
$computerId = $computer->add([
'name' => $email,
'users_id' => $userId,
'entities_id' => $entityId,
'serial' => $serial,
'uuid' => $uuid,
'computertypes_id' => $computerTypeId,
'is_dynamic' => 1,
]);
if ($computerId === false) {
$pfCommunication = new PluginFusioninventoryCommunication();
$pfAgent = new PluginFusioninventoryAgent();
//var_dump($_SESSION['glpi_plugin_fusioninventory']);
$_SESSION['glpi_fusionionventory_nolock'] = true;
$pfCommunication->handleOCSCommunication('',$inventory,'glpi');
unset($_SESSION['glpi_fusionionventory_nolock']);
$fiAgentId = $_SESSION['plugin_fusioninventory_agents_id']; // generated by FusionInventory

if ($fiAgentId === 0) {
$event = __("Cannot create the FusionInventory agent", 'flyvemdm');
$this->filterMessages($event);
$this->logInvitationEvent($invitation, $event);
return false;
}

$pfAgent->getFromDB($fiAgentId);
$computerId = $pfAgent->getField(Computer::getForeignKeyField());

if ($computerId === 0) {
$event = __("Cannot create the device", 'flyvemdm');
$this->filterMessages($event);
$this->logInvitationEvent($invitation, $event);
Expand All @@ -1254,7 +1256,6 @@ protected function enrollByInvitationToken($input) {
// Create the agent
$defaultFleet = PluginFlyvemdmFleet::getDefaultFleet();
if ($defaultFleet === null) {
$computer->delete(['id' => $computerId]);
$event = __("No default fleet available for the device", 'flyvemdm');
$this->filterMessages($event);
$this->logInvitationEvent($invitation, $event);
Expand Down
213 changes: 212 additions & 1 deletion tests/suite-integration/PluginFlyvemdmAgent.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,13 @@ public function testEnrollAgent() {
$rows = $invitationLog->find("`plugin_flyvemdm_invitations_id` = '$inviationId'");
$this->integer(count($rows))->isEqualTo($expectedLogCount);

// Test enrollment without inventory
$agent = $this->agentFromInvitation($user, $guestEmail, $serial, $invitationToken, 'android',
'6.0', '');
$this->boolean($agent->isNewItem())
->isTrue(json_encode($_SESSION['MESSAGE_AFTER_REDIRECT'], JSON_PRETTY_PRINT));
$expectedLogCount++;

// Test successful enrollment
$agent = $this->agentFromInvitation($user, $guestEmail, $serial, $invitationToken, 'apple');
$this->boolean($agent->isNewItem())
Expand Down Expand Up @@ -919,6 +926,7 @@ private function loginAndAddAgent(array $currentInvitation) {
'lastname' => 'Doe',
'version' => $this->minAndroidVersion,
'type' => 'android',
'inventory' => $this->rawData,
]);
return $agentId;
}
Expand All @@ -945,6 +953,7 @@ private function createUserInvitation($userIdField) {
* @param string $invitationToken
* @param string $mdmType
* @param string|null $version if null the value is not used
* @param string $inventory xml
* @return object
*/
private function agentFromInvitation(
Expand All @@ -953,7 +962,8 @@ private function agentFromInvitation(
$serial,
$invitationToken,
$mdmType = 'android',
$version = ''
$version = '',
$inventory = null
) {
//Version change
$finalVersion = $this->minAndroidVersion;
Expand All @@ -964,6 +974,8 @@ private function agentFromInvitation(
$finalVersion = null;
}

$finalInventory = (null !== $inventory)? $inventory : $this->xmlInventory();

//$finalVersion = (null === $version) ? null : ((!empty($version)) ? $version : $this->minAndroidVersion);
//$invitationToken = ($badToken) ? 'bad token' : $invitation->getField('invitation_token');
$input = [
Expand All @@ -974,6 +986,7 @@ private function agentFromInvitation(
'firstname' => 'John',
'lastname' => 'Doe',
'type' => $mdmType,
'inventory' => $finalInventory,
];

if ($serial) {
Expand All @@ -985,4 +998,202 @@ private function agentFromInvitation(

return $this->enrollFromInvitation($user, $input);
}

/**
* @param string $deviceId
* @param string $uuid
* @param string $macAddress
* @return string
*/
public function xmlInventory($deviceId = '', $uuid='', $macAddress='') {
$uuid = ($uuid)? $uuid : '1d24931052f35d92';
$macAddress = ($macAddress)? $macAddress : '02:00:00:00:00:00';
$deviceId = ($deviceId) ? $deviceId : $uuid . "_" . $macAddress;
return "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>
<REQUEST>
<QUERY>INVENTORY</QUERY>
<VERSIONCLIENT>FlyveMDM-Agent_v1.0</VERSIONCLIENT>
<DEVICEID>" . $deviceId . "</DEVICEID>
<CONTENT>
<ACCESSLOG>
<LOGDATE>" . date("Y-m-d H:i:s") . "</LOGDATE>
<USERID>N/A</USERID>
</ACCESSLOG>
<ACCOUNTINFO>
<KEYNAME>TAG</KEYNAME>
<KEYVALUE/>
</ACCOUNTINFO>
<HARDWARE>
<DATELASTLOGGEDUSER>09/11/17</DATELASTLOGGEDUSER>
<LASTLOGGEDUSER>jenkins</LASTLOGGEDUSER>
<NAME>Aquaris M10 FHD</NAME>
<OSNAME>Android</OSNAME>
<OSVERSION>6.0</OSVERSION>
<ARCHNAME>aarch64</ARCHNAME>
<UUID>" . $uuid . "</UUID>
<MEMORY>1961</MEMORY>
</HARDWARE>
<BIOS>
<BDATE>09/11/17</BDATE>
<BMANUFACTURER>bq</BMANUFACTURER>
<MMANUFACTURER>bq</MMANUFACTURER>
<SMODEL>Aquaris M10 FHD</SMODEL>
<SSN>FG022930</SSN>
</BIOS>
<MEMORIES>
<DESCRIPTION>Memory</DESCRIPTION>
<CAPACITY>1961</CAPACITY>
</MEMORIES>
<INPUTS>
<CAPTION>Touch Screen</CAPTION>
<DESCRIPTION>Touch Screen</DESCRIPTION>
<TYPE>FINGER</TYPE>
</INPUTS>
<SENSORS>
<NAME>ACCELEROMETER</NAME>
<NAME>MTK</NAME>
<TYPE>ACCELEROMETER</TYPE>
<POWER>0.13</POWER>
<VERSION>3</VERSION>
</SENSORS>
<SENSORS>
<NAME>LIGHT</NAME>
<NAME>MTK</NAME>
<TYPE>Unknow</TYPE>
<POWER>0.13</POWER>
<VERSION>1</VERSION>
</SENSORS>
<SENSORS>
<NAME>ORIENTATION</NAME>
<NAME>MTK</NAME>
<TYPE>Unknow</TYPE>
<POWER>0.25</POWER>
<VERSION>3</VERSION>
</SENSORS>
<SENSORS>
<NAME>MAGNETOMETER</NAME>
<NAME>MTK</NAME>
<TYPE>MAGNETIC FIELD</TYPE>
<POWER>0.25</POWER>
<VERSION>3</VERSION>
</SENSORS>
<DRIVES>
<VOLUMN>/system</VOLUMN>
<TOTAL>1487</TOTAL>
<FREE>72</FREE>
</DRIVES>
<DRIVES>
<VOLUMN>/storage/emulated/0</VOLUMN>
<TOTAL>12529</TOTAL>
<FREE>8322</FREE>
</DRIVES>
<DRIVES>
<VOLUMN>/data</VOLUMN>
<TOTAL>12529</TOTAL>
<FREE>8322</FREE>
</DRIVES>
<DRIVES>
<VOLUMN>/cache</VOLUMN>
<TOTAL>410</TOTAL>
<FREE>410</FREE>
</DRIVES>
<CPUS>
<NAME>AArch64 Processor rev 3 (aarch64)</NAME>
<SPEED>1500</SPEED>
</CPUS>
<SIMCARDS>
<STATE>SIM_STATE_UNKNOWN</STATE>
</SIMCARDS>
<VIDEOS>
<RESOLUTION>1920x1128</RESOLUTION>
</VIDEOS>
<CAMERAS>
<RESOLUTIONS>3264x2448</RESOLUTIONS>
</CAMERAS>
<CAMERAS>
<RESOLUTIONS>2880x1728</RESOLUTIONS>
</CAMERAS>
<NETWORKS>
<TYPE>WIFI</TYPE>
<MACADDR>" . $macAddress . "</MACADDR>
<SPEED>65</SPEED>
<BSSID>aa:5b:78:78:52:7e</BSSID>
<SSID>aa:5b:78:78:52:7e</SSID>
<IPGATEWAY>172.20.10.1</IPGATEWAY>
<IPADDRESS>172.20.10.3</IPADDRESS>
<IPMASK>0.0.0.0</IPMASK>
<IPDHCP>172.20.10.1</IPDHCP>
</NETWORKS>
<ENVS>
<KEY>SYSTEMSERVERCLASSPATH</KEY>
<VAL>/system/framework/services.jar:/system/framework/ethernet-service.jar:/system/framework/wifi-service.jar</VAL>
</ENVS>
<ENVS>
<KEY>ANDROID_SOCKET_zygote</KEY>
<VAL>11</VAL>
</ENVS>
<ENVS>
<KEY>ANDROID_DATA</KEY>
<VAL>/data</VAL>
</ENVS>
<ENVS>
<KEY>PATH</KEY>
<VAL>/sbin:/vendor/bin:/system/sbin:/system/bin:/system/xbin</VAL>
</ENVS>
<ENVS>
<KEY>ANDROID_ASSETS</KEY>
<VAL>/system/app</VAL>
</ENVS>
<ENVS>
<KEY>ANDROID_ROOT</KEY>
<VAL>/system</VAL>
</ENVS>
<ENVS>
<KEY>ASEC_MOUNTPOINT</KEY>
<VAL>/mnt/asec</VAL>
</ENVS>
<ENVS>
<KEY>LD_PRELOAD</KEY>
<VAL>libdirect-coredump.so</VAL>
</ENVS>
<ENVS>
<KEY>ANDROID_BOOTLOGO</KEY>
<VAL>1</VAL>
</ENVS>
<ENVS>
<KEY>BOOTCLASSPATH</KEY>
<VAL>/system/framework/core-libart.jar:/system/framework/conscrypt.jar:/system/framework/okhttp.jar:/system/framework/core-junit.jar:/system/framework/bouncycastle.jar:/system/framework/ext.jar:/system/framework/framework.jar:/system/framework/telephony-common.jar:/system/framework/voip-common.jar:/system/framework/ims-common.jar:/system/framework/apache-xml.jar:/system/framework/org.apache.http.legacy.boot.jar:/system/framework/mediatek-common.jar:/system/framework/mediatek-framework.jar:/system/framework/mediatek-telephony-common.jar:/system/framework/dolby_ds2.jar:/system/framework/dolby_ds1.jar</VAL>
</ENVS>
<ENVS>
<KEY>ANDROID_PROPERTY_WORKSPACE</KEY>
<VAL>9,0</VAL>
</ENVS>
<ENVS>
<KEY>EXTERNAL_STORAGE</KEY>
<VAL>/sdcard</VAL>
</ENVS>
<ENVS>
<KEY>ANDROID_STORAGE</KEY>
<VAL>/storage</VAL>
</ENVS>
<JVMS>
<NAME>Dalvik</NAME>
<LANGUAGE>en_GB</LANGUAGE>
<VENDOR>The Android Project</VENDOR>
<RUNTIME>0.9</RUNTIME>
<HOME>/system</HOME>
<VERSION>2.1.0</VERSION>
<CLASSPATH>.</CLASSPATH>
</JVMS>
<BATTERIES>
<CHEMISTRY>Li-ion</CHEMISTRY>
<TEMPERATURE>23.0c</TEMPERATURE>
<VOLTAGE>3.745V</VOLTAGE>
<LEVEL>60%</LEVEL>
<HEALTH>Good</HEALTH>
<STATUS>Not charging</STATUS>
</BATTERIES>
</CONTENT>
</REQUEST>";
}
}

0 comments on commit 098b992

Please sign in to comment.