Skip to content

docs: improve banning users #1109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 27 additions & 13 deletions docs/references/authentication/authentication.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 6 additions & 0 deletions docs/user_management/banning_users.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
8 changes: 7 additions & 1 deletion docs/user_management/forcing_password_reset.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -32,7 +38,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
Expand Down
23 changes: 21 additions & 2 deletions docs/user_management/managing_users.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,26 @@ 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. 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;
Expand All @@ -30,7 +47,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)
Expand Down