diff --git a/CHANGELOG.md b/CHANGELOG.md index ddf58c957a..bfe73ea23f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ HumHub Changelog - Fix #7465: Formatted Arabic numbers displays 0 instead of the number - Fix #7471: Fix advanced searching by space filter - Fix #7472: Fix missing fields when creating a new user from admin +- Fix #7477: Refactor Registration Form Options 1.17.1 (March 6, 2025) ---------------------- diff --git a/MIGRATE-DEV.md b/MIGRATE-DEV.md index b966356063..f241dcb350 100644 --- a/MIGRATE-DEV.md +++ b/MIGRATE-DEV.md @@ -1,13 +1,14 @@ Module Migration Guide ====================== -Version 1.17.1 +Version 1.17.2 --------------- ### Behaviour change - Method signature changed - `humhub\modules\user\models\fieldtype\BaseType::getUserValue(User $user, bool $raw = true, bool $encode = true): ?string` +- Constructor changed - `humhub\modules\user\models\forms\Registration` and properties (`$enablePasswordForm`, `$enableMustChangePassword`, `$enableEmailField`) are now private Version 1.17 diff --git a/protected/humhub/modules/admin/controllers/UserController.php b/protected/humhub/modules/admin/controllers/UserController.php index acdcb4e384..9efd5a8762 100644 --- a/protected/humhub/modules/admin/controllers/UserController.php +++ b/protected/humhub/modules/admin/controllers/UserController.php @@ -261,11 +261,8 @@ class UserController extends Controller public function actionAdd() { - $registration = new Registration([], null, [ - 'enableEmailField' => true, - 'enableUserApproval' => false, - 'enableMustChangePassword' => true, - ]); + $registration = new Registration(enableEmailField: true, enablePasswordForm: true, enableMustChangePassword: true); + $registration->enableUserApproval = true; if ($registration->submitted('save') && $registration->validate() && $registration->register()) { return $this->redirect(['edit', 'id' => $registration->getUser()->id]); diff --git a/protected/humhub/modules/user/controllers/RegistrationController.php b/protected/humhub/modules/user/controllers/RegistrationController.php index e7271e37c4..14867c79ae 100644 --- a/protected/humhub/modules/user/controllers/RegistrationController.php +++ b/protected/humhub/modules/user/controllers/RegistrationController.php @@ -92,7 +92,7 @@ class RegistrationController extends Controller $inviteRegistrationService->populateRegistration($registration); } elseif (Yii::$app->session->has('authClient')) { $authClient = Yii::$app->session->get('authClient'); - $this->handleAuthClientRegistration($authClient, $registration); + $registration = $this->createRegistrationByAuthClient($authClient); } else { Yii::warning('Registration failed: No token (query) or authclient (session) found!', 'user'); Yii::$app->session->setFlash('error', 'Registration failed.'); @@ -173,7 +173,7 @@ class RegistrationController extends Controller * @param Registration $registration * @throws Exception */ - protected function handleAuthClientRegistration(ClientInterface $authClient, Registration $registration) + protected function createRegistrationByAuthClient(ClientInterface $authClient): Registration { $attributes = $authClient->getUserAttributes(); @@ -181,7 +181,8 @@ class RegistrationController extends Controller throw new Exception("No user id given by authclient!"); } - $registration->enablePasswordForm = false; + $registration = new Registration(enablePasswordForm: false); + if ($authClient instanceof ApprovalBypass) { $registration->enableUserApproval = false; } @@ -191,5 +192,7 @@ class RegistrationController extends Controller $registration->getUser()->setAttributes($attributes, false); $registration->getProfile()->setAttributes($attributes, false); + + return $registration; } } diff --git a/protected/humhub/modules/user/models/forms/Registration.php b/protected/humhub/modules/user/models/forms/Registration.php index 5d688963fa..d0a523d1f2 100644 --- a/protected/humhub/modules/user/models/forms/Registration.php +++ b/protected/humhub/modules/user/models/forms/Registration.php @@ -36,17 +36,17 @@ class Registration extends HForm /** * @var bool show password creation form */ - public $enablePasswordForm = true; + private $enablePasswordForm; /** * @var bool show checkbox to force to change password on first log in */ - public $enableMustChangePassword = false; + private $enableMustChangePassword; /** * @var bool show e-mail field */ - public $enableEmailField = false; + private $enableEmailField; /** * @var bool|null require user approval by admin after registration. @@ -73,6 +73,21 @@ class Registration extends HForm */ private $_profile = null; + public function __construct( + $definition = [], + $primaryModel = null, + array $config = [], + bool $enableEmailField = false, + bool $enablePasswordForm = true, + bool $enableMustChangePassword = false + ) { + $this->enableEmailField = $enableEmailField; + $this->enablePasswordForm = $enablePasswordForm; + $this->enableMustChangePassword = $enableMustChangePassword; + + parent::__construct($definition, $primaryModel, $config); + } + /** * @inheritdoc */ @@ -102,7 +117,8 @@ class Registration extends HForm if ($this->enablePasswordForm) { $this->definition['elements']['Password'] = $this->getPasswordFormDefinition(); } - $this->definition['elements']['Profile'] = array_merge(['type' => 'form'], $this->getProfile()->getFormDefinition()); + $this->definition['elements']['Profile'] = array_merge(['type' => 'form'], + $this->getProfile()->getFormDefinition()); $this->definition['buttons'] = [ 'save' => [ 'type' => 'submit', @@ -181,7 +197,9 @@ class Registration extends HForm { $groupModels = Group::getRegistrationGroups($this->getUser()); - $groupFieldType = (Yii::$app->getModule('user')->settings->get('auth.showRegistrationUserGroup') && count($groupModels) > 1) + $groupFieldType = (Yii::$app->getModule('user')->settings->get('auth.showRegistrationUserGroup') && count( + $groupModels + ) > 1) ? 'dropdownlist' : 'hidden'; // TODO: Completely hide the element instead of current @@ -266,7 +284,6 @@ class Registration extends HForm } if ($this->models['User']->save()) { - // Save User Profile $this->models['Profile']->user_id = $this->models['User']->id; $this->models['Profile']->save(); @@ -290,7 +307,10 @@ class Registration extends HForm if ($authClient !== null) { (new AuthClientUserService($this->models['User']))->add($authClient); - $authClient->trigger(BaseClient::EVENT_CREATE_USER, new UserEvent(['identity' => $this->models['User']])); + $authClient->trigger( + BaseClient::EVENT_CREATE_USER, + new UserEvent(['identity' => $this->models['User']]) + ); } $this->trigger(self::EVENT_AFTER_REGISTRATION, new UserEvent(['identity' => $this->models['User']])); diff --git a/protected/humhub/modules/user/services/AuthClientService.php b/protected/humhub/modules/user/services/AuthClientService.php index 1129d84f1a..00b2122a3f 100644 --- a/protected/humhub/modules/user/services/AuthClientService.php +++ b/protected/humhub/modules/user/services/AuthClientService.php @@ -123,10 +123,7 @@ class AuthClientService return null; } - $registration = new Registration([], null, [ - 'enablePasswordForm' => false, - 'enableEmailField' => true, - ]); + $registration = new Registration(enablePasswordForm: false, enableEmailField: true); if ($this->authClient instanceof ApprovalBypass) { $registration->enableUserApproval = false;