From 4841b891091c9d1e0b19daf03091e9ea1ac4a749 Mon Sep 17 00:00:00 2001 From: spencerrlongg Date: Wed, 13 Nov 2024 12:45:39 -0600 Subject: [PATCH 1/9] scratch code for this issue --- app/Models/Asset.php | 9 +++++++++ app/Models/CustomFieldset.php | 4 ++++ 2 files changed, 13 insertions(+) diff --git a/app/Models/Asset.php b/app/Models/Asset.php index ce8b870eb2..ecf1b13321 100644 --- a/app/Models/Asset.php +++ b/app/Models/Asset.php @@ -213,6 +213,15 @@ class Asset extends Depreciable $this->attributes['expected_checkin'] = $value; } + public function withValidator($validator) + { + foreach ($this->customFields as $field) { + if ($field->isEncrypted()) { + Crypt::decrypt($this->value); + } + } + } + /** * This handles the custom field validation for assets * diff --git a/app/Models/CustomFieldset.php b/app/Models/CustomFieldset.php index 71be28e8a3..c286005bf4 100644 --- a/app/Models/CustomFieldset.php +++ b/app/Models/CustomFieldset.php @@ -92,6 +92,10 @@ class CustomFieldset extends Model $rule[] = 'unique_undeleted'; } + if ($field->hasFormat() && $field->isEncrypted()) { + $rule[] = $rule.'-encrypted'; + } + array_push($rule, $field->attributes['format']); $rules[$field->db_column_name()] = $rule; From 3982201d0e96576d1c262b75258eee49a8b46b92 Mon Sep 17 00:00:00 2001 From: spencerrlongg Date: Wed, 13 Nov 2024 21:38:09 -0600 Subject: [PATCH 2/9] this should work in theory - local is screwy though --- app/Models/Asset.php | 19 +++++++++++-------- app/Models/CustomFieldset.php | 19 +++++++++++++++---- app/Rules/AlphaEncrypted.php | 28 ++++++++++++++++++++++++++++ app/Rules/NumericEncrypted.php | 28 ++++++++++++++++++++++++++++ 4 files changed, 82 insertions(+), 12 deletions(-) create mode 100644 app/Rules/AlphaEncrypted.php create mode 100644 app/Rules/NumericEncrypted.php diff --git a/app/Models/Asset.php b/app/Models/Asset.php index ecf1b13321..e0c0f45ef9 100644 --- a/app/Models/Asset.php +++ b/app/Models/Asset.php @@ -12,11 +12,13 @@ use App\Presenters\Presentable; use App\Presenters\AssetPresenter; use Illuminate\Support\Facades\Auth; use Carbon\Carbon; +use Illuminate\Support\Facades\Crypt; use Illuminate\Support\Facades\DB; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Support\Facades\Storage; +use Illuminate\Support\Facades\Validator; use Watson\Validating\ValidatingTrait; use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Model; @@ -213,14 +215,15 @@ class Asset extends Depreciable $this->attributes['expected_checkin'] = $value; } - public function withValidator($validator) - { - foreach ($this->customFields as $field) { - if ($field->isEncrypted()) { - Crypt::decrypt($this->value); - } - } - } + // i don't think this will work the way we'd need it to + //public function withValidator(Validator $validator) + //{ + // foreach ($this->customFields as $field) { + // if ($field->field_encrypted) { + // return Crypt::decrypt($this->value); + // } + // } + //} /** * This handles the custom field validation for assets diff --git a/app/Models/CustomFieldset.php b/app/Models/CustomFieldset.php index c286005bf4..1059c5d5ff 100644 --- a/app/Models/CustomFieldset.php +++ b/app/Models/CustomFieldset.php @@ -2,6 +2,8 @@ namespace App\Models; +use App\Rules\AlphaEncrypted; +use App\Rules\NumericEncrypted; use Gate; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; @@ -92,13 +94,20 @@ class CustomFieldset extends Model $rule[] = 'unique_undeleted'; } - if ($field->hasFormat() && $field->isEncrypted()) { - $rule[] = $rule.'-encrypted'; - } - array_push($rule, $field->attributes['format']); $rules[$field->db_column_name()] = $rule; + + if ($field->format === 'NUMERIC' && $field->field_encrypted) { + $numericKey = array_search('numeric', $rules[$field->db_column_name()]); + $rules[$field->db_column_name()][$numericKey] = new NumericEncrypted; + } + + if ($field->format === 'ALPHA' && $field->field_encrypted) { + $alphaKey = array_search('alpha', $rules[$field->db_column_name()]); + $rules[$field->db_column_name()][$alphaKey] = new AlphaEncrypted; + } + // add not_array to rules for all fields but checkboxes if ($field->element != 'checkbox') { $rules[$field->db_column_name()][] = 'not_array'; @@ -113,6 +122,8 @@ class CustomFieldset extends Model } } + dump($rules); + return $rules; } } diff --git a/app/Rules/AlphaEncrypted.php b/app/Rules/AlphaEncrypted.php new file mode 100644 index 0000000000..246d92f0b7 --- /dev/null +++ b/app/Rules/AlphaEncrypted.php @@ -0,0 +1,28 @@ +getMessage()); + } + } +} diff --git a/app/Rules/NumericEncrypted.php b/app/Rules/NumericEncrypted.php new file mode 100644 index 0000000000..f36c4174ed --- /dev/null +++ b/app/Rules/NumericEncrypted.php @@ -0,0 +1,28 @@ +getMessage()); + } + } +} From 25163d1756f31d77c42fb0961c506ee0e6c59562 Mon Sep 17 00:00:00 2001 From: spencerrlongg Date: Wed, 13 Nov 2024 21:46:23 -0600 Subject: [PATCH 3/9] =?UTF-8?q?working=20except=20for=20null=20?= =?UTF-8?q?=F0=9F=A4=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Rules/AlphaEncrypted.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Rules/AlphaEncrypted.php b/app/Rules/AlphaEncrypted.php index 246d92f0b7..5e0ffcbd52 100644 --- a/app/Rules/AlphaEncrypted.php +++ b/app/Rules/AlphaEncrypted.php @@ -19,7 +19,7 @@ class AlphaEncrypted implements ValidationRule $decrypted = Crypt::decrypt($value); dump($decrypted); if (!ctype_alpha($decrypted)) { - $fail($attribute.' is not numeric.'); + $fail($attribute.' is not alphabetic.'); } } catch (\Exception $e) { $fail($e->getMessage()); From 7e7cbc4cc8024c7a54304d69222e4a7be308ef0a Mon Sep 17 00:00:00 2001 From: spencerrlongg Date: Wed, 13 Nov 2024 21:54:25 -0600 Subject: [PATCH 4/9] seems to work just fine now, needs translations --- app/Rules/AlphaEncrypted.php | 6 +++--- app/Rules/NumericEncrypted.php | 8 +++++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/app/Rules/AlphaEncrypted.php b/app/Rules/AlphaEncrypted.php index 5e0ffcbd52..048c663717 100644 --- a/app/Rules/AlphaEncrypted.php +++ b/app/Rules/AlphaEncrypted.php @@ -17,12 +17,12 @@ class AlphaEncrypted implements ValidationRule { try { $decrypted = Crypt::decrypt($value); - dump($decrypted); - if (!ctype_alpha($decrypted)) { + if (!ctype_alpha($decrypted) && !is_null($decrypted)) { $fail($attribute.' is not alphabetic.'); } } catch (\Exception $e) { - $fail($e->getMessage()); + report($e); + $fail('something went wrong.'); } } } diff --git a/app/Rules/NumericEncrypted.php b/app/Rules/NumericEncrypted.php index f36c4174ed..bef4aad5ed 100644 --- a/app/Rules/NumericEncrypted.php +++ b/app/Rules/NumericEncrypted.php @@ -16,13 +16,15 @@ class NumericEncrypted implements ValidationRule */ public function validate(string $attribute, mixed $value, Closure $fail): void { + try { - $value = Crypt::decrypt($value); - if (!is_numeric($value)) { + $decrypted = Crypt::decrypt($value); + if (!is_numeric($decrypted) && !is_null($decrypted)) { $fail($attribute.' is not numeric.'); } } catch (\Exception $e) { - $fail($e->getMessage()); + report($e->getMessage()); + $fail('something went wrong'); } } } From 8c6869fd3466a4be722f108cc5f80fafe87d0404 Mon Sep 17 00:00:00 2001 From: spencerrlongg Date: Wed, 13 Nov 2024 21:59:32 -0600 Subject: [PATCH 5/9] note and rm dump and comment --- app/Models/Asset.php | 10 ---------- app/Models/CustomFieldset.php | 4 ++-- 2 files changed, 2 insertions(+), 12 deletions(-) diff --git a/app/Models/Asset.php b/app/Models/Asset.php index e0c0f45ef9..aa6a96d41a 100644 --- a/app/Models/Asset.php +++ b/app/Models/Asset.php @@ -215,16 +215,6 @@ class Asset extends Depreciable $this->attributes['expected_checkin'] = $value; } - // i don't think this will work the way we'd need it to - //public function withValidator(Validator $validator) - //{ - // foreach ($this->customFields as $field) { - // if ($field->field_encrypted) { - // return Crypt::decrypt($this->value); - // } - // } - //} - /** * This handles the custom field validation for assets * diff --git a/app/Models/CustomFieldset.php b/app/Models/CustomFieldset.php index 1059c5d5ff..d6bd7a1bef 100644 --- a/app/Models/CustomFieldset.php +++ b/app/Models/CustomFieldset.php @@ -98,6 +98,8 @@ class CustomFieldset extends Model $rules[$field->db_column_name()] = $rule; + // these are to replace the standard 'numeric' and 'alpha' rules if the custom field is also encrypted. + // the values need to be decrypted first, because encrypted strings are alphanumeric if ($field->format === 'NUMERIC' && $field->field_encrypted) { $numericKey = array_search('numeric', $rules[$field->db_column_name()]); $rules[$field->db_column_name()][$numericKey] = new NumericEncrypted; @@ -122,8 +124,6 @@ class CustomFieldset extends Model } } - dump($rules); - return $rules; } } From 95cd7793343a65b08b1a6ef7b1d1d02df6fe7b43 Mon Sep 17 00:00:00 2001 From: spencerrlongg Date: Thu, 14 Nov 2024 11:42:35 -0600 Subject: [PATCH 6/9] translations and attribute names --- app/Rules/AlphaEncrypted.php | 3 ++- app/Rules/NumericEncrypted.php | 3 ++- resources/lang/en-US/validation.php | 2 ++ 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/app/Rules/AlphaEncrypted.php b/app/Rules/AlphaEncrypted.php index 048c663717..102e03dd6b 100644 --- a/app/Rules/AlphaEncrypted.php +++ b/app/Rules/AlphaEncrypted.php @@ -16,9 +16,10 @@ class AlphaEncrypted implements ValidationRule public function validate(string $attribute, mixed $value, Closure $fail): void { try { + $attributeName = trim(preg_replace('/_+|snipeit|\d+/', ' ', $attribute)); $decrypted = Crypt::decrypt($value); if (!ctype_alpha($decrypted) && !is_null($decrypted)) { - $fail($attribute.' is not alphabetic.'); + $fail(trans('validation.custom.alpha_encrypted', ['attribute' => $attributeName])); } } catch (\Exception $e) { report($e); diff --git a/app/Rules/NumericEncrypted.php b/app/Rules/NumericEncrypted.php index bef4aad5ed..c4bf865f3b 100644 --- a/app/Rules/NumericEncrypted.php +++ b/app/Rules/NumericEncrypted.php @@ -18,9 +18,10 @@ class NumericEncrypted implements ValidationRule { try { + $attributeName = trim(preg_replace('/_+|snipeit|\d+/', ' ', $attribute)); $decrypted = Crypt::decrypt($value); if (!is_numeric($decrypted) && !is_null($decrypted)) { - $fail($attribute.' is not numeric.'); + $fail(trans('validation.custom.numeric_encrypted', ['attribute' => $attributeName])); } } catch (\Exception $e) { report($e->getMessage()); diff --git a/resources/lang/en-US/validation.php b/resources/lang/en-US/validation.php index 7d7840eb42..d054c8b439 100644 --- a/resources/lang/en-US/validation.php +++ b/resources/lang/en-US/validation.php @@ -187,6 +187,8 @@ return [ 'custom' => [ 'alpha_space' => 'The :attribute field contains a character that is not allowed.', + 'alpha_encrypted' => 'The :attribute field should be alphabetic characters.', + 'numeric_encrypted' => 'The :attribute field should be numeric characters.', 'email_array' => 'One or more email addresses is invalid.', 'hashed_pass' => 'Your current password is incorrect', 'dumbpwd' => 'That password is too common.', From 846580653664ff2b0b046abc8fc959bb44ce0ed3 Mon Sep 17 00:00:00 2001 From: spencerrlongg Date: Thu, 14 Nov 2024 11:51:41 -0600 Subject: [PATCH 7/9] rm unnecessary imports --- app/Models/Asset.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/app/Models/Asset.php b/app/Models/Asset.php index aa6a96d41a..ce8b870eb2 100644 --- a/app/Models/Asset.php +++ b/app/Models/Asset.php @@ -12,13 +12,11 @@ use App\Presenters\Presentable; use App\Presenters\AssetPresenter; use Illuminate\Support\Facades\Auth; use Carbon\Carbon; -use Illuminate\Support\Facades\Crypt; use Illuminate\Support\Facades\DB; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Support\Facades\Storage; -use Illuminate\Support\Facades\Validator; use Watson\Validating\ValidatingTrait; use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Model; From 124f9c84c203ef30bcbee73665de5ae0bdfea804 Mon Sep 17 00:00:00 2001 From: spencerrlongg Date: Thu, 14 Nov 2024 13:45:47 -0600 Subject: [PATCH 8/9] oops, something went wrong translation --- app/Rules/AlphaEncrypted.php | 2 +- app/Rules/NumericEncrypted.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/Rules/AlphaEncrypted.php b/app/Rules/AlphaEncrypted.php index 102e03dd6b..b39a5fedba 100644 --- a/app/Rules/AlphaEncrypted.php +++ b/app/Rules/AlphaEncrypted.php @@ -23,7 +23,7 @@ class AlphaEncrypted implements ValidationRule } } catch (\Exception $e) { report($e); - $fail('something went wrong.'); + $fail(trans('general.something_went_wrong')); } } } diff --git a/app/Rules/NumericEncrypted.php b/app/Rules/NumericEncrypted.php index c4bf865f3b..4121ce5178 100644 --- a/app/Rules/NumericEncrypted.php +++ b/app/Rules/NumericEncrypted.php @@ -25,7 +25,7 @@ class NumericEncrypted implements ValidationRule } } catch (\Exception $e) { report($e->getMessage()); - $fail('something went wrong'); + $fail(trans('general.something_went_wrong')); } } } From 22602c7997a98764ebb8b6473adc95d22d5d3569 Mon Sep 17 00:00:00 2001 From: spencerrlongg Date: Tue, 19 Nov 2024 08:59:47 -0600 Subject: [PATCH 9/9] use existing validation strings --- app/Rules/AlphaEncrypted.php | 2 +- app/Rules/NumericEncrypted.php | 2 +- resources/lang/en-US/validation.php | 2 -- 3 files changed, 2 insertions(+), 4 deletions(-) diff --git a/app/Rules/AlphaEncrypted.php b/app/Rules/AlphaEncrypted.php index b39a5fedba..f4ed1d6c32 100644 --- a/app/Rules/AlphaEncrypted.php +++ b/app/Rules/AlphaEncrypted.php @@ -19,7 +19,7 @@ class AlphaEncrypted implements ValidationRule $attributeName = trim(preg_replace('/_+|snipeit|\d+/', ' ', $attribute)); $decrypted = Crypt::decrypt($value); if (!ctype_alpha($decrypted) && !is_null($decrypted)) { - $fail(trans('validation.custom.alpha_encrypted', ['attribute' => $attributeName])); + $fail(trans('validation.alpha', ['attribute' => $attributeName])); } } catch (\Exception $e) { report($e); diff --git a/app/Rules/NumericEncrypted.php b/app/Rules/NumericEncrypted.php index 4121ce5178..f3cb3ba76e 100644 --- a/app/Rules/NumericEncrypted.php +++ b/app/Rules/NumericEncrypted.php @@ -21,7 +21,7 @@ class NumericEncrypted implements ValidationRule $attributeName = trim(preg_replace('/_+|snipeit|\d+/', ' ', $attribute)); $decrypted = Crypt::decrypt($value); if (!is_numeric($decrypted) && !is_null($decrypted)) { - $fail(trans('validation.custom.numeric_encrypted', ['attribute' => $attributeName])); + $fail(trans('validation.numeric', ['attribute' => $attributeName])); } } catch (\Exception $e) { report($e->getMessage()); diff --git a/resources/lang/en-US/validation.php b/resources/lang/en-US/validation.php index d054c8b439..7d7840eb42 100644 --- a/resources/lang/en-US/validation.php +++ b/resources/lang/en-US/validation.php @@ -187,8 +187,6 @@ return [ 'custom' => [ 'alpha_space' => 'The :attribute field contains a character that is not allowed.', - 'alpha_encrypted' => 'The :attribute field should be alphabetic characters.', - 'numeric_encrypted' => 'The :attribute field should be numeric characters.', 'email_array' => 'One or more email addresses is invalid.', 'hashed_pass' => 'Your current password is incorrect', 'dumbpwd' => 'That password is too common.',