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

Commit 248d715

Browse files
committed
feat(agent): enroll by entity token
Signed-off-by: Thierry Bugier <[email protected]>
1 parent 2f260f0 commit 248d715

File tree

1 file changed

+39
-84
lines changed

1 file changed

+39
-84
lines changed

inc/agent.class.php

Lines changed: 39 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1390,91 +1390,46 @@ protected function enrollByInvitationToken($input) {
13901390
}
13911391

13921392
/**
1393-
* @param string $serial
1394-
* @param array $authFactors
1395-
* @param string $csr Certificate Signing Request from the agent
1396-
* @param &string $notFoundMessage Contains the error message if the enrollment failed
1397-
* @return boolean|PluginFlyvemdmAgent
1398-
*
1393+
* Attemt to enroll with an entity invitation token
1394+
* @param array $input Enrollment data
1395+
* @return array|bool
13991396
*/
1400-
//protected static function enrollByEntityToken($serial, $authFactors, $csr, &$errorMessage) {
1401-
//global $DB;
1402-
1403-
//$token = $DB->escape($authFactors['entityToken']);
1404-
1405-
//// Find an entity matching the given token
1406-
//$entity = new PluginFlyvemdmEntityconfig();
1407-
//if (!$entity->getFromDBByCrit(['enroll_token' => $token])) {
1408-
// $errorMessage = "no entity token not found";
1409-
// return false;
1410-
//}
1411-
1412-
//// Create a new computer for the device being enrolled
1413-
//// TODO : Enable localization of the type
1414-
//$computerType = new ComputerType();
1415-
//$computerTypeId = $computerType->import(['name' => 'Smartphone']);
1416-
//if ($computerTypeId == -1 || $computerTypeId === false) {
1417-
// $computerTypeId = 0;
1418-
//}
1419-
//$computer = new Computer();
1420-
//$condition = "`serial`='" . $DB->escape($serial) . "' AND `entities_id`='" . $entity->getID() . "'";
1421-
//$computerCollection = $computer->find($condition);
1422-
//if (count($computerCollection) > 1) {
1423-
// $errorMessage = "failed to find the computer";
1424-
// return false;
1425-
//}
1426-
//if (count($computerCollection) == 1) {
1427-
1428-
// reset($computerCollection);
1429-
// $computer->getFromDB(key($computerCollection));
1430-
// $computerId = $computer->getID();
1431-
1432-
//} else {
1433-
// $computerId = $computer->add([
1434-
// 'entities_id' => $entity->getID(),
1435-
// 'serial' => $serial,
1436-
// 'computertypes_id' => $computerTypeId
1437-
// ]);
1438-
1439-
// if ($computerId === false) {
1440-
// $errorMessage = "failed to create the computer";
1441-
// return false;
1442-
// }
1443-
//}
1444-
1445-
//if (! $computerId > 0) {
1446-
// $errorMessage = "failed to update the computer";
1447-
// return false;
1448-
//}
1449-
1450-
//// Create an agent for this device, linked to the new computer
1451-
//$agent = new PluginFlyvemdmAgent();
1452-
//$condition = "`computers_id`='$computerId'";
1453-
//$agentCollection = $agent->find($condition);
1454-
//if (count($agentCollection) > 1) {
1455-
// return false;
1456-
//}
1457-
//if (count($agentCollection) == 1) {
1458-
1459-
// reset($agentCollection);
1460-
// $agent->getFromDB(key($agentCollection));
1461-
// $agentId = $agent->getId();
1462-
1463-
//} else {
1464-
// $agentId = $agent->add([
1465-
// 'entities_id' => $entity->getID(),
1466-
// 'computers_id' => $computer->getID(),
1467-
// 'token_expire' => '0000-00-00 00:00:00'
1468-
// ]);
1469-
//}
1470-
1471-
//if (! $agentId > 0) {
1472-
// return false;
1473-
//}
1474-
1475-
//return $agent;
1476-
1477-
//}
1397+
protected function enrollByEntityToken($input) {
1398+
$invitationToken = isset($input['_invitation_token']) ? $input['_invitation_token'] : null;
1399+
$serial = isset($input['_serial']) ? $input['_serial'] : null;
1400+
$uuid = isset($input['_uuid']) ? $input['_uuid'] : null;
1401+
$csr = isset($input['csr']) ? $input['csr'] : null;
1402+
$version = isset($input['version']) ? $input['version'] : null;
1403+
$mdmType = isset($input['type']) ? $input['type'] : null;
1404+
$inventory = isset($input['inventory']) ? htmlspecialchars_decode($input['inventory'], ENT_XML1) : null;
1405+
$systemPermission = isset($input['has_system_permission']) ? $input['has_system_permission'] : 0;
1406+
// For non-android agents, system permssion might be forced to 1 depending on the lack of such cosntraint
1407+
1408+
$input = [];
1409+
1410+
// Find the invitation
1411+
$entityConfig = new PluginFlyvemdmEntityconfig();
1412+
if (!$entityConfig->getFromDBByToken($invitationToken)) {
1413+
$this->filterMessages(__('Invitation token invalid', 'flyvemdm'));
1414+
return false;
1415+
}
1416+
1417+
// To be written
1418+
// ...
1419+
1420+
$input['name'] = $email;
1421+
$input['computers_id'] = $computerId;
1422+
$input['entities_id'] = $entityId;
1423+
$input['plugin_flyvemdm_fleets_id'] = $defaultFleet->getID();
1424+
$input['_invitations_id'] = $invitation->getID();
1425+
$input['enroll_status'] = 'enrolled';
1426+
$input['version'] = $version;
1427+
$input['users_id'] = $agentAccount->getID();
1428+
$input['mdm_type'] = $mdmType;
1429+
$input['$systemPermission'] = $systemPermission;
1430+
1431+
return $input;
1432+
}
14781433

14791434
/**
14801435
* Erase delete persisted MQTT topics of the agent

0 commit comments

Comments
 (0)