diff --git a/src/install/sql/content.sql b/src/install/sql/content.sql index 55cd3b8fb..b381cd37e 100644 --- a/src/install/sql/content.sql +++ b/src/install/sql/content.sql @@ -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'), diff --git a/src/install/sql/structure.sql b/src/install/sql/structure.sql index 5f32b4c6c..c411d4dc2 100644 --- a/src/install/sql/structure.sql +++ b/src/install/sql/structure.sql @@ -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, diff --git a/src/library/FOSSBilling/UpdatePatcher.php b/src/library/FOSSBilling/UpdatePatcher.php index 923bfc7fe..b12ea9732 100644 --- a/src/library/FOSSBilling/UpdatePatcher.php +++ b/src/library/FOSSBilling/UpdatePatcher.php @@ -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); diff --git a/src/library/Model/Client.php b/src/library/Model/Client.php index 3b97ea7c1..8e246ccbc 100644 --- a/src/library/Model/Client.php +++ b/src/library/Model/Client.php @@ -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 { diff --git a/src/modules/Profile/ClientValidator.php b/src/modules/Profile/ClientValidator.php new file mode 100644 index 000000000..1320d7268 --- /dev/null +++ b/src/modules/Profile/ClientValidator.php @@ -0,0 +1,77 @@ +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; + } + +} diff --git a/src/modules/Profile/Service.php b/src/modules/Profile/Service.php index ecd6925bd..18b5bcac6 100644 --- a/src/modules/Profile/Service.php +++ b/src/modules/Profile/Service.php @@ -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; diff --git a/tests-legacy/modules/Profile/ServiceTest.php b/tests-legacy/modules/Profile/ServiceTest.php index 03ce0104b..bb2bac5d0 100644 --- a/tests-legacy/modules/Profile/ServiceTest.php +++ b/tests-legacy/modules/Profile/ServiceTest.php @@ -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',