From 4bc300fc42474d19e7b9db70e1346d0c30049720 Mon Sep 17 00:00:00 2001 From: kenjis Date: Thu, 25 Apr 2024 09:14:43 +0900 Subject: [PATCH 1/5] docs: fix wrong link --- docs/user_management/forcing_password_reset.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/user_management/forcing_password_reset.md b/docs/user_management/forcing_password_reset.md index e5b8faead..f426f3773 100644 --- a/docs/user_management/forcing_password_reset.md +++ b/docs/user_management/forcing_password_reset.md @@ -32,7 +32,7 @@ if ($user->requiresPasswordReset()) { !!! note - You can use the [force-reset](../../references/controller_filters/#forcing-password-reset) + You can use the [force-reset](../references/controller_filters/#forcing-password-reset) filter to check. ### Force Password Reset On a User From 405592e0c8f8b4bcb767f5b76a231e04c8d7a2f7 Mon Sep 17 00:00:00 2001 From: kenjis Date: Thu, 25 Apr 2024 09:17:48 +0900 Subject: [PATCH 2/5] docs: improve explanation and sample code --- .../authentication/authentication.md | 40 +++++++++++++------ 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/docs/references/authentication/authentication.md b/docs/references/authentication/authentication.md index 9cb057d3a..6ff162fee 100644 --- a/docs/references/authentication/authentication.md +++ b/docs/references/authentication/authentication.md @@ -29,28 +29,42 @@ public string $defaultAuthenticator = 'session'; ## Auth Helper -The auth functionality is designed to be used with the `auth_helper` that comes with Shield. This -helper method provides the `auth()` function which returns a convenient interface to the most frequently -used functionality within the auth libraries. +The auth functionality is designed to be used with the `auth_helper` that comes +with Shield. + +!!! note + + The `auth_helper` is autoloaded by CodeIgniter's autoloader if you follow the + installation instruction. If you want to *override* the functions, create + **app/Helpers/auth_helper.php**. + +### Getting the Current User + +The `auth()` function returns a convenient interface to the most frequently used +functionality within the auth libraries. + +You can get the current `User` entity. ```php // get the current user -auth()->user(); +$user = auth()->user(); // get the current user's id -auth()->id(); +$user_id = auth()->id(); // or -user_id(); - -// get the User Provider (UserModel by default) -auth()->getProvider(); +$user_id = user_id(); ``` -!!! note +The `user_id()` function returns the current user's id. - The `auth_helper` is autoloaded by CodeIgniter's autoloader if you follow the - installation instruction. If you want to *override* the functions, create - **app/Helpers/auth_helper.php**. +### Getting the User Provider + +You can also get the User Provider. + +```php +// get the User Provider (UserModel by default) +$users = auth()->getProvider(); +``` ## Authenticator Responses From 019fc9715c6d798f60194e5d639f10c13781cbad Mon Sep 17 00:00:00 2001 From: kenjis Date: Thu, 25 Apr 2024 09:18:29 +0900 Subject: [PATCH 3/5] docs: add empty lines --- docs/user_management/managing_users.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/docs/user_management/managing_users.md b/docs/user_management/managing_users.md index 834670ac4..97d38374d 100644 --- a/docs/user_management/managing_users.md +++ b/docs/user_management/managing_users.md @@ -6,7 +6,9 @@ Since Shield uses a more complex user setup than many other systems, separating ### Creating Users -By default, the only values stored in the users table is the username. The first step is to create the user record with the username. If you don't have a username, be sure to set the value to `null` anyway, so that it passes CodeIgniter's empty data check. +By default, the only values stored in the users table is the username. + +The first step is to create the user record with the username. If you don't have a username, be sure to set the value to `null` anyway, so that it passes CodeIgniter's empty data check. ```php use CodeIgniter\Shield\Entities\User; @@ -30,7 +32,9 @@ $users->addToDefaultGroup($user); ### Deleting Users -A user's data can be spread over a few different tables so you might be concerned about how to delete all of the user's data from the system. This is handled automatically at the database level for all information that Shield knows about, through the `onCascade` settings of the table's foreign keys. You can delete a user like any other entity. +A user's data can be spread over a few different tables so you might be concerned about how to delete all of the user's data from the system. This is handled automatically at the database level for all information that Shield knows about, through the `onCascade` settings of the table's foreign keys. + +You can delete a user like any other entity. ```php // Get the User Provider (UserModel by default) From 37d73fc3ec6df9058bcb1f1472e55bdc6cde69ee Mon Sep 17 00:00:00 2001 From: kenjis Date: Thu, 25 Apr 2024 09:18:47 +0900 Subject: [PATCH 4/5] docs: add section "Finding a User" --- docs/user_management/managing_users.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/docs/user_management/managing_users.md b/docs/user_management/managing_users.md index 97d38374d..3c321bd3d 100644 --- a/docs/user_management/managing_users.md +++ b/docs/user_management/managing_users.md @@ -4,6 +4,21 @@ Since Shield uses a more complex user setup than many other systems, separating ## Managing Users by Code +### Finding a User + +You can find an existing user from the User Provider. It returns a `User` +[entity](https://codeigniter.com/user_guide/models/entities.html). + +```php +// Get the User Provider (UserModel by default) +$users = auth()->getProvider(); + +// Find by the user_id +$user = $users->findById(123); +// Find by the user email +$user = $users->findByCredentials(['email' => 'user@example.com']); +``` + ### Creating Users By default, the only values stored in the users table is the username. From 70aa4fed7e5a1ef2f6ed18bded11ded66a432235 Mon Sep 17 00:00:00 2001 From: kenjis Date: Thu, 25 Apr 2024 09:19:11 +0900 Subject: [PATCH 5/5] docs: add note for how to get a user --- docs/user_management/banning_users.md | 6 ++++++ docs/user_management/forcing_password_reset.md | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/docs/user_management/banning_users.md b/docs/user_management/banning_users.md index 54e1196fd..fed4b7936 100644 --- a/docs/user_management/banning_users.md +++ b/docs/user_management/banning_users.md @@ -2,6 +2,12 @@ Shield provides a way to ban users from your application. This is useful if you need to prevent a user from logging in, or logging them out in the event that they breach your terms of service. +!!! note + + Before using the following methods, you need to get the `User` entity. See + [Getting the Current User](../references/authentication/authentication.md#getting-the-current-user) + or [Finding a User](./managing_users.md#finding-a-user) for details. + ### Check if a User is Banned You can check if a user is banned using `isBanned()` method on the `User` entity. The method returns a boolean `true`/`false`. diff --git a/docs/user_management/forcing_password_reset.md b/docs/user_management/forcing_password_reset.md index f426f3773..2fae5f98e 100644 --- a/docs/user_management/forcing_password_reset.md +++ b/docs/user_management/forcing_password_reset.md @@ -8,6 +8,12 @@ compromised. This guide provides you with ways to achieve this. +!!! note + + Before using the following methods, you need to get the `User` entity. See + [Getting the Current User](../references/authentication/authentication.md#getting-the-current-user) + or [Finding a User](./managing_users.md#finding-a-user) for details. + ## Available Methods Shield provides a way to enforce password resets throughout your application.