diff --git a/docs/customization/user_provider.md b/docs/customization/user_provider.md index 379574e80..8ae742e19 100644 --- a/docs/customization/user_provider.md +++ b/docs/customization/user_provider.md @@ -54,3 +54,17 @@ class UserModel extends ShieldUserModel } } ``` + +## Creating a Custom User Entity + +Starting from v1.2.0, `UserModel` in Shield has the `createNewUser()` method to +create a new User Entity. + +```php +$user = $userModel->createNewUser($data); +``` + +It takes an optional user data array as the first argument, and passes it to the +constructor of the `$returnType` class. + +If your custom User entity cannot be instantiated in this way, override this method. diff --git a/src/Controllers/RegisterController.php b/src/Controllers/RegisterController.php index 7b310b1f8..b85e90082 100644 --- a/src/Controllers/RegisterController.php +++ b/src/Controllers/RegisterController.php @@ -103,8 +103,7 @@ public function registerAction(): RedirectResponse // Save the user $allowedPostFields = array_keys($rules); - $user = $this->getUserEntity(); - $user->fill($this->request->getPost($allowedPostFields)); + $user = $users->createNewUser($this->request->getPost($allowedPostFields)); // Workaround for email only registration/login if ($user->username === null) { @@ -160,10 +159,14 @@ protected function getUserProvider(): UserModel /** * Returns the Entity class that should be used + * + * @deprecated 1.2.0 No longer used. */ protected function getUserEntity(): User { - return new User(); + $userProvider = $this->getUserProvider(); + + return $userProvider->createNewUser(); } /** diff --git a/src/Models/UserModel.php b/src/Models/UserModel.php index 27020b0b8..620434c1a 100644 --- a/src/Models/UserModel.php +++ b/src/Models/UserModel.php @@ -395,4 +395,14 @@ private function checkReturnType(): void throw new LogicException('Return type must be a subclass of ' . User::class); } } + + /** + * Returns a new User Entity. + * + * @param array|bool|float|int|object|string|null> $data (Optional) user data + */ + public function createNewUser(array $data = []): User + { + return new $this->returnType($data); + } }