Merge pull request #14529 from mauro-miatello/develop

Hide/Show encrypted values in hardware list
This commit is contained in:
snipe 2024-04-04 15:07:58 +01:00 committed by GitHub
commit dcba2bfd25
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 76 additions and 13 deletions

View file

@ -414,19 +414,24 @@
@if ($field->isFieldDecryptable($asset->{$field->db_column_name()} ))
@can('assets.view.encrypted_custom_fields')
<span id="text-{{ $field->id }}-to-hide">********</span>
<span class="js-copy-{{ $field->id }}" id="text-{{ $field->id }}-to-show" style="font-size: 0px;">
@if (($field->format=='URL') && ($asset->{$field->db_column_name()}!=''))
<a href="{{ Helper::gracefulDecrypt($field, $asset->{$field->db_column_name()}) }}" target="_new">{{ Helper::gracefulDecrypt($field, $asset->{$field->db_column_name()}) }}</a>
@elseif (($field->format=='DATE') && ($asset->{$field->db_column_name()}!=''))
{{ \App\Helpers\Helper::gracefulDecrypt($field, \App\Helpers\Helper::getFormattedDateObject($asset->{$field->db_column_name()}, 'date', false)) }}
@else
{{ Helper::gracefulDecrypt($field, $asset->{$field->db_column_name()}) }}
@endif
</span>
<i class="fa-regular fa-clipboard js-copy-link" data-clipboard-target=".js-copy-{{ $field->id }}" aria-hidden="true" data-tooltip="true" data-placement="top" title="{{ trans('general.copy_to_clipboard') }}">
<span class="sr-only">{{ trans('general.copy_to_clipboard') }}</span>
</i>
@php
$fieldSize=strlen(Helper::gracefulDecrypt($field, $asset->{$field->db_column_name()}))
@endphp
@if ($fieldSize>0)
<span id="text-{{ $field->id }}-to-hide">{{ str_repeat('*', $fieldSize) }}</span>
<span class="js-copy-{{ $field->id }}" id="text-{{ $field->id }}-to-show" style="font-size: 0px;">
@if (($field->format=='URL') && ($asset->{$field->db_column_name()}!=''))
<a href="{{ Helper::gracefulDecrypt($field, $asset->{$field->db_column_name()}) }}" target="_new">{{ Helper::gracefulDecrypt($field, $asset->{$field->db_column_name()}) }}</a>
@elseif (($field->format=='DATE') && ($asset->{$field->db_column_name()}!=''))
{{ \App\Helpers\Helper::gracefulDecrypt($field, \App\Helpers\Helper::getFormattedDateObject($asset->{$field->db_column_name()}, 'date', false)) }}
@else
{{ Helper::gracefulDecrypt($field, $asset->{$field->db_column_name()}) }}
@endif
</span>
<i class="fa-regular fa-clipboard js-copy-link" data-clipboard-target=".js-copy-{{ $field->id }}" aria-hidden="true" data-tooltip="true" data-placement="top" title="{{ trans('general.copy_to_clipboard') }}">
<span class="sr-only">{{ trans('general.copy_to_clipboard') }}</span>
</i>
@endif
@else
{{ strtoupper(trans('admin/custom_fields/general.encrypted')) }}
@endcan

View file

@ -1041,6 +1041,64 @@
});
});
// Select encrypted custom fields to hide them in the asset list
$(document).ready(function() {
// Selector for elements with css-padlock class
var selector = 'td.css-padlock';
// Function to add original value to elements
function addValue($element) {
// Get original value of the element
var originalValue = $element.text().trim();
// Show asterisks only for not empty values
if (originalValue !== '') {
// This is necessary to avoid loop because value is generated dynamically
if (originalValue !== '' && originalValue !== asterisks) $element.attr('value', originalValue);
// Hide the original value and show asterisks of the same length
var asterisks = '*'.repeat(originalValue.length);
$element.text(asterisks);
// Add click event to show original text
$element.click(function() {
var $this = $(this);
if ($this.text().trim() === asterisks) {
$this.text($this.attr('value'));
} else {
$this.text(asterisks);
}
});
}
}
// Add value to existing elements
$(selector).each(function() {
addValue($(this));
});
// Function to handle mutations in the DOM because content is generated dynamically
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
// Check if new nodes have been inserted
if (mutation.type === 'childList') {
mutation.addedNodes.forEach(function(node) {
if ($(node).is(selector)) {
addValue($(node));
} else {
$(node).find(selector).each(function() {
addValue($(this));
});
}
});
}
});
});
// Configure the observer to observe changes in the DOM
var config = { childList: true, subtree: true };
observer.observe(document.body, config);
});
</script>