diff --git a/inc/agent.class.php b/inc/agent.class.php index e0e0376b..be132fd1 100644 --- a/inc/agent.class.php +++ b/inc/agent.class.php @@ -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([ @@ -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 @@ -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); @@ -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(); @@ -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'); @@ -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); @@ -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); diff --git a/tests/suite-integration/PluginFlyvemdmAgent.php b/tests/suite-integration/PluginFlyvemdmAgent.php index 509cf5e2..85fea14e 100644 --- a/tests/suite-integration/PluginFlyvemdmAgent.php +++ b/tests/suite-integration/PluginFlyvemdmAgent.php @@ -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()) @@ -919,6 +926,7 @@ private function loginAndAddAgent(array $currentInvitation) { 'lastname' => 'Doe', 'version' => $this->minAndroidVersion, 'type' => 'android', + 'inventory' => $this->rawData, ]); return $agentId; } @@ -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( @@ -953,7 +962,8 @@ private function agentFromInvitation( $serial, $invitationToken, $mdmType = 'android', - $version = '' + $version = '', + $inventory = null ) { //Version change $finalVersion = $this->minAndroidVersion; @@ -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 = [ @@ -974,6 +986,7 @@ private function agentFromInvitation( 'firstname' => 'John', 'lastname' => 'Doe', 'type' => $mdmType, + 'inventory' => $finalInventory, ]; if ($serial) { @@ -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 " + + INVENTORY + FlyveMDM-Agent_v1.0 + " . $deviceId . " + + + " . date("Y-m-d H:i:s") . " + N/A + + + TAG + + + + 09/11/17 + jenkins + Aquaris M10 FHD + Android + 6.0 + aarch64 + " . $uuid . " + 1961 + + + 09/11/17 + bq + bq + Aquaris M10 FHD + FG022930 + + + Memory + 1961 + + + Touch Screen + Touch Screen + FINGER + + + ACCELEROMETER + MTK + ACCELEROMETER + 0.13 + 3 + + + LIGHT + MTK + Unknow + 0.13 + 1 + + + ORIENTATION + MTK + Unknow + 0.25 + 3 + + + MAGNETOMETER + MTK + MAGNETIC FIELD + 0.25 + 3 + + + /system + 1487 + 72 + + + /storage/emulated/0 + 12529 + 8322 + + + /data + 12529 + 8322 + + + /cache + 410 + 410 + + + AArch64 Processor rev 3 (aarch64) + 1500 + + + SIM_STATE_UNKNOWN + + + 1920x1128 + + + 3264x2448 + + + 2880x1728 + + + WIFI + " . $macAddress . " + 65 + aa:5b:78:78:52:7e + aa:5b:78:78:52:7e + 172.20.10.1 + 172.20.10.3 + 0.0.0.0 + 172.20.10.1 + + + SYSTEMSERVERCLASSPATH + /system/framework/services.jar:/system/framework/ethernet-service.jar:/system/framework/wifi-service.jar + + + ANDROID_SOCKET_zygote + 11 + + + ANDROID_DATA + /data + + + PATH + /sbin:/vendor/bin:/system/sbin:/system/bin:/system/xbin + + + ANDROID_ASSETS + /system/app + + + ANDROID_ROOT + /system + + + ASEC_MOUNTPOINT + /mnt/asec + + + LD_PRELOAD + libdirect-coredump.so + + + ANDROID_BOOTLOGO + 1 + + + BOOTCLASSPATH + /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 + + + ANDROID_PROPERTY_WORKSPACE + 9,0 + + + EXTERNAL_STORAGE + /sdcard + + + ANDROID_STORAGE + /storage + + + Dalvik + en_GB + The Android Project + 0.9 + /system + 2.1.0 + . + + + Li-ion + 23.0c + 3.745V + 60% + Good + Not charging + + + "; + } } \ No newline at end of file