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

Commit 037f589

Browse files
committed
feat(agent): give ability to update an existing agent
Signed-off-by: Thierry Bugier <[email protected]>
1 parent 714301b commit 037f589

File tree

5 files changed

+177
-9
lines changed

5 files changed

+177
-9
lines changed

inc/agent.class.php

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,27 @@ public function canViewItem() {
386386
return $_SESSION['glpiID'] == $computer->getField('users_id');
387387
}
388388

389+
public function canUpdateItem() {
390+
// Check the active profile
391+
$config = Config::getConfigurationValues('flyvemdm', ['guest_profiles_id']);
392+
if ($_SESSION['glpiactiveprofile']['id'] != $config['guest_profiles_id']) {
393+
return parent::canUpdateItem();
394+
}
395+
396+
if (!$this->checkEntity(true)) {
397+
return false;
398+
}
399+
400+
// the active profile is guest user, then check the user is
401+
// owner of the item's computer
402+
$computer = $this->getComputer();
403+
if ($computer === null) {
404+
return false;
405+
}
406+
407+
return $_SESSION['glpiID'] == $computer->getField('users_id');
408+
}
409+
389410
/**
390411
* Sends a wipe command to the agent
391412
*/
@@ -469,6 +490,11 @@ public function prepareInputForAdd($input) {
469490
}
470491

471492
public function prepareInputForUpdate($input) {
493+
$config = Config::getConfigurationValues('flyvemdm', ['guest_profiles_id']);
494+
if ($_SESSION['glpiactiveprofile']['id'] == $config['guest_profiles_id']) {
495+
return $this->prepareInputForUpdateFromDevice($input);
496+
}
497+
472498
if (isset($input['plugin_flyvemdm_fleets_id'])) {
473499
// Update MQTT ACL for the fleet
474500
$oldFleet = new PluginFlyvemdmFleet();
@@ -547,6 +573,31 @@ public function prepareInputForUpdate($input) {
547573
return $input;
548574
}
549575

576+
/**
577+
* Prepare input for update from the agent itseld
578+
*
579+
* @param array $input
580+
* @return array
581+
*/
582+
private function prepareInputForUpdateFromDevice($input) {
583+
//Sanitize input
584+
unset($input[Computer::getForeignKeyField()]);
585+
unset($input[User::getForeignKeyField()]);
586+
unset($input[Entity::getForeignKeyField()]);
587+
unset($input[PluginFlyvemdmFleet::getForeignKeyField()]);
588+
unset($input['name']);
589+
unset($input['wipe']);
590+
unset($input['lock']);
591+
unset($input['enroll_status']);
592+
unset($input['last_report']);
593+
unset($input['last_contact']);
594+
unset($input['is_online']);
595+
unset($input['certificate']);
596+
unset($input['mdm_type']);
597+
598+
return $input;
599+
}
600+
550601
public function post_addItem() {
551602
// Notify the agent about its fleets
552603
$this->updateSubscription();
@@ -1294,6 +1345,15 @@ protected function enrollByInvitationToken($input) {
12941345
}
12951346
$computerId = $pfAgent->getField(Computer::getForeignKeyField());
12961347

1348+
// Check no Flyvemdm agent is linked to this computer
1349+
$agent = new self();
1350+
if ($agent->getFromDBByCrit(['computers_id' => $computerId])) {
1351+
// Save the agent ID in session to allow the device to find it
1352+
// and update it. Give up creation
1353+
$_SESSION['plugin_flyvemdm_agents_id'] = $agent->getID();
1354+
return false;
1355+
}
1356+
12971357
if ($computerId === 0) {
12981358
$event = __("Cannot create the device", 'flyvemdm');
12991359
$this->filterMessages($event);
@@ -1355,7 +1415,7 @@ protected function enrollByInvitationToken($input) {
13551415
// Create the agent
13561416
$defaultFleet = PluginFlyvemdmFleet::getDefaultFleet();
13571417
if ($defaultFleet === null) {
1358-
$event = __("No default fleet available for the device", 'flyvemdm');
1418+
$event = __('No default fleet available for the device', 'flyvemdm');
13591419
$this->filterMessages($event);
13601420
$this->logInvitationEvent($invitation, $event);
13611421
return false;

inc/invitation.class.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,18 +95,18 @@ public function getRights($interface = 'central') {
9595
public function prepareInputForAdd($input) {
9696
// integrity checks
9797
if (!isset($input['_useremails'])) {
98-
Session::addMessageAfterRedirect(__("Email address is invalid", 'flyvemdm'));
98+
Session::addMessageAfterRedirect(__('Email address is not set', 'flyvemdm'));
9999
return false;
100100
}
101101

102102
$input['_useremails'] = filter_var($input['_useremails'], FILTER_VALIDATE_EMAIL);
103103
if (!$input['_useremails']) {
104-
Session::addMessageAfterRedirect(__("Email address is invalid", 'flyvemdm'));
104+
Session::addMessageAfterRedirect(__('Email address is invalid', 'flyvemdm'));
105105
return false;
106106
}
107107

108108
// Find guest profile's id
109-
$config = Config::getConfigurationValues("flyvemdm", ['guest_profiles_id']);
109+
$config = Config::getConfigurationValues('flyvemdm', ['guest_profiles_id']);
110110
$guestProfileId = $config['guest_profiles_id'];
111111

112112
$entityId = $input['entities_id'];
@@ -134,15 +134,15 @@ public function prepareInputForAdd($input) {
134134
]);
135135

136136
if ($user->isNewItem()) {
137-
Session::addMessageAfterRedirect(__("Cannot create the user", 'flyvemdm'), false, INFO,
137+
Session::addMessageAfterRedirect(__('Cannot create the user', 'flyvemdm'), false, INFO,
138138
true);
139139
return false;
140140
}
141141

142142
} else {
143143
// Do not handle deleted users
144144
if ($user->isDeleted()) {
145-
Session::addMessageAfterRedirect(__("The user already exists and has been deleted. You must restore or purge him first.",
145+
Session::addMessageAfterRedirect(__('The user already exists and has been deleted. You must restore or purge him first.',
146146
'flyvemdm'), false, INFO, true);
147147
return false;
148148
}

install/upgrade_to_dev.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,21 @@ function upgrade(Migration $migration) {
4242

4343
$migration->setVersion(PLUGIN_FLYVEMDM_VERSION);
4444

45-
$profileRight = new ProfileRight();
46-
4745
$config = Config::getConfigurationValues('flyvemdm');
4846
if (!isset($config['mqtt_broker_port_backend'])) {
4947
// Split port setting for client in one hand and backend in the other hand
5048
$config['mqtt_broker_tls_port_backend'] = $config['mqtt_broker_tls_port'];
5149
$config['mqtt_broker_port_backend'] = $config['mqtt_broker_port'];
5250
Config::setConfigurationValues('flyvemdm', $config);
5351
}
52+
53+
// Merge new rights into guest profile
54+
$profileId = $config['guest_profiles_id'];
55+
$currentRights = ProfileRight::getProfileRights($profileId);
56+
$newRights = array_merge($currentRights, [
57+
PluginFlyvemdmAgent::$rightname => CREATE| READ | UPDATE ,
58+
]);
59+
$profileRight = new ProfileRight();
60+
$profileRight->updateProfileRights($profileId, $newRights);
5461
}
5562
}

tests/suite-integration/ProfileRight.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public function testGuestProfileRights() {
133133
$profileId = $config['guest_profiles_id'];
134134
// Expected rights
135135
$rightsSet = [
136-
\PluginFlyvemdmAgent::$rightname => READ | CREATE,
136+
\PluginFlyvemdmAgent::$rightname => READ | CREATE | UPDATE,
137137
\PluginFlyvemdmPackage::$rightname => READ,
138138
\PluginFlyvemdmFile::$rightname => READ,
139139
];

tests/suite-unit/PluginFlyvemdmAgent.php

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,4 +348,105 @@ public function testGetSpecificValueToDisplay() {
348348
$this->string($instance->getSpecificValueToDisplay('mdm_type',
349349
'android'))->contains('Android');
350350
}
351+
352+
353+
public function providerCanUpdateItem() {
354+
$this->login('glpi', 'glpi');
355+
$agent = $this->createAgent();
356+
$dataset = [
357+
[
358+
359+
]
360+
];
361+
362+
return $dataset;
363+
}
364+
365+
public function testCanUpdateItem() {
366+
// Check a super admin can update the agent
367+
$this->login('glpi', 'glpi');
368+
$agent = $this->createAgent();
369+
$output = $agent->canUpdateItem();
370+
$this->boolean($output)->isTrue();
371+
372+
// Check the acount of the agent can update it
373+
$user = new \User();
374+
$user->getFromDB($agent->fields[\User::getForeignKeyField()]);
375+
$this->loginWithUserToken($user->fields['api_token']);
376+
$output = $agent->canUpdateItem();
377+
$this->boolean($output)->isTrue();
378+
379+
$this->login('post-only', 'postonly');
380+
$output = $agent->canUpdateItem();
381+
$this->boolean($output)->isTrue(); // note that canUpdate() shall return false
382+
}
383+
384+
/**
385+
* @tags testPrepareInputForUpdate
386+
*/
387+
public function testPrepareInputForUpdate() {
388+
$this->login('glpi', 'glpi');
389+
$agent = $this->createAgent();
390+
list($user, $serial, $guestEmail, $invitation) = $this->createUserInvitation(\User::getForeignKeyField());
391+
$this->loginWithUserToken($user->fields['api_token']);
392+
$output = $agent->prepareInputForUpdate([
393+
\Computer::getForeignKeyField() => '0',
394+
\User::getForeignKeyField() => '0',
395+
\Entity::getForeignKeyField() => '0',
396+
\PluginFlyvemdmFleet::getForeignKeyField() => '0',
397+
'name' => '',
398+
'wipe' => '0',
399+
'lock' => '0',
400+
'enroll_status' => '',
401+
'last_report' => '',
402+
'last_contact' => '',
403+
'is_online' => '',
404+
'certificate' => '',
405+
'mdm_type' => '',
406+
]);
407+
408+
$this->array($output)->notHasKeys([
409+
\Computer::getForeignKeyField(),
410+
\User::getForeignKeyField(),
411+
\Entity::getForeignKeyField(),
412+
\PluginFlyvemdmFleet::getForeignKeyField(),
413+
'name',
414+
'wipe',
415+
'lock',
416+
'enroll_status',
417+
'last_report',
418+
'last_contact',
419+
'is_online',
420+
'certificate',
421+
'mdm_type',
422+
]);
423+
}
424+
425+
/**
426+
* @tags testPrepareInputForAdd
427+
*/
428+
public function testPrepareInputForAdd() {
429+
list($user, $serial, $guestEmail, $invitation) = $this->createUserInvitation(\User::getForeignKeyField());
430+
list($user2, $serial2, $guestEmail2, $invitation2) = $this->createUserInvitation(\User::getForeignKeyField());
431+
$invitationToken = $invitation->fields['invitation_token'];
432+
$invitationToken2 = $invitation2->fields['invitation_token'];
433+
$inventory = self::AgentXmlInventory($serial);
434+
$agent = $this->agentFromInvitation($user, $guestEmail, $serial, $invitationToken, 'android',
435+
'', $inventory, [], true);
436+
$this->array($_SESSION)->notHasKey('plugin_flyvemdm_agents_id');
437+
438+
// Inconsistency : creating an invitation for tests should not generate a serial
439+
// the serial should be crated in the caller instead
440+
list($user, $serial, $guestEmail, $invitation) = $this->createUserInvitation(\User::getForeignKeyField());
441+
$newAgent = $this->agentFromInvitation($user2, $guestEmail2, $serial, $invitationToken2, 'android',
442+
'', $inventory, [], false);
443+
// Check the agent is not created
444+
$this->boolean($newAgent->isNewItem())->isTrue();
445+
// Do not test the message in session: it may be filtered when
446+
// debug mode for enrollment is disabled
447+
// Instead, let's check if the session contains the ID of the agent to update
448+
$this->array($_SESSION)->hasKey('plugin_flyvemdm_agents_id');
449+
$this->integer((int) $_SESSION['plugin_flyvemdm_agents_id'])
450+
->isEqualTo($agent->getID());
451+
}
351452
}

0 commit comments

Comments
 (0)