A free open source IT asset/license management system
use laravel validation methods, email works
spencerrlonggb8239e8ed9cf
diff --git a/app/Models/CustomFieldset.php b/app/Models/CustomFieldset.phpindex 7c9ad7dd81..8a8bbd10e9 100644--- a/app/Models/CustomFieldset.php+++ b/app/Models/CustomFieldset.php@@ -3,6 +3,7 @@namespace App\Models;use App\Rules\AlphaEncrypted;+use App\Rules\EmailEncrypted;use App\Rules\NumericEncrypted;use Gate;use Illuminate\Database\Eloquent\Factories\HasFactory;@@ -134,6 +135,11 @@ class CustomFieldset extends Model$rules[$field->db_column_name()][$alphaKey] = new AlphaEncrypted;}+ if ($field->format === 'EMAIL' && $field->field_encrypted) {+ $emailKey = array_search('email', $rules[$field->db_column_name()]);+ $rules[$field->db_column_name()][$emailKey] = new EmailEncrypted;+ }+// add not_array to rules for all fields but checkboxesif ($field->element != 'checkbox') {$rules[$field->db_column_name()][] = 'not_array';diff --git a/app/Rules/AlphaEncrypted.php b/app/Rules/AlphaEncrypted.phpindex f4ed1d6c32..12f3c77da3 100644--- a/app/Rules/AlphaEncrypted.php+++ b/app/Rules/AlphaEncrypted.php@@ -5,9 +5,11 @@ namespace App\Rules;use Closure;use Illuminate\Contracts\Validation\ValidationRule;use Illuminate\Support\Facades\Crypt;+use Illuminate\Validation\Concerns\ValidatesAttributes;class AlphaEncrypted implements ValidationRule{+ use ValidatesAttributes;/*** Run the validation rule.*@@ -18,7 +20,7 @@ class AlphaEncrypted implements ValidationRuletry {$attributeName = trim(preg_replace('/_+|snipeit|\d+/', ' ', $attribute));$decrypted = Crypt::decrypt($value);- if (!ctype_alpha($decrypted) && !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/EmailEncrypted.php b/app/Rules/EmailEncrypted.phpnew file mode 100644index 0000000000..cd0e4ad289--- /dev/null+++ b/app/Rules/EmailEncrypted.php@@ -0,0 +1,32 @@+<?php++namespace App\Rules;++use Closure;+use Illuminate\Contracts\Validation\ValidationRule;+use Illuminate\Support\Facades\Crypt;+use Illuminate\Validation\Concerns\ValidatesAttributes;++class EmailEncrypted 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->validateEmail($attribute, $decrypted, []) && !is_null($decrypted)) {+ $fail(trans('validation.email', ['attribute' => $attributeName]));+ }+ } catch (\Exception $e) {+ report($e);+ $fail(trans('general.something_went_wrong'));+ }+ }+}diff --git a/app/Rules/NumericEncrypted.php b/app/Rules/NumericEncrypted.phpindex f3cb3ba76e..3476293d87 100644--- a/app/Rules/NumericEncrypted.php+++ b/app/Rules/NumericEncrypted.php@@ -3,12 +3,17 @@namespace App\Rules;use Closure;+use Egulias\EmailValidator\Validation\RFCValidation;use Illuminate\Contracts\Encryption\DecryptException;use Illuminate\Contracts\Validation\ValidationRule;use Illuminate\Support\Facades\Crypt;+use Illuminate\Validation\Concerns\ValidatesAttributes;class NumericEncrypted implements ValidationRule{+ use ValidatesAttributes;++ //$this->validateEmail($attribute, $decrypted);/*** Run the validation rule.*@@ -16,13 +21,15 @@ class NumericEncrypted 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 (!is_numeric($decrypted) && !is_null($decrypted)) {+ if (!$this->validateNumeric($attributeName, $decrypted) && !is_null($decrypted)) {$fail(trans('validation.numeric', ['attribute' => $attributeName]));}+ //if (!is_numeric($decrypted) && !is_null($decrypted)) {+ // $fail(trans('validation.numeric', ['attribute' => $attributeName]));+ //}} catch (\Exception $e) {report($e->getMessage());$fail(trans('general.something_went_wrong'));diff --git a/tests/Feature/Assets/Ui/BulkEditAssetsTest.php b/tests/Feature/Assets/Ui/BulkEditAssetsTest.phpindex 58f7a11598..36bb9346b0 100644--- a/tests/Feature/Assets/Ui/BulkEditAssetsTest.php+++ b/tests/Feature/Assets/Ui/BulkEditAssetsTest.php@@ -29,7 +29,7 @@ class BulkEditAssetsTest extends TestCase])->assertStatus(200);}- public function testStandardUserCannotAccessPage()+ public function test_standard_user_cannot_access_page(){$user = User::factory()->create();$assets = Asset::factory()->count(2)->create();@@ -44,7 +44,7 @@ class BulkEditAssetsTest extends TestCase])->assertStatus(403);}- public function testBulkEditAssetsAcceptsAllPossibleAttributes()+ public function test_bulk_edit_assets_accepts_all_possible_attributes(){// sets up all needed models and attributes on the assets// this test does not deal with custom fields - will be dealt with in separate cases@@ -112,7 +112,7 @@ class BulkEditAssetsTest extends TestCase});}- public function testBulkEditAssetsNullsOutFieldsIfSelected()+ public function test_bulk_edit_assets_nulls_out_fields_if_selected(){// sets up all needed models and attributes on the assets// this test does not deal with custom fields - will be dealt with in separate cases@@ -165,7 +165,7 @@ class BulkEditAssetsTest extends TestCase});}- public function testBulkEditAssetsAcceptsAndUpdatesUnencryptedCustomFields()+ public function test_bulk_edit_assets_accepts_and_updates_unencrypted_custom_fields(){$this->markIncompleteIfMySQL('Custom Fields tests do not work on MySQL');@@ -197,7 +197,7 @@ class BulkEditAssetsTest extends TestCase});}- public function testBulkEditAssetsNullsCustomFieldsIfSelected()+ public function test_bulk_edit_assets_nulls_custom_fields_if_selected(){$this->markIncompleteIfMySQL('Custom Fields tests do not work on MySQL');@@ -238,7 +238,7 @@ class BulkEditAssetsTest extends TestCase});}- public function testBulkEditAssetsAcceptsAndUpdatesEncryptedCustomFields()+ public function test_bulk_edit_assets_accepts_and_updates_encrypted_custom_fields(){$this->markIncompleteIfMySQL('Custom Fields tests do not work on MySQL');@@ -262,7 +262,7 @@ class BulkEditAssetsTest extends TestCase});}- public function testBulkEditAssetsRequiresadminToUpdateEncryptedCustomFields()+ public function test_bulk_edit_assets_requires_admin_to_update_encrypted_custom_fields(){$this->markIncompleteIfMySQL('Custom Fields tests do not work on mysql');$edit_user = User::factory()->editAssets()->create();