Skip to content

A free open source IT asset/license management system

more encryption rules extenting laravel's own

spencerrlonggf9b05bc8deb4
diff --git a/app/Models/CustomFieldset.php b/app/Models/CustomFieldset.php
index 8a8bbd10e9..91b1d0cf71 100644
--- a/app/Models/CustomFieldset.php
+++ b/app/Models/CustomFieldset.php
@@ -3,8 +3,10 @@
namespace App\Models;
use App\Rules\AlphaEncrypted;
+use App\Rules\DateEncrypted;
use App\Rules\EmailEncrypted;
use App\Rules\NumericEncrypted;
+use App\Rules\UrlEncrypted;
use Gate;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
@@ -100,7 +102,7 @@ class CustomFieldset extends Model
* @since [v3.0]
* @return array
*/
- public function validation_rules()
+ public function validation_rules(): array
{
$rules = [];
foreach ($this->fields as $field) {
@@ -140,6 +142,16 @@ class CustomFieldset extends Model
$rules[$field->db_column_name()][$emailKey] = new EmailEncrypted;
}
+ if ($field->format === 'DATE' && $field->field_encrypted) {
+ $dateKey = array_search('date', $rules[$field->db_column_name()]);
+ $rules[$field->db_column_name()][$dateKey] = new DateEncrypted;
+ }
+
+ if ($field->format === 'URL' && $field->field_encrypted) {
+ $urlKey = array_search('url', $rules[$field->db_column_name()]);
+ $rules[$field->db_column_name()][$urlKey] = new UrlEncrypted;
+ }
+
// add not_array to rules for all fields but checkboxes
if ($field->element != 'checkbox') {
$rules[$field->db_column_name()][] = 'not_array';
diff --git a/app/Rules/AlphaEncrypted.php b/app/Rules/AlphaEncrypted.php
index 12f3c77da3..e13f677946 100644
--- a/app/Rules/AlphaEncrypted.php
+++ b/app/Rules/AlphaEncrypted.php
@@ -20,7 +20,7 @@ class AlphaEncrypted implements ValidationRule
try {
$attributeName = trim(preg_replace('/_+|snipeit|\d+/', ' ', $attribute));
$decrypted = Crypt::decrypt($value);
- if ($this->validateAlpha($attributeName, $decrypted, 'ascii') && !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/DateEncrypted.php b/app/Rules/DateEncrypted.php
new file mode 100644
index 0000000000..73d5cd1b3e
--- /dev/null
+++ b/app/Rules/DateEncrypted.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 DateEncrypted implements ValidationRule
+{
+ use ValidatesAttributes;
+
+ /**
+ * Run the validation rule.
+ *
+ * @param \Closure(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->validateDate($attributeName, $decrypted) && !is_null($decrypted)) {
+ $fail(trans('validation.date', ['attribute' => $attributeName]));
+ }
+ } catch (\Exception $e) {
+ report($e);
+ $fail(trans('general.something_went_wrong'));
+ }
+ }
+}
diff --git a/app/Rules/IPEncrypted.php b/app/Rules/IPEncrypted.php
new file mode 100644
index 0000000000..2d64546308
--- /dev/null
+++ b/app/Rules/IPEncrypted.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 IPEncrypted implements ValidationRule
+{
+ use ValidatesAttributes;
+
+ /**
+ * Run the validation rule.
+ *
+ * @param \Closure(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->validateIp($attributeName, $decrypted) && !is_null($decrypted)) {
+ $fail(trans('validation.ip', ['attribute' => $attributeName]));
+ }
+ } catch (\Exception $e) {
+ report($e);
+ $fail(trans('general.something_went_wrong'));
+ }
+ }
+}
diff --git a/app/Rules/IPv4Encrypted.php b/app/Rules/IPv4Encrypted.php
new file mode 100644
index 0000000000..57f36da49a
--- /dev/null
+++ b/app/Rules/IPv4Encrypted.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 IPv4Encrypted implements ValidationRule
+{
+ use ValidatesAttributes;
+
+ /**
+ * Run the validation rule.
+ *
+ * @param \Closure(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->validateIpv4($attributeName, $decrypted) && !is_null($decrypted)) {
+ $fail(trans('validation.ipv4', ['attribute' => $attributeName]));
+ }
+ } catch (\Exception $e) {
+ report($e);
+ $fail(trans('general.something_went_wrong'));
+ }
+ }
+}
diff --git a/app/Rules/IPv6Encrypted.php b/app/Rules/IPv6Encrypted.php
new file mode 100644
index 0000000000..6bb44fb2d9
--- /dev/null
+++ b/app/Rules/IPv6Encrypted.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 IPv6Encrypted implements ValidationRule
+{
+ use ValidatesAttributes;
+
+ /**
+ * Run the validation rule.
+ *
+ * @param \Closure(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->validateIpv6($attributeName, $decrypted) && !is_null($decrypted)) {
+ $fail(trans('validation.ipv6', ['attribute' => $attributeName]));
+ }
+ } catch (\Exception $e) {
+ report($e);
+ $fail(trans('general.something_went_wrong'));
+ }
+ }
+}
diff --git a/app/Rules/MACEncrypted.php b/app/Rules/MACEncrypted.php
new file mode 100644
index 0000000000..4f6d8a2f4d
--- /dev/null
+++ b/app/Rules/MACEncrypted.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 MACEncrypted implements ValidationRule
+{
+ use ValidatesAttributes;
+
+ /**
+ * Run the validation rule.
+ *
+ * @param \Closure(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->validateMacAddress($attributeName, $decrypted, 'ascii') && !is_null($decrypted)) {
+ $fail(trans('validation.mac_address', ['attribute' => $attributeName]));
+ }
+ } catch (\Exception $e) {
+ report($e);
+ $fail(trans('general.something_went_wrong'));
+ }
+ }
+}
diff --git a/app/Rules/UrlEncrypted.php b/app/Rules/UrlEncrypted.php
new file mode 100644
index 0000000000..61c5f51c90
--- /dev/null
+++ b/app/Rules/UrlEncrypted.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 UrlEncrypted implements ValidationRule
+{
+ use ValidatesAttributes;
+
+ /**
+ * Run the validation rule.
+ *
+ * @param \Closure(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->validateUrl($attributeName, $decrypted, []) && !is_null($decrypted)) {
+ $fail(trans('validation.url', ['attribute' => $attributeName]));
+ }
+ } catch (\Exception $e) {
+ report($e);
+ $fail(trans('general.something_went_wrong'));
+ }
+ }
+}