This repository has been archived by the owner on Aug 9, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 79
WIP Add APN policy #550
Open
DIOHz0r
wants to merge
3
commits into
flyve-mdm:develop
Choose a base branch
from
DIOHz0r:feature/404_apn_policy
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
WIP Add APN policy #550
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 . '\]\'[^>]*>.*/', | ||
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.