Skip to content

Commit

Permalink
custom field option profile restrictions
Browse files Browse the repository at this point in the history
  • Loading branch information
cconard96 committed Feb 11, 2025
1 parent d14f22c commit bb0f58e
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 2 deletions.
98 changes: 98 additions & 0 deletions src/Glpi/Asset/CustomFieldOption/ProfileRestrictOption.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

/**
* ---------------------------------------------------------------------
*
* GLPI - Gestionnaire Libre de Parc Informatique
*
* http://glpi-project.org
*
* @copyright 2015-2025 Teclib' and contributors.
* @licence https://www.gnu.org/licenses/gpl-3.0.html
*
* ---------------------------------------------------------------------
*
* LICENSE
*
* This file is part of GLPI.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* ---------------------------------------------------------------------
*/

namespace Glpi\Asset\CustomFieldOption;

use Glpi\Application\View\TemplateRenderer;

class ProfileRestrictOption extends AbstractOption
{
public function getFormInput(): string
{
$twig_params = [
'item' => $this->custom_field,
'key' => $this->getKey(),
'label' => $this->getName(),
'value' => parent::getValue(),
'inverted' => $this->getInverted(),
];
// language=Twig
return TemplateRenderer::getInstance()->renderFromStringTemplate(<<<TWIG
{% import 'components/form/fields_macros.html.twig' as fields %}
{% set match_invert_field %}
{{ fields.sliderField('field_options[' ~ key ~ '_invert]', inverted, __('Restrict to all but these profiles'), {
field_class: 'col-12',
label_class: 'col-xxl-10',
input_class: 'col-xxl-2'
}) }}
{% endset %}
{{ fields.dropdownField('Profile', 'field_options[' ~ key ~ ']', value, label, {
multiple: true,
add_field_html: match_invert_field,
to_add: {
'-1': __('All')
},
condition: {
'interface': 'central'
}
}) }}
TWIG, $twig_params);
}

protected function getInverted(): bool
{
return (bool) ($this->custom_field->fields['field_options'][$this->getKey() . '_invert'] ?? false);
}

public function getValue(): bool
{
$inverted = $this->getInverted();
$value = parent::getValue() ?? [];

if (!is_array($value)) {
$value = [$value];
}

// Handle special 'All' value
if (in_array(-1, $value, true)) {
return !$inverted;
}

$active_profile = $_SESSION['glpiactiveprofile']['id'] ?? null;
if ($active_profile === null) {
return false;
}
return $inverted ? !in_array($active_profile, $value, false) : in_array($active_profile, $value, false);
}
}
4 changes: 3 additions & 1 deletion src/Glpi/Asset/CustomFieldType/AbstractType.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

use Glpi\Asset\CustomFieldDefinition;
use Glpi\Asset\CustomFieldOption\BooleanOption;
use Glpi\Asset\CustomFieldOption\ProfileRestrictOption;
use Glpi\DBAL\QueryExpression;
use Glpi\DBAL\QueryFunction;

Expand Down Expand Up @@ -70,9 +71,10 @@ public function getOptions(): array
{
return [
new BooleanOption($this->custom_field, 'full_width', __('Full width'), false),
new BooleanOption($this->custom_field, 'readonly', __('Readonly'), false),
new ProfileRestrictOption($this->custom_field, 'readonly', __('Readonly'), false),
new BooleanOption($this->custom_field, 'required', __('Mandatory'), false),
new BooleanOption($this->custom_field, 'disabled', __('Disabled'), false), // Not exposed in the UI. Only used in field order preview
new ProfileRestrictOption($this->custom_field, 'hidden', __('Hidden'), false),
];
}

Expand Down
2 changes: 1 addition & 1 deletion tests/cypress/e2e/Asset/custom_fields.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ describe("Custom Assets - Custom Fields", () => {
cy.findByLabelText('Multiple values').check();
}
if (options.has('readonly')) {
cy.findByLabelText('Readonly').check();
cy.findByLabelText('Readonly').selectDropdownValue('Super-Admin');
}
if (options.has('mandatory')) {
cy.findByLabelText('Mandatory').check();
Expand Down

0 comments on commit bb0f58e

Please sign in to comment.