Skip to content

A free open source IT asset/license management system

this should work in theory - local is screwy though

spencerrlongg3982201d0e96
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 @@
+<?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());
+ }
+ }
+}
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 @@
+<?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());
+ }
+ }
+}