Skip to content

Commit 6197194

Browse files
authored
Merge pull request #633 from kenjis/feat-rename-table-names-wo-constant
feat: customize name of Shield Tables w/o constant
2 parents f13ce6d + 1e67351 commit 6197194

25 files changed

+378
-201
lines changed

docs/customization.md

Lines changed: 77 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,40 @@
11
# Customizing Shield
22

3-
- [Customizing Shield](#customizing-shield)
4-
- [Route Configuration](#route-configuration)
5-
- [Custom Redirect URLs](#custom-redirect-urls)
6-
- [Customize Login Redirect](#customize-login-redirect)
7-
- [Customize Register Redirect](#customize-register-redirect)
8-
- [Customize Logout Redirect](#customize-logout-redirect)
9-
- [Extending the Controllers](#extending-the-controllers)
10-
- [Integrating Custom View Libraries](#integrating-custom-view-libraries)
11-
- [Custom Validation Rules](#custom-validation-rules)
12-
- [Registration](#registration)
13-
- [Login](#login)
14-
- [Custom User Provider](#custom-user-provider)
15-
- [Custom Login Identifier](#custom-login-identifier)
3+
- [Customizing Shield](#customizing-shield)
4+
- [Custom Table Names](#custom-table-names)
5+
- [Route Configuration](#route-configuration)
6+
- [Custom Redirect URLs](#custom-redirect-urls)
7+
- [Customize Login Redirect](#customize-login-redirect)
8+
- [Customize Register Redirect](#customize-register-redirect)
9+
- [Customize Logout Redirect](#customize-logout-redirect)
10+
- [Extending the Controllers](#extending-the-controllers)
11+
- [Integrating Custom View Libraries](#integrating-custom-view-libraries)
12+
- [Custom Validation Rules](#custom-validation-rules)
13+
- [Registration](#registration)
14+
- [Login](#login)
15+
- [Custom User Provider](#custom-user-provider)
16+
- [Custom Login Identifier](#custom-login-identifier)
17+
18+
## Custom Table Names
19+
20+
If you want to change the default table names, you can change the table names
21+
in **app/Config/Auth.php**.
22+
23+
```php
24+
public array $tables = [
25+
'users' => 'users',
26+
'identities' => 'auth_identities',
27+
'logins' => 'auth_logins',
28+
'token_logins' => 'auth_token_logins',
29+
'remember_tokens' => 'auth_remember_tokens',
30+
'groups_users' => 'auth_groups_users',
31+
'permissions_users' => 'auth_permissions_users',
32+
];
33+
```
34+
35+
Set the table names that you want in the array values.
36+
37+
> **Note** You must change the table names before running database migrations.
1638
1739
## Route Configuration
1840

@@ -149,24 +171,40 @@ Shield has the following rules for registration:
149171
```php
150172
[
151173
'username' => [
152-
'label' => 'Auth.username',
153-
'rules' => 'required|max_length[30]|min_length[3]|regex_match[/\A[a-zA-Z0-9\.]+\z/]|is_unique[users.username]',
174+
'label' => 'Auth.username',
175+
'rules' => [
176+
'required',
177+
'max_length[30]',
178+
'min_length[3]',
179+
'regex_match[/\A[a-zA-Z0-9\.]+\z/]',
180+
'is_unique[users.username]',
181+
],
154182
],
155183
'email' => [
156-
'label' => 'Auth.email',
157-
'rules' => 'required|max_length[254]|valid_email|is_unique[auth_identities.secret]',
184+
'label' => 'Auth.email',
185+
'rules' => [
186+
'required',
187+
'max_length[254]',
188+
'valid_email',
189+
'is_unique[auth_identities.secret]',
190+
],
158191
],
159192
'password' => [
160-
'label' => 'Auth.password',
193+
'label' => 'Auth.password',
161194
'rules' => 'required|strong_password',
162195
],
163196
'password_confirm' => [
164-
'label' => 'Auth.passwordConfirm',
197+
'label' => 'Auth.passwordConfirm',
165198
'rules' => 'required|matches[password]',
166199
],
167200
];
168201
```
169202

203+
> **Note** If you customize the table names, the table names
204+
> (`users` and `auth_identities`) in the above rules will be automatically
205+
> changed. The rules are implemented in
206+
> `RegisterController::getValidationRules()`.
207+
170208
If you need a different set of rules for registration, you can specify them in your `Validation` configuration (**app/Config/Validation.php**) like:
171209

172210
```php
@@ -175,24 +213,38 @@ If you need a different set of rules for registration, you can specify them in y
175213
//--------------------------------------------------------------------
176214
public $registration = [
177215
'username' => [
178-
'label' => 'Auth.username',
179-
'rules' => 'required|max_length[30]|min_length[3]|regex_match[/\A[a-zA-Z0-9\.]+\z/]|is_unique[users.username]',
216+
'label' => 'Auth.username',
217+
'rules' => [
218+
'required',
219+
'max_length[30]',
220+
'min_length[3]',
221+
'regex_match[/\A[a-zA-Z0-9\.]+\z/]',
222+
'is_unique[users.username]',
223+
],
180224
],
181225
'email' => [
182-
'label' => 'Auth.email',
183-
'rules' => 'required|max_length[254]|valid_email|is_unique[auth_identities.secret]',
226+
'label' => 'Auth.email',
227+
'rules' => [
228+
'required',
229+
'max_length[254]',
230+
'valid_email',
231+
'is_unique[auth_identities.secret]',
232+
],
184233
],
185234
'password' => [
186-
'label' => 'Auth.password',
235+
'label' => 'Auth.password',
187236
'rules' => 'required|strong_password',
188237
],
189238
'password_confirm' => [
190-
'label' => 'Auth.passwordConfirm',
239+
'label' => 'Auth.passwordConfirm',
191240
'rules' => 'required|matches[password]',
192241
],
193242
];
194243
```
195244

245+
> **Note** If you customize the table names, set the correct table names in the
246+
> rules.
247+
196248
### Login
197249

198250
Similar to the process for validation rules in the **Registration** section, you can add rules for the login form to **app/Config/Validation.php** and change the rules.

docs/install.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ Require it with an explicit version constraint allowing its desired stability.
7474
php spark shield:setup
7575
```
7676

77+
> **Note** If you want to customize table names, you must change the table names
78+
> before running database migrations.
79+
> See [Customizing Shield](./customization.md#custom-table-names).
80+
7781
2. Configure **app/Config/Email.php** to allow Shield to send emails with the [Email Class](https://codeigniter.com/user_guide/libraries/email.html).
7882

7983
```php
@@ -145,6 +149,10 @@ your project.
145149

146150
5. **Migration** Run the migrations.
147151

152+
> **Note** If you want to customize table names, you must change the table names
153+
> before running database migrations.
154+
> See [Customizing Shield](./customization.md#custom-table-names).
155+
148156
```console
149157
php spark migrate --all
150158
```
@@ -278,4 +286,4 @@ public $globals = [
278286
]
279287
]
280288
```
281-
The same should apply for the Rate Limiting and Forcing Password Reset.
289+
The same should apply for the Rate Limiting and Forcing Password Reset.

src/Config/Auth.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,36 @@ class Auth extends BaseConfig
3737
'magic-link-email' => '\CodeIgniter\Shield\Views\Email\magic_link_email',
3838
];
3939

40+
/**
41+
* --------------------------------------------------------------------
42+
* Customize Name of Shield Tables
43+
* --------------------------------------------------------------------
44+
* Only change if you want to rename the default Shield table names
45+
*
46+
* It may be necessary to change the names of the tables for
47+
* security reasons, to prevent the conflict of table names,
48+
* the internal policy of the companies or any other reason.
49+
*
50+
* - users Auth Users Table, the users info is stored.
51+
* - auth_identities Auth Identities Table, Used for storage of passwords, access tokens, social login identities, etc.
52+
* - auth_logins Auth Login Attempts, Table records login attempts.
53+
* - auth_token_logins Auth Token Login Attempts Table, Records Bearer Token type login attempts.
54+
* - auth_remember_tokens Auth Remember Tokens (remember-me) Table.
55+
* - auth_groups_users Groups Users Table.
56+
* - auth_permissions_users Users Permissions Table.
57+
*
58+
* @var array<string, string>
59+
*/
60+
public array $tables = [
61+
'users' => 'users',
62+
'identities' => 'auth_identities',
63+
'logins' => 'auth_logins',
64+
'token_logins' => 'auth_token_logins',
65+
'remember_tokens' => 'auth_remember_tokens',
66+
'groups_users' => 'auth_groups_users',
67+
'permissions_users' => 'auth_permissions_users',
68+
];
69+
4070
/**
4171
* --------------------------------------------------------------------
4272
* Redirect URLs

src/Controllers/RegisterController.php

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,15 @@
77
use App\Controllers\BaseController;
88
use CodeIgniter\Events\Events;
99
use CodeIgniter\HTTP\RedirectResponse;
10+
use CodeIgniter\HTTP\RequestInterface;
11+
use CodeIgniter\HTTP\ResponseInterface;
1012
use CodeIgniter\Shield\Authentication\Authenticators\Session;
13+
use CodeIgniter\Shield\Config\Auth;
1114
use CodeIgniter\Shield\Entities\User;
1215
use CodeIgniter\Shield\Exceptions\ValidationException;
1316
use CodeIgniter\Shield\Models\UserModel;
1417
use CodeIgniter\Shield\Traits\Viewable;
18+
use Psr\Log\LoggerInterface;
1519

1620
/**
1721
* Class RegisterController
@@ -25,6 +29,27 @@ class RegisterController extends BaseController
2529

2630
protected $helpers = ['setting'];
2731

32+
/**
33+
* Auth Table names
34+
*/
35+
private array $tables;
36+
37+
public function initController(
38+
RequestInterface $request,
39+
ResponseInterface $response,
40+
LoggerInterface $logger
41+
): void {
42+
parent::initController(
43+
$request,
44+
$response,
45+
$logger
46+
);
47+
48+
/** @var Auth $authConfig */
49+
$authConfig = config('Auth');
50+
$this->tables = $authConfig->tables;
51+
}
52+
2853
/**
2954
* Displays the registration form.
3055
*
@@ -153,11 +178,11 @@ protected function getValidationRules(): array
153178
{
154179
$registrationUsernameRules = array_merge(
155180
config('AuthSession')->usernameValidationRules,
156-
['is_unique[users.username]']
181+
[sprintf('is_unique[%s.username]', $this->tables['users'])]
157182
);
158183
$registrationEmailRules = array_merge(
159184
config('AuthSession')->emailValidationRules,
160-
['is_unique[auth_identities.secret]']
185+
[sprintf('is_unique[%s.secret]', $this->tables['identities'])]
161186
);
162187

163188
return setting('Validation.registration') ?? [

src/Database/Migrations/2020-12-28-223112_create_auth_tables.php

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,26 @@
44

55
namespace CodeIgniter\Shield\Database\Migrations;
66

7+
use CodeIgniter\Database\Forge;
78
use CodeIgniter\Database\Migration;
9+
use CodeIgniter\Shield\Config\Auth;
810

911
class CreateAuthTables extends Migration
1012
{
13+
/**
14+
* Auth Table names
15+
*/
16+
private array $tables;
17+
18+
public function __construct(?Forge $forge = null)
19+
{
20+
parent::__construct($forge);
21+
22+
/** @var Auth $authConfig */
23+
$authConfig = config('Auth');
24+
$this->tables = $authConfig->tables;
25+
}
26+
1127
public function up(): void
1228
{
1329
// Users Table
@@ -24,7 +40,7 @@ public function up(): void
2440
]);
2541
$this->forge->addPrimaryKey('id');
2642
$this->forge->addUniqueKey('username');
27-
$this->forge->createTable('users');
43+
$this->forge->createTable($this->tables['users']);
2844

2945
/*
3046
* Auth Identities Table
@@ -47,8 +63,8 @@ public function up(): void
4763
$this->forge->addPrimaryKey('id');
4864
$this->forge->addUniqueKey(['type', 'secret']);
4965
$this->forge->addKey('user_id');
50-
$this->forge->addForeignKey('user_id', 'users', 'id', '', 'CASCADE');
51-
$this->forge->createTable('auth_identities');
66+
$this->forge->addForeignKey('user_id', $this->tables['users'], 'id', '', 'CASCADE');
67+
$this->forge->createTable($this->tables['identities']);
5268

5369
/**
5470
* Auth Login Attempts Table
@@ -69,7 +85,7 @@ public function up(): void
6985
$this->forge->addKey(['id_type', 'identifier']);
7086
$this->forge->addKey('user_id');
7187
// NOTE: Do NOT delete the user_id or identifier when the user is deleted for security audits
72-
$this->forge->createTable('auth_logins');
88+
$this->forge->createTable($this->tables['logins']);
7389

7490
/*
7591
* Auth Token Login Attempts Table
@@ -89,7 +105,7 @@ public function up(): void
89105
$this->forge->addKey(['id_type', 'identifier']);
90106
$this->forge->addKey('user_id');
91107
// NOTE: Do NOT delete the user_id or identifier when the user is deleted for security audits
92-
$this->forge->createTable('auth_token_logins');
108+
$this->forge->createTable($this->tables['token_logins']);
93109

94110
/*
95111
* Auth Remember Tokens (remember-me) Table
@@ -106,8 +122,8 @@ public function up(): void
106122
]);
107123
$this->forge->addPrimaryKey('id');
108124
$this->forge->addUniqueKey('selector');
109-
$this->forge->addForeignKey('user_id', 'users', 'id', '', 'CASCADE');
110-
$this->forge->createTable('auth_remember_tokens');
125+
$this->forge->addForeignKey('user_id', $this->tables['users'], 'id', '', 'CASCADE');
126+
$this->forge->createTable($this->tables['remember_tokens']);
111127

112128
// Groups Users Table
113129
$this->forge->addField([
@@ -117,8 +133,8 @@ public function up(): void
117133
'created_at' => ['type' => 'datetime', 'null' => false],
118134
]);
119135
$this->forge->addPrimaryKey('id');
120-
$this->forge->addForeignKey('user_id', 'users', 'id', '', 'CASCADE');
121-
$this->forge->createTable('auth_groups_users');
136+
$this->forge->addForeignKey('user_id', $this->tables['users'], 'id', '', 'CASCADE');
137+
$this->forge->createTable($this->tables['groups_users']);
122138

123139
// Users Permissions Table
124140
$this->forge->addField([
@@ -128,8 +144,8 @@ public function up(): void
128144
'created_at' => ['type' => 'datetime', 'null' => false],
129145
]);
130146
$this->forge->addPrimaryKey('id');
131-
$this->forge->addForeignKey('user_id', 'users', 'id', '', 'CASCADE');
132-
$this->forge->createTable('auth_permissions_users');
147+
$this->forge->addForeignKey('user_id', $this->tables['users'], 'id', '', 'CASCADE');
148+
$this->forge->createTable($this->tables['permissions_users']);
133149
}
134150

135151
// --------------------------------------------------------------------
@@ -138,13 +154,13 @@ public function down(): void
138154
{
139155
$this->db->disableForeignKeyChecks();
140156

141-
$this->forge->dropTable('auth_logins', true);
142-
$this->forge->dropTable('auth_token_logins', true);
143-
$this->forge->dropTable('auth_remember_tokens', true);
144-
$this->forge->dropTable('auth_identities', true);
145-
$this->forge->dropTable('auth_groups_users', true);
146-
$this->forge->dropTable('auth_permissions_users', true);
147-
$this->forge->dropTable('users', true);
157+
$this->forge->dropTable($this->tables['logins'], true);
158+
$this->forge->dropTable($this->tables['token_logins'], true);
159+
$this->forge->dropTable($this->tables['remember_tokens'], true);
160+
$this->forge->dropTable($this->tables['identities'], true);
161+
$this->forge->dropTable($this->tables['groups_users'], true);
162+
$this->forge->dropTable($this->tables['permissions_users'], true);
163+
$this->forge->dropTable($this->tables['users'], true);
148164

149165
$this->db->enableForeignKeyChecks();
150166
}

0 commit comments

Comments
 (0)