From f9b05bc8deb46a0c5e5a0fc12c314bfc7f455897 Mon Sep 17 00:00:00 2001 From: spencerrlongg Date: Tue, 15 Jul 2025 15:03:51 -0500 Subject: [PATCH] more encryption rules extenting laravel's own --- app/Models/CustomFieldset.php | 14 +++++++++++++- app/Rules/AlphaEncrypted.php | 2 +- app/Rules/DateEncrypted.php | 32 ++++++++++++++++++++++++++++++++ app/Rules/IPEncrypted.php | 32 ++++++++++++++++++++++++++++++++ app/Rules/IPv4Encrypted.php | 32 ++++++++++++++++++++++++++++++++ app/Rules/IPv6Encrypted.php | 32 ++++++++++++++++++++++++++++++++ app/Rules/MACEncrypted.php | 32 ++++++++++++++++++++++++++++++++ app/Rules/UrlEncrypted.php | 32 ++++++++++++++++++++++++++++++++ 8 files changed, 206 insertions(+), 2 deletions(-) create mode 100644 app/Rules/DateEncrypted.php create mode 100644 app/Rules/IPEncrypted.php create mode 100644 app/Rules/IPv4Encrypted.php create mode 100644 app/Rules/IPv6Encrypted.php create mode 100644 app/Rules/MACEncrypted.php create mode 100644 app/Rules/UrlEncrypted.php diff --git a/app/Models/CustomFieldset.php b/app/Models/CustomFieldset.php index 8a8bbd10e9..91b1d0cf71 100644 --- a/app/Models/CustomFieldset.php +++ b/app/Models/CustomFieldset.php @@ -3,8 +3,10 @@ namespace App\Models; use App\Rules\AlphaEncrypted; +use App\Rules\DateEncrypted; use App\Rules\EmailEncrypted; use App\Rules\NumericEncrypted; +use App\Rules\UrlEncrypted; use Gate; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; @@ -100,7 +102,7 @@ class CustomFieldset extends Model * @since [v3.0] * @return array */ - public function validation_rules() + public function validation_rules(): array { $rules = []; foreach ($this->fields as $field) { @@ -140,6 +142,16 @@ class CustomFieldset extends Model $rules[$field->db_column_name()][$emailKey] = new EmailEncrypted; } + if ($field->format === 'DATE' && $field->field_encrypted) { + $dateKey = array_search('date', $rules[$field->db_column_name()]); + $rules[$field->db_column_name()][$dateKey] = new DateEncrypted; + } + + if ($field->format === 'URL' && $field->field_encrypted) { + $urlKey = array_search('url', $rules[$field->db_column_name()]); + $rules[$field->db_column_name()][$urlKey] = new UrlEncrypted; + } + // add not_array to rules for all fields but checkboxes if ($field->element != 'checkbox') { $rules[$field->db_column_name()][] = 'not_array'; diff --git a/app/Rules/AlphaEncrypted.php b/app/Rules/AlphaEncrypted.php index 12f3c77da3..e13f677946 100644 --- a/app/Rules/AlphaEncrypted.php +++ b/app/Rules/AlphaEncrypted.php @@ -20,7 +20,7 @@ class AlphaEncrypted implements ValidationRule try { $attributeName = trim(preg_replace('/_+|snipeit|\d+/', ' ', $attribute)); $decrypted = Crypt::decrypt($value); - if ($this->validateAlpha($attributeName, $decrypted, 'ascii') && !is_null($decrypted)) { + if (!$this->validateAlpha($attributeName, $decrypted, 'ascii') && !is_null($decrypted)) { $fail(trans('validation.alpha', ['attribute' => $attributeName])); } } catch (\Exception $e) { diff --git a/app/Rules/DateEncrypted.php b/app/Rules/DateEncrypted.php new file mode 100644 index 0000000000..73d5cd1b3e --- /dev/null +++ b/app/Rules/DateEncrypted.php @@ -0,0 +1,32 @@ +validateDate($attributeName, $decrypted) && !is_null($decrypted)) { + $fail(trans('validation.date', ['attribute' => $attributeName])); + } + } catch (\Exception $e) { + report($e); + $fail(trans('general.something_went_wrong')); + } + } +} diff --git a/app/Rules/IPEncrypted.php b/app/Rules/IPEncrypted.php new file mode 100644 index 0000000000..2d64546308 --- /dev/null +++ b/app/Rules/IPEncrypted.php @@ -0,0 +1,32 @@ +validateIp($attributeName, $decrypted) && !is_null($decrypted)) { + $fail(trans('validation.ip', ['attribute' => $attributeName])); + } + } catch (\Exception $e) { + report($e); + $fail(trans('general.something_went_wrong')); + } + } +} diff --git a/app/Rules/IPv4Encrypted.php b/app/Rules/IPv4Encrypted.php new file mode 100644 index 0000000000..57f36da49a --- /dev/null +++ b/app/Rules/IPv4Encrypted.php @@ -0,0 +1,32 @@ +validateIpv4($attributeName, $decrypted) && !is_null($decrypted)) { + $fail(trans('validation.ipv4', ['attribute' => $attributeName])); + } + } catch (\Exception $e) { + report($e); + $fail(trans('general.something_went_wrong')); + } + } +} diff --git a/app/Rules/IPv6Encrypted.php b/app/Rules/IPv6Encrypted.php new file mode 100644 index 0000000000..6bb44fb2d9 --- /dev/null +++ b/app/Rules/IPv6Encrypted.php @@ -0,0 +1,32 @@ +validateIpv6($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.php new file mode 100644 index 0000000000..4f6d8a2f4d --- /dev/null +++ b/app/Rules/MACEncrypted.php @@ -0,0 +1,32 @@ +validateMacAddress($attributeName, $decrypted, 'ascii') && !is_null($decrypted)) { + $fail(trans('validation.mac_address', ['attribute' => $attributeName])); + } + } catch (\Exception $e) { + report($e); + $fail(trans('general.something_went_wrong')); + } + } +} diff --git a/app/Rules/UrlEncrypted.php b/app/Rules/UrlEncrypted.php new file mode 100644 index 0000000000..61c5f51c90 --- /dev/null +++ b/app/Rules/UrlEncrypted.php @@ -0,0 +1,32 @@ +validateUrl($attributeName, $decrypted, []) && !is_null($decrypted)) { + $fail(trans('validation.url', ['attribute' => $attributeName])); + } + } catch (\Exception $e) { + report($e); + $fail(trans('general.something_went_wrong')); + } + } +}