From da8d5ed2344a685168dadbde23d1c24b7111385a Mon Sep 17 00:00:00 2001 From: ArchBlood <35392110+ArchBlood@users.noreply.github.com> Date: Mon, 23 Sep 2024 20:32:41 -0400 Subject: [PATCH 01/10] Update Events.php --- Events.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Events.php b/Events.php index 46a9495..8522068 100644 --- a/Events.php +++ b/Events.php @@ -283,6 +283,17 @@ public static function onAccountSettingsMenuInit($event) } } + public static function onBeforeValidate($event) + { + $registrationForm = $event->sender; + $minimumAge = Yii::$app->getModule('legal')->getMinimumAge(); + + if ($minimumAge > 0) { + $ageValidator = new validators\AgeValidator(['minimumAge' => $minimumAge]); + $ageValidator->validateAttribute($registrationForm, 'birthday'); + } + } + /** * Callback on daily cron job run */ From 042648e637e90683aca63a3c8718413204c10197 Mon Sep 17 00:00:00 2001 From: ArchBlood <35392110+ArchBlood@users.noreply.github.com> Date: Mon, 23 Sep 2024 20:33:22 -0400 Subject: [PATCH 02/10] Create AgeValidator.php --- validators/AgeValidator.php | 63 +++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 validators/AgeValidator.php diff --git a/validators/AgeValidator.php b/validators/AgeValidator.php new file mode 100644 index 0000000..0f7180e --- /dev/null +++ b/validators/AgeValidator.php @@ -0,0 +1,63 @@ +minimumAge === null) { + $this->minimumAge = Yii::$app->getModule('legal')->getMinimumAge(); + } + } + + /** + * Validates the age of the user based on the given attribute value. + * + * @param \yii\base\Model $model the data model being validated + * @param string $attribute the name of the attribute to be validated + */ + public function validateAttribute($model, $attribute) + { + $value = $model->$attribute; + + if (!$value instanceof DateTime) { + try { + $value = new DateTime($value); + } catch (\Exception $e) { + $this->addError($model, $attribute, Yii::t('LegalModule.base', 'Invalid date format.')); + return; + } + } + + $today = new DateTime(); + $age = $today->diff($value)->y; + + if ($age < $this->minimumAge) { + $message = Yii::t('LegalModule.base', 'You must be at least {age} years old.', ['age' => $this->minimumAge]); + $this->addError($model, $attribute, $message); + + if ($this->minimumAge > 0 && isset($model->user) && $model->user instanceof User) { + $model->user->status = User::STATUS_DISABLED; + $model->user->save(false); + } + } + } +} From 7f06858b130fada73b1f1d33bf582fe081f49957 Mon Sep 17 00:00:00 2001 From: ArchBlood <35392110+ArchBlood@users.noreply.github.com> Date: Mon, 23 Sep 2024 20:34:02 -0400 Subject: [PATCH 03/10] Update config.php --- config.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/config.php b/config.php index 046312f..d25c356 100644 --- a/config.php +++ b/config.php @@ -5,6 +5,7 @@ use humhub\modules\content\widgets\richtext\ProsemirrorRichText; use humhub\modules\user\models\forms\Registration; use humhub\modules\user\widgets\AccountSettingsMenu; +use humhub\modules\user\models\Profile; use humhub\widgets\FooterMenu; use humhub\widgets\LayoutAddons; @@ -28,5 +29,6 @@ ['class' => ProsemirrorRichText::class, 'event' => ProsemirrorRichText::EVENT_AFTER_RUN, 'callback' => ['humhub\modules\legal\Events', 'onAfterRunRichText']], ['class' => AccountSettingsMenu::class, 'event' => AccountSettingsMenu::EVENT_INIT, 'callback' => ['humhub\modules\legal\Events', 'onAccountSettingsMenuInit']], ['class' => CronController::class, 'event' => CronController::EVENT_ON_DAILY_RUN, 'callback' => ['humhub\modules\legal\Events', 'onCronDailyRun']], + ['class' => Profile::class, 'event' => Profile::EVENT_BEFORE_VALIDATE, 'callback' => ['humhub\modules\legal\Events', 'onBeforeValidate']], ], ]; From 7a2d5a2b49d6c9b267064675e2ef0cea76c45e92 Mon Sep 17 00:00:00 2001 From: ArchBlood <35392110+ArchBlood@users.noreply.github.com> Date: Mon, 23 Sep 2024 21:49:23 -0400 Subject: [PATCH 04/10] Update LegalCest.php --- tests/codeception/acceptance/LegalCest.php | 46 +++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/tests/codeception/acceptance/LegalCest.php b/tests/codeception/acceptance/LegalCest.php index 4044377..f5926f5 100644 --- a/tests/codeception/acceptance/LegalCest.php +++ b/tests/codeception/acceptance/LegalCest.php @@ -132,4 +132,48 @@ public function testAgeVerification(AcceptanceTester $I) $I->amUser1(true); $I->dontSee($title, '.panel-heading'); } -} \ No newline at end of file + + public function testAgeValidation(AcceptanceTester $I) + { + $I->wantTo('test age validation during registration and profile update'); + $minimumAge = 18; + + $I->amAdmin(); + $I->amGoingTo('enable age verification'); + $I->enableAgeVerification($minimumAge); + + // Test registration with valid age + $I->amGoingTo('test registration with valid age'); + $I->amOnRoute('/user/registration'); + $I->fillField('Registration[username]', 'validAgeUser'); + $I->fillField('Registration[email]', 'validage@example.com'); + $I->fillField('Registration[password]', 'ValidPassword123'); + $I->fillField('Registration[birthday]', date('Y-m-d', strtotime("-{$minimumAge} years -1 day"))); + $I->click('Register'); + $I->dontSee('You must be at least ' . $minimumAge . ' years old.'); + + // Test registration with invalid age + $I->amGoingTo('test registration with invalid age'); + $I->amOnRoute('/user/registration'); + $I->fillField('Registration[username]', 'invalidAgeUser'); + $I->fillField('Registration[email]', 'invalidage@example.com'); + $I->fillField('Registration[password]', 'InvalidPassword123'); + $I->fillField('Registration[birthday]', date('Y-m-d', strtotime("-{$minimumAge} years +1 day"))); + $I->click('Register'); + $I->see('You must be at least ' . $minimumAge . ' years old.'); + + // Test profile update with invalid age + $I->amGoingTo('test profile update with invalid age'); + $I->amUser1(true); + $I->amOnRoute('/user/account/edit'); + $I->fillField('Profile[birthday]', date('Y-m-d', strtotime("-{$minimumAge} years +1 day"))); + $I->click('Save'); + $I->see('You must be at least ' . $minimumAge . ' years old.'); + + // Test profile update with valid age + $I->amGoingTo('test profile update with valid age'); + $I->fillField('Profile[birthday]', date('Y-m-d', strtotime("-{$minimumAge} years -1 day"))); + $I->click('Save'); + $I->dontSee('You must be at least ' . $minimumAge . ' years old.'); + } +} From 805e323cb64f0eac21a421f466bb27a3637ce1c9 Mon Sep 17 00:00:00 2001 From: ArchBlood <35392110+ArchBlood@users.noreply.github.com> Date: Tue, 24 Sep 2024 10:29:12 -0400 Subject: [PATCH 05/10] Update AgeValidator.php --- validators/AgeValidator.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/validators/AgeValidator.php b/validators/AgeValidator.php index 0f7180e..34c9413 100644 --- a/validators/AgeValidator.php +++ b/validators/AgeValidator.php @@ -6,6 +6,7 @@ use Yii; use yii\validators\Validator; use humhub\modules\user\models\User; +use humhub\modules\user\models\Group; /** * AgeValidator validates that the given value represents an age greater than or equal to a specified minimum age. @@ -18,7 +19,7 @@ class AgeValidator extends Validator public $minimumAge; /** - * {@inheritdoc} + * @inheritdoc */ public function init() { @@ -36,6 +37,12 @@ public function init() */ public function validateAttribute($model, $attribute) { + // Check if the user is a member of the admin group + if (Group::getAdminGroup()->isMember($model)) { + // Skip validation for admin accounts + return; + } + $value = $model->$attribute; if (!$value instanceof DateTime) { From b00d78355153b0f5b31616753a19b0fc6c12fc69 Mon Sep 17 00:00:00 2001 From: ArchBlood <35392110+ArchBlood@users.noreply.github.com> Date: Tue, 24 Sep 2024 18:30:49 -0400 Subject: [PATCH 06/10] Fix: Validation for admins --- validators/AgeValidator.php | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/validators/AgeValidator.php b/validators/AgeValidator.php index 34c9413..8ae0796 100644 --- a/validators/AgeValidator.php +++ b/validators/AgeValidator.php @@ -1,5 +1,4 @@ isMember($model)) { - // Skip validation for admin accounts - return; - } - $value = $model->$attribute; - if (!$value instanceof DateTime) { try { $value = new DateTime($value); @@ -61,7 +53,8 @@ public function validateAttribute($model, $attribute) $message = Yii::t('LegalModule.base', 'You must be at least {age} years old.', ['age' => $this->minimumAge]); $this->addError($model, $attribute, $message); - if ($this->minimumAge > 0 && isset($model->user) && $model->user instanceof User) { + // Disable the user account if they are underage and not an admin + if ($this->minimumAge > 0 && isset($model->user) && $model->user instanceof User && !$model->user->isSystemAdmin()) { $model->user->status = User::STATUS_DISABLED; $model->user->save(false); } From 36912378a3ce0e57e9a98850e4c5c47f9317779a Mon Sep 17 00:00:00 2001 From: ArchBlood <35392110+ArchBlood@users.noreply.github.com> Date: Tue, 24 Sep 2024 18:42:50 -0400 Subject: [PATCH 07/10] Fix: Removed unused class --- validators/AgeValidator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/validators/AgeValidator.php b/validators/AgeValidator.php index 8ae0796..cd3beea 100644 --- a/validators/AgeValidator.php +++ b/validators/AgeValidator.php @@ -1,11 +1,11 @@ Date: Wed, 25 Sep 2024 15:00:42 -0400 Subject: [PATCH 08/10] Fix: Remove Disabling Account --- validators/AgeValidator.php | 1 - 1 file changed, 1 deletion(-) diff --git a/validators/AgeValidator.php b/validators/AgeValidator.php index cd3beea..aba379f 100644 --- a/validators/AgeValidator.php +++ b/validators/AgeValidator.php @@ -55,7 +55,6 @@ public function validateAttribute($model, $attribute) // Disable the user account if they are underage and not an admin if ($this->minimumAge > 0 && isset($model->user) && $model->user instanceof User && !$model->user->isSystemAdmin()) { - $model->user->status = User::STATUS_DISABLED; $model->user->save(false); } } From 1a4c9fbb7fb5dbf93e448e2a15c61422cffb2b32 Mon Sep 17 00:00:00 2001 From: ArchBlood <35392110+ArchBlood@users.noreply.github.com> Date: Thu, 26 Sep 2024 13:42:36 -0400 Subject: [PATCH 09/10] Update AgeValidator.php --- validators/AgeValidator.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/validators/AgeValidator.php b/validators/AgeValidator.php index aba379f..2ea3f8a 100644 --- a/validators/AgeValidator.php +++ b/validators/AgeValidator.php @@ -52,11 +52,6 @@ public function validateAttribute($model, $attribute) if ($age < $this->minimumAge) { $message = Yii::t('LegalModule.base', 'You must be at least {age} years old.', ['age' => $this->minimumAge]); $this->addError($model, $attribute, $message); - - // Disable the user account if they are underage and not an admin - if ($this->minimumAge > 0 && isset($model->user) && $model->user instanceof User && !$model->user->isSystemAdmin()) { - $model->user->save(false); - } } } } From a00867d0581963b02b54a4a85c1d853c666d8cd6 Mon Sep 17 00:00:00 2001 From: ArchBlood <35392110+ArchBlood@users.noreply.github.com> Date: Thu, 26 Sep 2024 13:45:36 -0400 Subject: [PATCH 10/10] Update CHANGELOG.md --- docs/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index b7a2049..7e4f2ba 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -4,6 +4,7 @@ Changelog 1.4.3 (Unreleased) -------------------------- - Fix #85: Fix downloading of large user export data file +- Enh: Check User Birthday field 1.4.2 (September 13, 2024) --------------------------