Skip to content

Commit 289a191

Browse files
authored
Merge branch 'codeigniter4:develop' into userentity
2 parents 1919d46 + e7684c4 commit 289a191

File tree

8 files changed

+117
-24
lines changed

8 files changed

+117
-24
lines changed

docs/references/authentication/authentication.md

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,28 +29,42 @@ public string $defaultAuthenticator = 'session';
2929

3030
## Auth Helper
3131

32-
The auth functionality is designed to be used with the `auth_helper` that comes with Shield. This
33-
helper method provides the `auth()` function which returns a convenient interface to the most frequently
34-
used functionality within the auth libraries.
32+
The auth functionality is designed to be used with the `auth_helper` that comes
33+
with Shield.
34+
35+
!!! note
36+
37+
The `auth_helper` is autoloaded by CodeIgniter's autoloader if you follow the
38+
installation instruction. If you want to *override* the functions, create
39+
**app/Helpers/auth_helper.php**.
40+
41+
### Getting the Current User
42+
43+
The `auth()` function returns a convenient interface to the most frequently used
44+
functionality within the auth libraries.
45+
46+
You can get the current `User` entity.
3547

3648
```php
3749
// get the current user
38-
auth()->user();
50+
$user = auth()->user();
3951

4052
// get the current user's id
41-
auth()->id();
53+
$user_id = auth()->id();
4254
// or
43-
user_id();
44-
45-
// get the User Provider (UserModel by default)
46-
auth()->getProvider();
55+
$user_id = user_id();
4756
```
4857

49-
!!! note
58+
The `user_id()` function returns the current user's id.
5059

51-
The `auth_helper` is autoloaded by CodeIgniter's autoloader if you follow the
52-
installation instruction. If you want to *override* the functions, create
53-
**app/Helpers/auth_helper.php**.
60+
### Getting the User Provider
61+
62+
You can also get the User Provider.
63+
64+
```php
65+
// get the User Provider (UserModel by default)
66+
$users = auth()->getProvider();
67+
```
5468

5569
## Authenticator Responses
5670

docs/user_management/banning_users.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
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.
44

5+
!!! note
6+
7+
Before using the following methods, you need to get the `User` entity. See
8+
[Getting the Current User](../references/authentication/authentication.md#getting-the-current-user)
9+
or [Finding a User](./managing_users.md#finding-a-user) for details.
10+
511
### Check if a User is Banned
612

713
You can check if a user is banned using `isBanned()` method on the `User` entity. The method returns a boolean `true`/`false`.

docs/user_management/forcing_password_reset.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ compromised.
88

99
This guide provides you with ways to achieve this.
1010

11+
!!! note
12+
13+
Before using the following methods, you need to get the `User` entity. See
14+
[Getting the Current User](../references/authentication/authentication.md#getting-the-current-user)
15+
or [Finding a User](./managing_users.md#finding-a-user) for details.
16+
1117
## Available Methods
1218

1319
Shield provides a way to enforce password resets throughout your application.
@@ -32,7 +38,7 @@ if ($user->requiresPasswordReset()) {
3238

3339
!!! note
3440

35-
You can use the [force-reset](../../references/controller_filters/#forcing-password-reset)
41+
You can use the [force-reset](../references/controller_filters/#forcing-password-reset)
3642
filter to check.
3743

3844
### Force Password Reset On a User

docs/user_management/managing_users.md

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,26 @@ Since Shield uses a more complex user setup than many other systems, separating
44

55
## Managing Users by Code
66

7+
### Finding a User
8+
9+
You can find an existing user from the User Provider. It returns a `User`
10+
[entity](https://codeigniter.com/user_guide/models/entities.html).
11+
12+
```php
13+
// Get the User Provider (UserModel by default)
14+
$users = auth()->getProvider();
15+
16+
// Find by the user_id
17+
$user = $users->findById(123);
18+
// Find by the user email
19+
$user = $users->findByCredentials(['email' => '[email protected]']);
20+
```
21+
722
### Creating Users
823

9-
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.
24+
By default, the only values stored in the users table is the username.
25+
26+
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.
1027

1128
```php
1229
use CodeIgniter\Shield\Entities\User;
@@ -30,7 +47,9 @@ $users->addToDefaultGroup($user);
3047

3148
### Deleting Users
3249

33-
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.
50+
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.
51+
52+
You can delete a user like any other entity.
3453

3554
```php
3655
// Get the User Provider (UserModel by default)

src/Commands/Setup.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -256,10 +256,8 @@ private function setAutoloadHelpers(): void
256256
$helpers = $config->helpers;
257257
$newHelpers = array_unique(array_merge($helpers, ['auth', 'setting']));
258258

259-
$pattern = '/^ public \$helpers = \[.*\];/mu';
260-
$replace = ' public $helpers = [\'' . implode("', '", $newHelpers) . '\'];';
261259
$content = file_get_contents($path);
262-
$output = preg_replace($pattern, $replace, $content);
260+
$output = $this->updateAutoloadHelpers($content, $newHelpers);
263261

264262
// check if the content is updated
265263
if ($output === $content) {
@@ -277,6 +275,18 @@ private function setAutoloadHelpers(): void
277275
}
278276
}
279277

278+
/**
279+
* @param string $content The content of Config\Autoload.
280+
* @param list<string> $newHelpers The list of helpers.
281+
*/
282+
private function updateAutoloadHelpers(string $content, array $newHelpers): string
283+
{
284+
$pattern = '/^ public \$helpers = \[.*?\];/msu';
285+
$replace = ' public $helpers = [\'' . implode("', '", $newHelpers) . '\'];';
286+
287+
return preg_replace($pattern, $replace, $content);
288+
}
289+
280290
private function removeHelperLoadingInBaseController(): void
281291
{
282292
$file = 'Controllers/BaseController.php';

src/Language/de/Auth.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,16 @@
3030
'throttled' => 'Es wurden zu viele Anfragen von dieser IP-Adresse gestellt. Sie können es in {0} Sekunden erneut versuchen.',
3131
'notEnoughPrivilege' => 'Sie haben nicht die erforderliche Berechtigung, um den gewünschten Vorgang auszuführen.',
3232
// JWT Exceptions
33-
'invalidJWT' => '(To be translated) The token is invalid.',
34-
'expiredJWT' => '(To be translated) The token has expired.',
35-
'beforeValidJWT' => '(To be translated) The token is not yet available.',
33+
'invalidJWT' => 'Der Token ist ungültig.',
34+
'expiredJWT' => 'Der Token ist abgelaufen.',
35+
'beforeValidJWT' => 'Der Token ist noch nicht verfügbar.',
3636

3737
'email' => 'E-Mail-Adresse',
3838
'username' => 'Benutzername',
3939
'password' => 'Passwort',
4040
'passwordConfirm' => 'Passwort (erneut)',
4141
'haveAccount' => 'Haben Sie bereits ein Konto?',
42-
'token' => '(To be translated) Token',
42+
'token' => 'Token',
4343

4444
// Buttons
4545
'confirm' => 'Bestätigen',
@@ -61,7 +61,7 @@
6161
'magicLinkExpired' => 'Sorry, der Link ist abgelaufen.',
6262
'checkYourEmail' => 'Prüfen Sie Ihre E-Mail!',
6363
'magicLinkDetails' => 'Wir haben Ihnen gerade eine E-Mail mit einem Login-Link geschickt. Er ist nur für {0} Minuten gültig.',
64-
'magicLinkDisabled' => '(To be translated) Use of MagicLink is currently not allowed.',
64+
'magicLinkDisabled' => 'Die Verwendung von MagicLink ist derzeit nicht erlaubt.',
6565
'successLogout' => 'Sie haben sich erfolgreich abgemeldet.',
6666
'backToLogin' => 'Zurück zur Anmeldung',
6767

tests/Commands/SetupTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,41 @@ public function testRunEmailConfigIsFine(): void
131131
);
132132
}
133133

134+
public function testUpdateAutoloadHelpers(): void
135+
{
136+
$command = new Setup(Services::logger(), Services::commands());
137+
138+
$updateAutoloadHelpers = $this->getPrivateMethodInvoker($command, 'updateAutoloadHelpers');
139+
140+
$content = <<<'EOL'
141+
class Autoload extends AutoloadConfig
142+
{
143+
/**
144+
* -------------------------------------------------------------------
145+
* Helpers
146+
* -------------------------------------------------------------------
147+
* Prototype:
148+
* $helpers = [
149+
* 'form',
150+
* ];
151+
*
152+
* @var list<string>
153+
*/
154+
public $helpers = [
155+
'text',
156+
'form',
157+
];
158+
}
159+
EOL;
160+
$helpers = ['text', 'form', 'auth', 'setting'];
161+
$output = $updateAutoloadHelpers($content, $helpers);
162+
163+
$this->assertStringContainsString(
164+
"public \$helpers = ['text', 'form', 'auth', 'setting'];",
165+
$output
166+
);
167+
}
168+
134169
/**
135170
* @return string app folder path
136171
*/

tests/Language/GermanTranslationTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,7 @@
1818
*/
1919
final class GermanTranslationTest extends AbstractTranslationTestCase
2020
{
21+
protected array $excludedLocaleKeyTranslations = [
22+
'Auth.token',
23+
];
2124
}

0 commit comments

Comments
 (0)