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

Commit

Permalink
feat(policy): show policy restrictions for android
Browse files Browse the repository at this point in the history
Signed-off-by: Thierry Bugier <tbugier@teclib.com>
  • Loading branch information
btry authored and DIOHz0r committed Aug 30, 2018
1 parent b6e591c commit bbb9e09
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 30 deletions.
1 change: 0 additions & 1 deletion ajax/policyValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
$policy = $factory->createFromDBByID($policyId);
if ($policy !== null) {
echo $policy->formGenerator($mode, [
'policyId' => $policyId,
'task' => (int) ((isset($_POST['task'])) ? $_POST['task'] : 0),
'itemtype_applied' => ((isset($_POST['itemtype_applied'])) ? $_POST['itemtype_applied'] : ''),
'items_id_applied' => (int) ((isset($_POST['items_id_applied'])) ? $_POST['items_id_applied'] : 0),
Expand Down
35 changes: 33 additions & 2 deletions inc/policybase.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ public function showValueInput($value = '', $itemType = '', $itemId = 0) {
$data['itemtype'] = $itemType;
$data['value'] = $value;
$data['typeTmpl'] = PluginFlyvemdmPolicyBase::class;
$data['android_requirements'] = $this->getAndroidCompatibilityMessage();
$twig = plugin_flyvemdm_getTemplateEngine();
return $twig->render('policy_value.html.twig', ['data' => $data]);
}
Expand Down Expand Up @@ -252,13 +253,13 @@ public function formGenerator(
}
$form = [
'mode' => $mode,
'input' => $this->showValueInput($value, $itemtype, $itemId)
'input' => $this->showValueInput($value, $itemtype, $itemId),
];
if ($mode == "update") {
$form['url'] = Toolbox::getItemTypeFormURL(PluginFlyvemdmTask::class);
$form['rand'] = mt_rand();
$form['taskId'] = $_input['task'];
$form['policyId'] = $_input['policyId'];
$form['policyId'] = $this->policyData->getID();
$form['itemtype_applied'] = $_input['itemtype_applied'];
$form['items_id_applied'] = $_input['items_id_applied'];
$form['_csrf'] = (GLPI_USE_CSRF_CHECK) ? Html::hidden('_glpi_csrf_token',
Expand All @@ -267,4 +268,34 @@ public function formGenerator(
$twig = plugin_flyvemdm_getTemplateEngine();
return $twig->render('policy_form.html.twig', ['form' => $form]);
}

protected function getAndroidCompatibilityMessage() {
$min = $this->policyData->getField('android_min_version');
$max = $this->policyData->getField('android_max_version');

$message = '';
if ($min == '0' && $max != '0') {
$message = sprintf(
__('Compatibility with Android up to %1$s.', 'flyvemdm'),
$max
);
}
if ($min != '0' && $max == '0') {
$message = sprintf(
__('Compatibility with Android from %1$s.', 'flyvemdm'),
$min
);
}
if ($min != '0' && $max != '0') {
$message = sprintf(
__('Compatibility with Android %1$s to %2$s.', 'flyvemdm'),
$min,
$max
);
}
if ($this->policyData->getField('is_android_system') != '0') {
$message .= ' ' . sprintf('Requires system privileges.', 'flyvemdm');
}
return $message;
}
}
7 changes: 1 addition & 6 deletions inc/policyboolean.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,13 @@ public function getMqttMessage($value, $itemtype, $itemId) {
return $array;
}

/**
* @param string $value
* @param string $itemType
* @param int $itemId
* @return int|string
*/
public function showValueInput($value = '', $itemType = '', $itemId = 0) {
$data['itemtype'] = $itemType;
$data['typeTmpl'] = PluginFlyvemdmPolicyBoolean::class;
$data['dropdown'] = [
Dropdown::showYesNo('value', $value, -1, ['display' => false])
];
$data['android_requirements'] = $this->getAndroidCompatibilityMessage();
$twig = plugin_flyvemdm_getTemplateEngine();
return $twig->render('policy_value.html.twig', ['data' => $data]);
}
Expand Down
1 change: 1 addition & 0 deletions inc/policydeployapplication.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ public function showValueInput($value = '', $itemType = '', $itemId = 0) {
]),
Dropdown::showYesNo('value[remove_on_delete]', $removeOnDelete, -1, ['display' => false])
];
$data['android_requirements'] = $this->getAndroidCompatibilityMessage();

$twig = plugin_flyvemdm_getTemplateEngine();
return $twig->render('policy_value.html.twig', ['data' => $data]);
Expand Down
7 changes: 1 addition & 6 deletions inc/policydeployfile.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,12 +258,6 @@ public function post_unapply($value, $itemtype, $itemId, PluginFlyvemdmNotifiabl
$task->add($this->postUnapplyTask);
}

/**
* @param string $value
* @param string $itemType
* @param int $itemId
* @return string|void
*/
public function showValueInput($value = '', $itemType = '', $itemId = 0) {
$itemtype = PluginFlyvemdmFile::class;
$removeOnDelete = 1;
Expand Down Expand Up @@ -297,6 +291,7 @@ public function showValueInput($value = '', $itemType = '', $itemId = 0) {
]),
Dropdown::showYesNo('value[remove_on_delete]', $removeOnDelete, -1, ['display' => false])
];
$data['android_requirements'] = $this->getAndroidCompatibilityMessage();
$twig = plugin_flyvemdm_getTemplateEngine();
return $twig->render('policy_value.html.twig', ['data' => $data]);
}
Expand Down
7 changes: 1 addition & 6 deletions inc/policydropdown.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,18 +95,13 @@ public function translateData() {
return $translated;
}

/**
* @param string $value
* @param string $itemType
* @param int $itemId
* @return int|string
*/
public function showValueInput($value = '', $itemType = '', $itemId = 0) {
$data['itemtype'] = $itemType;
$data['typeTmpl'] = PluginFlyvemdmPolicyDropdown::class;
$data['dropdown'] = [
Dropdown::showFromArray('value', $this->valueList, ['display' => false, 'value' => $value])
];
$data['android_requirements'] = $this->getAndroidCompatibilityMessage();
$twig = plugin_flyvemdm_getTemplateEngine();
return $twig->render('policy_value.html.twig', ['data' => $data]);
}
Expand Down
22 changes: 15 additions & 7 deletions tests/suite-unit/PluginFlyvemdmPolicyBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,23 @@ class PluginFlyvemdmPolicyBase extends CommonTestCase {

private $dataField = [];

public function beforeTestMethod($method) {
parent::beforeTestMethod($method);
switch ($method) {
case 'testFormGenerator':
$this->login('glpi', 'glpi');
break;
}
}

/**
* @return array
*/
private function createNewPolicyInstance() {
private function createNewPolicyInstance($symbol = null) {
$policyData = new \PluginFlyvemdmPolicy();
$policyData->fields = $this->dataField;
if ($symbol !== null) {
$policyData->getFromDbBySymbol($symbol);
}
$policy = $this->newTestedInstance($policyData);
return [$policy, $policyData];
}
Expand Down Expand Up @@ -126,6 +137,7 @@ public function testShowValueInput() {
$data['itemtype'] = '';
$data['value'] = '';
$data['typeTmpl'] = \PluginFlyvemdmPolicyBase::class;
$data['android_requirements'] = 'Compatibility with Android N/A to N/A. Requires system privileges.';
$twig = plugin_flyvemdm_getTemplateEngine();
$this->string($policy->showValueInput())->isEqualTo($twig->render('policy_value.html.twig', ['data' => $data]));
}
Expand Down Expand Up @@ -204,8 +216,6 @@ public function providerFilterStatus() {
* @tags testFormGenerator
*/
public function testFormGenerator() {
$this->login('glpi', 'glpi');

$existingPolicy = new \PluginFlyvemdmPolicy();
$existingPolicy->getFromDbBySymbol('storageEncryption');
$fleet = $this->createFleet(['name' => $this->getUniqueString()]);
Expand All @@ -221,10 +231,9 @@ public function testFormGenerator() {
'items_id' => '',
]);

list($policy) = $this->createNewPolicyInstance();
list($policy) = $this->createNewPolicyInstance($existingPolicy->getField('symbol'));
// add action
$html = $policy->formGenerator('add', [
'policyId' => $policyId,
'task' => 0,
'itemtype_applied' => '',
'items_id_applied' => 0,
Expand All @@ -236,7 +245,6 @@ public function testFormGenerator() {
// edit action
$taskId = $task->getID();
$html = $policy->formGenerator('update', [
'policyId' => $policyId,
'task' => $taskId,
'itemtype_applied' => 'fleet',
'items_id_applied' => $fleetId,
Expand Down
8 changes: 7 additions & 1 deletion tpl/fleet_policy.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@
<div id="plugin_flyvemdm_policy_value"></div>
</td>
</tr>
<tr>
<td colspan="2">
<span id="plugin_flyvemdm_policy_android_requirements"></span>
<br>
<!-- <span id="plugin_flyvemdm_policy_ios_requirements"></span> -->
</td>
</tr>
<tr>
<td colspan="2" class="center">
<input type="hidden" name="itemtype_applied" value="{{ task.itemtype_applied }}">
Expand Down Expand Up @@ -62,7 +69,6 @@
<th>{{ __('Policy', 'flyvemdm') }}</th>
<th>{{ __('Value', 'flyvemdm') }}</th>
</tr>

</table>
{% if canEdit %}
{{ massiveActionForm.end|raw }}
Expand Down
5 changes: 4 additions & 1 deletion tpl/policy_value.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,7 @@
<script type="text/javascript">
$(document).find(editorId).dialog("option", "width", 200);
</script>
{% endif %}
{% endif %}
<script type="text/javascript">
$(document).find('#plugin_flyvemdm_policy_android_requirements')[0].innerText = "{{ data.android_requirements }}";
</script>

0 comments on commit bbb9e09

Please sign in to comment.