From 740929aadc7cffd6cb370a302e0c4ff2c62ca2b4 Mon Sep 17 00:00:00 2001 From: Christian Berkman Date: Sun, 18 Aug 2024 17:37:39 +0300 Subject: [PATCH 1/8] [UserModel] add function getReturnType() --- src/Models/UserModel.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Models/UserModel.php b/src/Models/UserModel.php index 27020b0b8..a89fca1e8 100644 --- a/src/Models/UserModel.php +++ b/src/Models/UserModel.php @@ -395,4 +395,9 @@ private function checkReturnType(): void throw new LogicException('Return type must be a subclass of ' . User::class); } } + + public function getUserEntity(): User + { + return $this->returnType; + } } From 398a50e2d9591352d624c6d6cf473ab846a4340f Mon Sep 17 00:00:00 2001 From: Christian Berkman Date: Sun, 18 Aug 2024 17:38:19 +0300 Subject: [PATCH 2/8] [RegisterController] getUserEntity(): use Entity from UserModel --- src/Controllers/RegisterController.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Controllers/RegisterController.php b/src/Controllers/RegisterController.php index 7b310b1f8..a4fd5f33e 100644 --- a/src/Controllers/RegisterController.php +++ b/src/Controllers/RegisterController.php @@ -19,7 +19,6 @@ use CodeIgniter\HTTP\RequestInterface; use CodeIgniter\HTTP\ResponseInterface; use CodeIgniter\Shield\Authentication\Authenticators\Session; -use CodeIgniter\Shield\Entities\User; use CodeIgniter\Shield\Exceptions\ValidationException; use CodeIgniter\Shield\Models\UserModel; use CodeIgniter\Shield\Traits\Viewable; @@ -163,7 +162,10 @@ protected function getUserProvider(): UserModel */ protected function getUserEntity(): User { - return new User(); + $userProvider = $this->getUserProvider(); + $userEntityClass = $userProvider->getReturnType(); + + return new $userEntityClass(); } /** From eb38e9be331f5c7ff9e6e1e74c230e41100f5f07 Mon Sep 17 00:00:00 2001 From: Christian Berkman Date: Sun, 18 Aug 2024 17:56:27 +0300 Subject: [PATCH 3/8] Update docs --- docs/customization/user_provider.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/docs/customization/user_provider.md b/docs/customization/user_provider.md index 379574e80..fd9953b02 100644 --- a/docs/customization/user_provider.md +++ b/docs/customization/user_provider.md @@ -54,3 +54,11 @@ class UserModel extends ShieldUserModel } } ``` + +## Using a custom ReturnType +If you have set a custom `ReturnType` in your custom `UserModel`, you may retrieve the `ReturnTypt` using `UserModel::getReturnType()` and easily create a new `UserEntity` using your custom `ReturnType`: + +```php +$userEntityClass = $userModel->getReturnType(); +$newUser = new $userEntityClass(); +``` From d81d02e20f14f5a4e36f469672b3b8dc6eab1137 Mon Sep 17 00:00:00 2001 From: christianberkman <39840601+christianberkman@users.noreply.github.com> Date: Mon, 19 Aug 2024 14:34:45 +0300 Subject: [PATCH 4/8] Apply suggestions from code review Co-authored-by: kenjis --- docs/customization/user_provider.md | 9 ++++++--- src/Models/UserModel.php | 7 ++++++- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/docs/customization/user_provider.md b/docs/customization/user_provider.md index fd9953b02..2f26baefc 100644 --- a/docs/customization/user_provider.md +++ b/docs/customization/user_provider.md @@ -55,10 +55,13 @@ class UserModel extends ShieldUserModel } ``` -## Using a custom ReturnType -If you have set a custom `ReturnType` in your custom `UserModel`, you may retrieve the `ReturnTypt` using `UserModel::getReturnType()` and easily create a new `UserEntity` using your custom `ReturnType`: +## Using a Custom User Entity + +If you have set a custom `$returnType` in your custom `UserModel`, you may +retrieve the return type using the `UserModel::getReturnType()` method and +easily create a new User Entity using it: ```php $userEntityClass = $userModel->getReturnType(); -$newUser = new $userEntityClass(); +$newUser = new $userEntityClass(); ``` diff --git a/src/Models/UserModel.php b/src/Models/UserModel.php index a89fca1e8..e56e51491 100644 --- a/src/Models/UserModel.php +++ b/src/Models/UserModel.php @@ -396,7 +396,12 @@ private function checkReturnType(): void } } - public function getUserEntity(): User + /** + * Returns User Entity Type + * + * @return class-string + */ + public function getReturnType(): string { return $this->returnType; } From f637d2ad245244bde3617ee6234996479c803591 Mon Sep 17 00:00:00 2001 From: christianberkman <39840601+christianberkman@users.noreply.github.com> Date: Mon, 19 Aug 2024 14:36:32 +0300 Subject: [PATCH 5/8] revert removeal of Sheild's User class --- src/Controllers/RegisterController.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Controllers/RegisterController.php b/src/Controllers/RegisterController.php index a4fd5f33e..d72f6337f 100644 --- a/src/Controllers/RegisterController.php +++ b/src/Controllers/RegisterController.php @@ -19,6 +19,7 @@ use CodeIgniter\HTTP\RequestInterface; use CodeIgniter\HTTP\ResponseInterface; use CodeIgniter\Shield\Authentication\Authenticators\Session; +use CodeIgniter\Shield\Entities\User; use CodeIgniter\Shield\Exceptions\ValidationException; use CodeIgniter\Shield\Models\UserModel; use CodeIgniter\Shield\Traits\Viewable; From 20ab4a582e63612949feac0a04f05e28fbdf4592 Mon Sep 17 00:00:00 2001 From: kenjis Date: Sun, 8 Sep 2024 11:45:46 +0900 Subject: [PATCH 6/8] refactor: add createNewUser() and remove getReturnType() --- src/Controllers/RegisterController.php | 5 ++--- src/Models/UserModel.php | 8 ++++---- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/Controllers/RegisterController.php b/src/Controllers/RegisterController.php index d72f6337f..6b34f0252 100644 --- a/src/Controllers/RegisterController.php +++ b/src/Controllers/RegisterController.php @@ -163,10 +163,9 @@ protected function getUserProvider(): UserModel */ protected function getUserEntity(): User { - $userProvider = $this->getUserProvider(); - $userEntityClass = $userProvider->getReturnType(); + $userProvider = $this->getUserProvider(); - return new $userEntityClass(); + return $userProvider->createNewUser(); } /** diff --git a/src/Models/UserModel.php b/src/Models/UserModel.php index e56e51491..620434c1a 100644 --- a/src/Models/UserModel.php +++ b/src/Models/UserModel.php @@ -397,12 +397,12 @@ private function checkReturnType(): void } /** - * Returns User Entity Type + * Returns a new User Entity. * - * @return class-string + * @param array|bool|float|int|object|string|null> $data (Optional) user data */ - public function getReturnType(): string + public function createNewUser(array $data = []): User { - return $this->returnType; + return new $this->returnType($data); } } From a2c36f10b3f63348cfa9f2e242027c1cdb8893f2 Mon Sep 17 00:00:00 2001 From: kenjis Date: Sun, 8 Sep 2024 11:51:24 +0900 Subject: [PATCH 7/8] refactor: we don't need to use getUserEntity() --- src/Controllers/RegisterController.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Controllers/RegisterController.php b/src/Controllers/RegisterController.php index 6b34f0252..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,6 +159,8 @@ protected function getUserProvider(): UserModel /** * Returns the Entity class that should be used + * + * @deprecated 1.2.0 No longer used. */ protected function getUserEntity(): User { From 9bf9e7d5fcf41a6eb450d9a4e44c2cdefe0a56ca Mon Sep 17 00:00:00 2001 From: kenjis Date: Sun, 8 Sep 2024 12:02:34 +0900 Subject: [PATCH 8/8] docs: update docs --- docs/customization/user_provider.md | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/docs/customization/user_provider.md b/docs/customization/user_provider.md index 2f26baefc..8ae742e19 100644 --- a/docs/customization/user_provider.md +++ b/docs/customization/user_provider.md @@ -55,13 +55,16 @@ class UserModel extends ShieldUserModel } ``` -## Using a Custom User Entity +## Creating a Custom User Entity -If you have set a custom `$returnType` in your custom `UserModel`, you may -retrieve the return type using the `UserModel::getReturnType()` method and -easily create a new User Entity using it: +Starting from v1.2.0, `UserModel` in Shield has the `createNewUser()` method to +create a new User Entity. ```php -$userEntityClass = $userModel->getReturnType(); -$newUser = new $userEntityClass(); +$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.