Skip to content

Commit b7aedb7

Browse files
authored
Merge pull request #15818 from spencerrlongg/bug/sc-27213
Resolves Validation for Encrypted Numeric and Alpha Custom Fields
2 parents 8b38dc8 + 22602c7 commit b7aedb7

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

app/Models/CustomFieldset.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace App\Models;
44

5+
use App\Rules\AlphaEncrypted;
6+
use App\Rules\NumericEncrypted;
57
use Gate;
68
use Illuminate\Database\Eloquent\Factories\HasFactory;
79
use Illuminate\Database\Eloquent\Model;
@@ -95,6 +97,19 @@ public function validation_rules()
9597
array_push($rule, $field->attributes['format']);
9698
$rules[$field->db_column_name()] = $rule;
9799

100+
101+
// these are to replace the standard 'numeric' and 'alpha' rules if the custom field is also encrypted.
102+
// the values need to be decrypted first, because encrypted strings are alphanumeric
103+
if ($field->format === 'NUMERIC' && $field->field_encrypted) {
104+
$numericKey = array_search('numeric', $rules[$field->db_column_name()]);
105+
$rules[$field->db_column_name()][$numericKey] = new NumericEncrypted;
106+
}
107+
108+
if ($field->format === 'ALPHA' && $field->field_encrypted) {
109+
$alphaKey = array_search('alpha', $rules[$field->db_column_name()]);
110+
$rules[$field->db_column_name()][$alphaKey] = new AlphaEncrypted;
111+
}
112+
98113
// add not_array to rules for all fields but checkboxes
99114
if ($field->element != 'checkbox') {
100115
$rules[$field->db_column_name()][] = 'not_array';

app/Rules/AlphaEncrypted.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace App\Rules;
4+
5+
use Closure;
6+
use Illuminate\Contracts\Validation\ValidationRule;
7+
use Illuminate\Support\Facades\Crypt;
8+
9+
class AlphaEncrypted implements ValidationRule
10+
{
11+
/**
12+
* Run the validation rule.
13+
*
14+
* @param \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail
15+
*/
16+
public function validate(string $attribute, mixed $value, Closure $fail): void
17+
{
18+
try {
19+
$attributeName = trim(preg_replace('/_+|snipeit|\d+/', ' ', $attribute));
20+
$decrypted = Crypt::decrypt($value);
21+
if (!ctype_alpha($decrypted) && !is_null($decrypted)) {
22+
$fail(trans('validation.alpha', ['attribute' => $attributeName]));
23+
}
24+
} catch (\Exception $e) {
25+
report($e);
26+
$fail(trans('general.something_went_wrong'));
27+
}
28+
}
29+
}

app/Rules/NumericEncrypted.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace App\Rules;
4+
5+
use Closure;
6+
use Illuminate\Contracts\Encryption\DecryptException;
7+
use Illuminate\Contracts\Validation\ValidationRule;
8+
use Illuminate\Support\Facades\Crypt;
9+
10+
class NumericEncrypted implements ValidationRule
11+
{
12+
/**
13+
* Run the validation rule.
14+
*
15+
* @param \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail
16+
*/
17+
public function validate(string $attribute, mixed $value, Closure $fail): void
18+
{
19+
20+
try {
21+
$attributeName = trim(preg_replace('/_+|snipeit|\d+/', ' ', $attribute));
22+
$decrypted = Crypt::decrypt($value);
23+
if (!is_numeric($decrypted) && !is_null($decrypted)) {
24+
$fail(trans('validation.numeric', ['attribute' => $attributeName]));
25+
}
26+
} catch (\Exception $e) {
27+
report($e->getMessage());
28+
$fail(trans('general.something_went_wrong'));
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)