A free open source IT asset/license management system
fixes booleans, adds note, changes name
spencerrlongge9948f07189a
diff --git a/app/Models/Asset.php b/app/Models/Asset.phpindex 0f6becd835..a9ec9e61a1 100644--- a/app/Models/Asset.php+++ b/app/Models/Asset.php@@ -224,7 +224,11 @@ class Asset extends Depreciableforeach ($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);}}diff --git a/app/Models/CustomFieldset.php b/app/Models/CustomFieldset.phpindex 083ecb30d0..86c5d44e67 100644--- a/app/Models/CustomFieldset.php+++ b/app/Models/CustomFieldset.php@@ -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,};}diff --git a/app/Rules/BooleanEncrypted.php b/app/Rules/BooleanEncrypted.phpnew file mode 100644index 0000000000..06fd370703--- /dev/null+++ b/app/Rules/BooleanEncrypted.php@@ -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'));+ }+ }+}diff --git a/app/Rules/MACEncrypted.php b/app/Rules/MacEncrypted.phpsimilarity index 95%rename from app/Rules/MACEncrypted.phprename to app/Rules/MacEncrypted.phpindex 27f012ef5d..c9b125a900 100644--- a/app/Rules/MACEncrypted.php+++ b/app/Rules/MacEncrypted.php@@ -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;