fixes booleans, adds note, changes name

This commit is contained in:
spencerrlongg
2025-07-21 15:34:08 -05:00
parent 2f74a8afe1
commit e9948f0718
4 changed files with 45 additions and 5 deletions

View File

@@ -224,7 +224,11 @@ class Asset extends Depreciable
foreach ($this->model->fieldset->fields as $field) {
if ($field->format == 'BOOLEAN') {
// this just casts booleans that may come through as strings to an actual boolean type
// adding !$field->field_encrypted because when the encrypted value comes through it
// screws things up for the encrypted validation rules (and the encrypted string
// is not a valid boolean type)
if ($field->format == 'BOOLEAN' && !$field->field_encrypted) {
$this->{$field->db_column} = filter_var($this->{$field->db_column}, FILTER_VALIDATE_BOOLEAN);
}
}

View File

@@ -3,12 +3,13 @@
namespace App\Models;
use App\Rules\AlphaEncrypted;
use App\Rules\BooleanEncrypted;
use App\Rules\DateEncrypted;
use App\Rules\EmailEncrypted;
use App\Rules\IPEncrypted;
use App\Rules\IPv4Encrypted;
use App\Rules\IPv6Encrypted;
use App\Rules\MACEncrypted;
use App\Rules\MacEncrypted;
use App\Rules\NumericEncrypted;
use App\Rules\UrlEncrypted;
use Gate;
@@ -138,7 +139,8 @@ class CustomFieldset extends Model
$ipKey = array_search('ip', $rules[$field->db_column_name()]);
$ipv4Key = array_search('ipv4', $rules[$field->db_column_name()]);
$ipv6Key = array_search('ipv6', $rules[$field->db_column_name()]);
$macKey = array_search('regex', $rules[$field->db_column_name()]);
$macKey = array_search('regex:/^[a-fA-F0-9]{2}:[a-fA-F0-9]{2}:[a-fA-F0-9]{2}:[a-fA-F0-9]{2}:[a-fA-F0-9]{2}:[a-fA-F0-9]{2}$/', $rules[$field->db_column_name()]);
$booleanKey = array_search('boolean', $rules[$field->db_column_name()]);
match ($field->format) {
'NUMERIC' => $rules[$field->db_column_name()][$numericKey] = new NumericEncrypted,
'ALPHA' => $rules[$field->db_column_name()][$alphaKey] = new AlphaEncrypted,
@@ -148,7 +150,8 @@ class CustomFieldset extends Model
'IP' => $rules[$field->db_column_name()][$ipKey] = new IPEncrypted,
'IPV4' => $rules[$field->db_column_name()][$ipv4Key] = new IPv4Encrypted,
'IPV6' => $rules[$field->db_column_name()][$ipv6Key] = new IPv6Encrypted,
'MAC' => $rules[$field->db_column_name()][$macKey] = new MACEncrypted,
'MAC' => $rules[$field->db_column_name()][$macKey] = new MacEncrypted,
'BOOLEAN' => $rules[$field->db_column_name()][$booleanKey] = new BooleanEncrypted,
default => null,
};
}

View File

@@ -0,0 +1,33 @@
<?php
namespace App\Rules;
use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Validation\Concerns\ValidatesAttributes;
class BooleanEncrypted implements ValidationRule
{
use ValidatesAttributes;
/**
* Run the validation rule.
*
* @param \Closure(string, ?string=): \Illuminate\Translation\PotentiallyTranslatedString $fail
*/
public function validate(string $attribute, mixed $value, Closure $fail): void
{
try {
$attributeName = trim(preg_replace('/_+|snipeit|\d+/', ' ', $attribute));
$decrypted = Crypt::decrypt($value);
if (!$this->validateBoolean($attributeName, $decrypted) && !is_null($decrypted)) {
$fail(trans('validation.ipv6', ['attribute' => $attributeName]));
}
} catch (\Exception $e) {
report($e);
$fail(trans('general.something_went_wrong'));
}
}
}

View File

@@ -8,7 +8,7 @@ use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Validation\Concerns\ValidatesAttributes;
class MACEncrypted implements ValidationRule
class MacEncrypted implements ValidationRule
{
use ValidatesAttributes;