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

WIP Add APN policy #550

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
147 changes: 147 additions & 0 deletions inc/policyapn.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
<?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/.
* ------------------------------------------------------------------------------
* @copyright Copyright © 2018 Teclib
* @license http://www.gnu.org/licenses/agpl.txt AGPLv3+
* @link https://github.com/flyve-mdm/glpi-plugin
* @link https://flyve-mdm.com/
* ------------------------------------------------------------------------------
*/

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

/**
* Class PluginFlyvemdmPolicyApn
* @since 2.1
*/
class PluginFlyvemdmPolicyApn extends PluginFlyvemdmPolicyBase implements PluginFlyvemdmPolicyInterface {

private $formFields = [];
private $apnAuthType = [];
private $apnType = [];

/**
* PluginFlyvemdmPolicyApn constructor.
* @param PluginFlyvemdmPolicy $policy
*/
public function __construct(PluginFlyvemdmPolicy $policy) {
$this->formFields = [
'apn_name' => ['label' => __('Name', 'flyvemdm'), 'type' => 'text'],
'apn_fqn' => ['label' => __('APN', 'flyvemdm'), 'type' => 'text'],
'apn_proxy' => ['label' => __('Proxy', 'flyvemdm'), 'type' => 'text'],
'apn_port' => ['label' => __('Port', 'flyvemdm'), 'type' => 'text'],
'apn_username' => ['label' => __('Username', 'flyvemdm'), 'type' => 'text'],
'apn_password' => ['label' => __('Password', 'flyvemdm'), 'type' => 'password'],
'apn_server' => ['label' => __('Server', 'flyvemdm'), 'type' => 'text'],
'apn_mmsc' => ['label' => __('MMSC', 'flyvemdm'), 'type' => 'text'],
'apn_proxy_mms' => ['label' => __('Proxy MMS', 'flyvemdm'), 'type' => 'text'],
'apn_proxy_mms_port' => ['label' => __('Proxy MMC port', 'flyvemdm'), 'type' => 'text'],
'apn_mmc' => ['label' => __('MMC', 'flyvemdm'), 'type' => 'text'],
'apn_mnc' => ['label' => __('MNC', 'flyvemdm'), 'type' => 'text'],
];
$this->apnType = [__('Default'), 'MMS', 'SUPL', 'DUN', 'HIPRI', 'FOTA'];
$this->apnAuthType = [__('No authentication'), 'PAP', 'CHAP', 'CHAP/PAP'];
parent::__construct($policy);
$this->symbol = $policy->getField('symbol');
$this->unicityRequired = ($policy->getField('unicity') != '0');
$this->group = $policy->getField('group');
}

/**
* @param mixed $value
* @param mixed $itemtype
* @param integer $itemId
* @return bool
*/
public function integrityCheck($value, $itemtype, $itemId) {
// Check the value exists
if (!isset($value['apn_name']) || !$value['apn_name']) {
Session::addMessageAfterRedirect(__('APN name is mandatory', 'flyvemdm'));
return false;
}
if (!isset($value['apn_fqn']) || !$value['apn_fqn']) {
Session::addMessageAfterRedirect(__('APN value is mandatory', 'flyvemdm'));
return false;
}
return true;
}

/**
* @param mixed $value
* @param mixed $itemtype
* @param int $itemId
* @return array|bool
*/
public function getBrokerMessage($value, $itemtype, $itemId) {
$decodedValue = json_decode($value, JSON_OBJECT_AS_ARRAY);
if (!$this->integrityCheck($decodedValue, $itemtype, $itemId)) {
return false;
}
$array = [
$this->symbol => $value,
];
return $array;
}

public function showValueInput($value = '', $itemType = '', $itemId = 0) {

$value = json_decode($value, JSON_OBJECT_AS_ARRAY);

$data = [];
foreach ($this->formFields as $inputName => $inputOptions) {
$data['inputs'][] = [
'name' => "value[$inputName]",
'label' => $inputOptions['label'],
'type' => $inputOptions['type'],
'value' => ($value[$inputName]) ? $value[$inputName] : '',
];
}

$apnAuthType = ($value['apn_auth_type']) ? $value['apn_auth_type'] : 0;
$apnType = ($value['apn_type']) ? $value['apn_type'] : 0;

$data['apnAuthType'] = [
'label' => 'Authentication Type',
'dropdown' => Dropdown::showFromArray('value[apn_auth_type]',
$this->apnAuthType,
['display' => false, 'value' => $apnAuthType]),
];
$data['apnType'] = [
'label' => 'APN Type',
'dropdown' => Dropdown::showFromArray('value[apn_type]',
$this->apnType,
['display' => false, 'value' => $apnType]),
];

$twig = plugin_flyvemdm_getTemplateEngine();
return $twig->render('policy_apn_form.html.twig', $data);
}

public function showValue(PluginFlyvemdmTask $task) {
$values = json_decode($task->getField('value'), JSON_OBJECT_AS_ARRAY);
$stringValues = $values['apn_name'];
return $stringValues;
}
}
4 changes: 4 additions & 0 deletions inc/policyfactory.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ public function createFromPolicy(PluginFlyvemdmPolicy $policyData) {
$policy = new PluginFlyvemdmPolicyRemovefile($policyData);
break;

case 'apn':
$policy = new PluginFlyvemdmPolicyApn($policyData);
break;

default:
return null;
}
Expand Down
16 changes: 16 additions & 0 deletions install/policies/peripherals.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,4 +216,20 @@
'apple_min_version' => '0',
'apple_max_version' => '0',
],

[
'name' => __('APN configuration', 'flyvemdm'),
'symbol' => 'apnConfiguration',
'group' => 'connectivity',
'type' => 'apn',
'type_data' => '',
'unicity' => 0,
'plugin_flyvemdm_policycategories_id' => $category,
'comment' => __('Set-up you APN values manually', 'flyvemdm'),
'default_value' => '',
'recommended_value' => '',
'is_android_policy' => '1',
'is_android_system' => '0',
'is_apple_policy' => '0',
],
];
1 change: 1 addition & 0 deletions tests/src/Flyvemdm/Tests/CommonTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,7 @@ public static function policyList() {
'Policy/disableStreamSystem',
'Policy/defaultStreamType',
'Policy/periodicGeolocation',
'Policy/apnConfiguration',
];
}

Expand Down
134 changes: 134 additions & 0 deletions tests/suite-unit/PluginFlyvemdmPolicyApn.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
<?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/.
* ------------------------------------------------------------------------------
* @copyright Copyright © 2018 Teclib
* @license http://www.gnu.org/licenses/agpl.txt AGPLv3+
* @link https://github.com/flyve-mdm/glpi-plugin
* @link https://flyve-mdm.com/
* ------------------------------------------------------------------------------
*/

namespace tests\units;

use Flyvemdm\Tests\CommonTestCase;

class PluginFlyvemdmPolicyApn extends CommonTestCase {

private $dataField = [
'group' => 'connectivity',
'symbol' => 'apnConfiguration',
'type' => 'apn',
'type_data' => '',
'unicity' => '0',
];

protected function validationProvider() {
return [
'Check apn_name is not set' => [
'data' => [[], null, null],
'expected' => [false, 'APN name is mandatory'],
],
'Check apn_name is not empty' => [
'data' => [['apn_name' => ''], null, null],
'expected' => [false, 'APN name is mandatory'],
],
'Check apn_fqn is not set' => [
'data' => [['apn_name' => 'lorem'], null, null],
'expected' => [false, 'APN value is mandatory'],
],
'Check apn_fqn is not empty' => [
'data' => [['apn_name' => 'lorem', 'apn_fqn' => ''], null, null],
'expected' => [false, 'APN value is mandatory'],
],
'Valid check 1' => [
'data' => [['apn_name' => 'lorem', 'apn_fqn' => 'ipsum'], null, null],
'expected' => [true],
],
];
}

/**
* @dataProvider validationProvider
* @tags testCreatePolicy
* @param array $data
* @param array $expected
*/
public function testCreatePolicy($data, $expected) {
list($policy) = $this->createNewPolicyInstance();
$success = $policy->integrityCheck($data[0], $data[1], $data[2]);
$this->boolean($success)->isEqualTo($expected[0]);
if (!$expected[0]) {
$this->string($_SESSION["MESSAGE_AFTER_REDIRECT"][0][0])->isEqualTo($expected[1]);
unset($_SESSION["MESSAGE_AFTER_REDIRECT"]); // to clear the buffer
}
}

private function createNewPolicyInstance() {
$policyData = new \PluginFlyvemdmPolicy();
$policyData->fields = $this->dataField;
$policy = $this->newTestedInstance($policyData);
return [$policy, $policyData];
}

/**
* @tags testGetBrokerMessage
*/
public function testGetBrokerMessage() {
list($policy) = $this->createNewPolicyInstance();

$this->boolean($policy->getBrokerMessage(null, null, null))->isFalse();
$value = '{"apn_name":"lorem","apn_fqn":"ipsum","apn_auth_type":"0","apn_type":"0"}';
$result = $policy->getBrokerMessage($value, null, null);
$this->array($result)->hasKeys([$this->dataField['symbol']])
->string($result[$this->dataField['symbol']])->contains('"apn_name":"lorem","apn_fqn":"ipsum"');
}

/**
* @tags testShowValueInput
*/
public function testShowValueInput() {
list($policy) = $this->createNewPolicyInstance();
$value = $policy->showValueInput();
$this->string($value)
->contains('input type="text" name="value[apn_name]" value=""')
->contains('input type="text" name="value[apn_fqn]" value=""');

$dropdowns = ['apn_auth_type', 'apn_type'];
foreach ($dropdowns as $inputName) {
$matches = null;
preg_match('/.*<select[^>]*name=\'value\[' . $inputName . '\]\'[^>]*>.*/',
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like there's a label missing for this input. That makes it hard for people using screen readers or voice control to use the input. If you don't want a visual label, try an aria-label attribute.

DIOHz0r marked this conversation as resolved.
Show resolved Hide resolved
$value, $matches);
$this->array($matches)->hasSize(1);
}
}

/**
* @tags testShowValue
*/
public function testShowValue() {
list($policy) = $this->createNewPolicyInstance();
$mockInstance = $this->newMockInstance('\PluginFlyvemdmTask');
$mockInstance->getMockController()->getField = '{"apn_name":"lorem","apn_fqn":"ipsum","apn_auth_type":"0","apn_type":"0"}';
$this->string($policy->showValue($mockInstance))->isEqualTo('lorem');
}
}
26 changes: 26 additions & 0 deletions tpl/policy_apn_form.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<table>
{% for input in inputs %}
{% if loop.index0 is even %}
<tr>
{% endif %}
<td><label for="{{ input.name }}">{{ input.label }}</label></td>
<td><input type="{{ input.type }}" name="{{ input.name }}" value="{{ input.value }}"></td>
DIOHz0r marked this conversation as resolved.
Show resolved Hide resolved
{% if loop.index0 is odd %}
</tr>
{% endif %}
{% endfor %}
<tr>
<td>
<label>{{ apnAuthType.label }}</label>
</td>
<td>
{{ apnAuthType.dropdown|raw }}
</td>
<td>
<label>{{ apnType.label }}</label>
</td>
<td>
{{ apnType.dropdown|raw }}
</td>
</tr>
</table>