this should work in theory - local is screwy though

This commit is contained in:
spencerrlongg 2024-11-13 21:38:09 -06:00
parent 4841b89109
commit 3982201d0e
4 changed files with 82 additions and 12 deletions

View file

@ -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

View file

@ -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;
}
}

View file

@ -0,0 +1,28 @@
<?php
namespace App\Rules;
use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Support\Facades\Crypt;
class AlphaEncrypted implements ValidationRule
{
/**
* Run the validation rule.
*
* @param \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail
*/
public function validate(string $attribute, mixed $value, Closure $fail): void
{
try {
$decrypted = Crypt::decrypt($value);
dump($decrypted);
if (!ctype_alpha($decrypted)) {
$fail($attribute.' is not numeric.');
}
} catch (\Exception $e) {
$fail($e->getMessage());
}
}
}

View file

@ -0,0 +1,28 @@
<?php
namespace App\Rules;
use Closure;
use Illuminate\Contracts\Encryption\DecryptException;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Support\Facades\Crypt;
class NumericEncrypted implements ValidationRule
{
/**
* Run the validation rule.
*
* @param \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail
*/
public function validate(string $attribute, mixed $value, Closure $fail): void
{
try {
$value = Crypt::decrypt($value);
if (!is_numeric($value)) {
$fail($attribute.' is not numeric.');
}
} catch (\Exception $e) {
$fail($e->getMessage());
}
}
}