Add Validation for birthday, gender and document type parameters in profile update (#2992)

* Add Validation

* update test

* add migration

* Update src/library/FOSSBilling/UpdatePatcher.php

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update src/modules/Profile/ClientValidator.php

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update src/modules/Profile/ClientValidator.php

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* update

* Update src/modules/Profile/ClientValidator.php

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update `last_patch` setting value to 46

Incremented the 'last_patch' value in the settings table from 45 to 46 to reflect the latest patch version.

---------

Co-authored-by: blackholered <blackholered@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: enigmaticious <enigmaticious@users.noreply.github.com>
Co-authored-by: Adam Daley <admdly@users.noreply.github.com>
This commit is contained in:
authored and GitHub committed 2025-12-27 22:34:04 +00:00
1 parent c944fa09fd
commit 8e4e6bd07a
7 files changed
+113 -19

No files matched your search

+1 -1
View File
@@ -315,7 +315,7 @@ LOCK TABLES `setting` WRITE;
INSERT INTO `setting` (`id`, `param`, `value`, `public`, `category`, `hash`, `created_at`, `updated_at`)
VALUES
(1,'last_patch','45',0,NULL,NULL,'2024-01-04 12:00:00','2024-01-04 12:00:00'),
(1,'last_patch','46',0,NULL,NULL,'2024-01-04 12:00:00','2024-01-04 12:00:00'),
(2,'company_name','Company Name',0,NULL,NULL,'2022-12-01 12:00:00','2022-12-01 12:00:00'),
(3,'company_email','support@yourcompany.com',0,NULL,NULL,'2022-12-01 12:00:00','2022-12-01 12:00:00'),
(4,'company_signature','FOSSBilling.org - Client Management, Invoicing and Support Software',0,NULL,NULL,'2022-12-01 12:00:00','2022-12-01 12:00:00'),
+2 -2
View File
@@ -221,7 +221,7 @@ CREATE TABLE `client` (
`type` varchar(100) DEFAULT NULL,
`first_name` varchar(100) DEFAULT NULL,
`last_name` varchar(100) DEFAULT NULL,
`gender` varchar(20) DEFAULT NULL,
`gender` ENUM('male', 'female', 'nonbinary', 'other') DEFAULT NULL,
`birthday` date DEFAULT NULL,
`phone_cc` varchar(10) DEFAULT NULL,
`phone` varchar(100) DEFAULT NULL,
@@ -234,7 +234,7 @@ CREATE TABLE `client` (
`state` varchar(100) DEFAULT NULL,
`postcode` varchar(100) DEFAULT NULL,
`country` varchar(100) DEFAULT NULL,
`document_type` varchar(100) DEFAULT NULL,
`document_type` ENUM('passport') DEFAULT NULL,
`document_nr` varchar(20) DEFAULT NULL,
`notes` text,
`currency` varchar(10) DEFAULT NULL,
+12
View File
@@ -431,6 +431,18 @@ class UpdatePatcher implements InjectionAwareInterface
$q = 'ALTER TABLE `activity_system` DROP COLUMN `updated_at`;';
$this->executeSql($q);
},
46 => function (): void {
// Change gender column to ENUM type
$q1 = 'ALTER TABLE `client`
MODIFY COLUMN `gender` ENUM("male", "female", "nonbinary", "other") DEFAULT NULL;';
// Change document_type column to ENUM type
$q2 = 'ALTER TABLE `client`
MODIFY COLUMN `document_type` ENUM("passport") DEFAULT NULL;';
$this->executeSql($q1);
$this->executeSql($q2);
},
];
ksort($patches, SORT_NATURAL);
+14
View File
@@ -13,6 +13,20 @@ class Model_Client extends RedBeanPHP\SimpleModel
final public const ACTIVE = 'active';
final public const SUSPENDED = 'suspended';
final public const CANCELED = 'canceled';
final public const GENDER_MALE = 'male';
final public const GENDER_FEMALE = 'female';
final public const GENDER_NON_BINARY = 'nonbinary';
final public const GENDER_OTHER = 'other';
public const ALLOWED_GENDERS = [
self::GENDER_MALE,
self::GENDER_FEMALE,
self::GENDER_NON_BINARY,
self::GENDER_OTHER
];
final public const DOC_PASSPORT = 'passport';
public const ALLOWED_DOCUMENT_TYPES = [
self::DOC_PASSPORT,
];
public function getFullName(): string
{
+77
View File
@@ -0,0 +1,77 @@
<?php
namespace Box\Mod\Profile;
use FOSSBilling\InformationException;
use Model_Client;
class ClientValidator
{
/**
* Validate and normalize gender.
*/
public static function validateGender(?string $gender): ?string
{
if ($gender === null || trim($gender) === '') {
return null;
}
$gender = strtolower(trim($gender));
if (!in_array($gender, Model_Client::ALLOWED_GENDERS, true)) {
throw new InformationException(
'Invalid gender value. Allowed: male, female, nonbinary, other'
);
}
return $gender;
}
/**
* Validate and normalize birthday.
* Ensures it's a valid date, not older than 120 years, not in the future.
*/
public static function validateBirthday(?string $birthday): ?string
{
if ($birthday === null) {
return null;
}
if (trim($birthday) === '') {
return null;
}
if (strtotime($birthday) === false) {
throw new InformationException('Invalid birthdate value');
}
$birthdayDate = new \DateTime($birthday);
$today = new \DateTime('today');
$minDate = (new \DateTime('today'))->modify('-120 years');
if ($birthdayDate < $minDate) {
throw new InformationException('Birthdate cannot be more than 120 years ago.');
}
if ($birthdayDate > $today) {
throw new InformationException('Birthdate cannot be in the future.');
}
return $birthdayDate->format('Y-m-d');
}
/**
* Validate document type and normalize it.
* If no type is provided but a document number exists, defaults to 'passport'.
*/
public static function validateDocument(string $documentType): string
{
if (!in_array($documentType, Model_Client::ALLOWED_DOCUMENT_TYPES, true)) {
throw new InformationException(
'Invalid document type. Allowed: :list',
[':list' => implode(', ', Model_Client::ALLOWED_DOCUMENT_TYPES)]
);
}
return $documentType;
}
}
+5 -14
View File
@@ -139,19 +139,8 @@ class Service implements InjectionAwareInterface
$client->first_name = $data['first_name'] ?? $client->first_name;
$client->last_name = $data['last_name'] ?? $client->last_name;
$client->gender = $data['gender'] ?? $client->gender;
$birthday = $data['birthday'] ?? null;
// Special handling for the birthday field
if (is_string($birthday) && strlen(trim($birthday)) === 0) {
$birthday = null;
} elseif ($birthday !== null && strtotime($birthday) === false) {
throw new \FOSSBilling\InformationException('Invalid birthdate value');
}
$client->birthday = $birthday ?? $client->birthday;
$client->gender = ClientValidator::validateGender($data['gender'] ?? $client->gender);
$client->birthday = ClientValidator::validateBirthday($data['birthday'] ?? $client->birthday);
$client->company = $data['company'] ?? $client->company;
$client->company_vat = $data['company_vat'] ?? $client->company_vat;
$client->company_number = $data['company_number'] ?? $client->company_number;
@@ -168,7 +157,9 @@ class Service implements InjectionAwareInterface
$client->document_nr = $data['document_nr'] ?? $client->document_nr;
if (isset($client->document_nr)) {
$client->document_type = $data['document_type'] ?? 'passport';
$client->document_type = ClientValidator::validateDocument(
$data['document_type'] ?? \Model_Client::DOC_PASSPORT,
);
}
$client->lang = $data['lang'] ?? $client->lang;
$client->notes = $data['notes'] ?? $client->notes;
+2 -2
View File
@@ -173,7 +173,7 @@ final class ServiceTest extends \BBTestCase
'email' => 'email@example.com',
'first_name' => 'string',
'last_name' => 'string',
'gender' => 'string',
'gender' => 'other',
'birthday' => '1981-01-01',
'company' => 'string',
'company_vat' => 'string',
@@ -187,7 +187,7 @@ final class ServiceTest extends \BBTestCase
'postcode' => 'string',
'city' => 'string',
'state' => 'string',
'document_type' => 'string',
'document_type' => 'passport',
'document_nr' => random_int(100000, 900000),
'lang' => 'string',
'notes' => 'string',