Skip to content

Commit 60c3554

Browse files
committed
refactor: remove SHIELD_TABLES
1 parent 07c85e1 commit 60c3554

22 files changed

Lines changed: 240 additions & 150 deletions

src/Config/Constants.php

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/Config/Registrar.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
use CodeIgniter\Shield\Filters\SessionAuth;
1515
use CodeIgniter\Shield\Filters\TokenAuth;
1616

17-
include_once __DIR__ . '/Constants.php';
18-
1917
class Registrar
2018
{
2119
/**

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-
[sprintf('is_unique[%s.username]', SHIELD_TABLES['users'])]
181+
[sprintf('is_unique[%s.username]', $this->tables['users'])]
157182
);
158183
$registrationEmailRules = array_merge(
159184
config('AuthSession')->emailValidationRules,
160-
[sprintf('is_unique[%s.secret]', SHIELD_TABLES['identities'])]
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(SHIELD_TABLES['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', SHIELD_TABLES['users'], 'id', '', 'CASCADE');
51-
$this->forge->createTable(SHIELD_TABLES['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(SHIELD_TABLES['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(SHIELD_TABLES['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', SHIELD_TABLES['users'], 'id', '', 'CASCADE');
110-
$this->forge->createTable(SHIELD_TABLES['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', SHIELD_TABLES['users'], 'id', '', 'CASCADE');
121-
$this->forge->createTable(SHIELD_TABLES['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', SHIELD_TABLES['users'], 'id', '', 'CASCADE');
132-
$this->forge->createTable(SHIELD_TABLES['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(SHIELD_TABLES['logins'], true);
142-
$this->forge->dropTable(SHIELD_TABLES['token_logins'], true);
143-
$this->forge->dropTable(SHIELD_TABLES['remember_tokens'], true);
144-
$this->forge->dropTable(SHIELD_TABLES['identities'], true);
145-
$this->forge->dropTable(SHIELD_TABLES['groups_users'], true);
146-
$this->forge->dropTable(SHIELD_TABLES['permissions_users'], true);
147-
$this->forge->dropTable(SHIELD_TABLES['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
}

src/Models/GroupModel.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
namespace CodeIgniter\Shield\Models;
66

77
use CodeIgniter\Model;
8+
use CodeIgniter\Shield\Config\Auth;
89
use CodeIgniter\Shield\Entities\User;
910

1011
class GroupModel extends Model
1112
{
1213
use CheckQueryReturnTrait;
1314

14-
protected $table = SHIELD_TABLES['groups_users'];
1515
protected $primaryKey = 'id';
1616
protected $returnType = 'array';
1717
protected $useSoftDeletes = false;
@@ -25,6 +25,14 @@ class GroupModel extends Model
2525
protected $validationMessages = [];
2626
protected $skipValidation = false;
2727

28+
public function initialize(): void
29+
{
30+
/** @var Auth $authConfig */
31+
$authConfig = config('Auth');
32+
33+
$this->table = $authConfig->tables['groups_users'];
34+
}
35+
2836
public function getForUser(User $user): array
2937
{
3038
$rows = $this->builder()

src/Models/LoginModel.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use CodeIgniter\I18n\Time;
88
use CodeIgniter\Model;
99
use CodeIgniter\Shield\Authentication\Authenticators\Session;
10+
use CodeIgniter\Shield\Config\Auth;
1011
use CodeIgniter\Shield\Entities\Login;
1112
use CodeIgniter\Shield\Entities\User;
1213
use Faker\Generator;
@@ -15,7 +16,6 @@ class LoginModel extends Model
1516
{
1617
use CheckQueryReturnTrait;
1718

18-
protected $table = SHIELD_TABLES['logins'];
1919
protected $primaryKey = 'id';
2020
protected $returnType = Login::class;
2121
protected $useSoftDeletes = false;
@@ -40,6 +40,14 @@ class LoginModel extends Model
4040
protected $validationMessages = [];
4141
protected $skipValidation = false;
4242

43+
public function initialize(): void
44+
{
45+
/** @var Auth $authConfig */
46+
$authConfig = config('Auth');
47+
48+
$this->table = $authConfig->tables['logins'];
49+
}
50+
4351
/**
4452
* Records login attempt.
4553
*

src/Models/PermissionModel.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
namespace CodeIgniter\Shield\Models;
66

77
use CodeIgniter\Model;
8+
use CodeIgniter\Shield\Config\Auth;
89
use CodeIgniter\Shield\Entities\User;
910

1011
class PermissionModel extends Model
1112
{
1213
use CheckQueryReturnTrait;
1314

14-
protected $table = SHIELD_TABLES['permissions_users'];
1515
protected $primaryKey = 'id';
1616
protected $returnType = 'array';
1717
protected $useSoftDeletes = false;
@@ -25,6 +25,14 @@ class PermissionModel extends Model
2525
protected $validationMessages = [];
2626
protected $skipValidation = false;
2727

28+
public function initialize(): void
29+
{
30+
/** @var Auth $authConfig */
31+
$authConfig = config('Auth');
32+
33+
$this->table = $authConfig->tables['permissions_users'];
34+
}
35+
2836
public function getForUser(User $user): array
2937
{
3038
$rows = $this->builder()

src/Models/RememberModel.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use CodeIgniter\I18n\Time;
88
use CodeIgniter\Model;
9+
use CodeIgniter\Shield\Config\Auth;
910
use CodeIgniter\Shield\Entities\User;
1011
use DateTime;
1112
use Faker\Generator;
@@ -15,7 +16,6 @@ class RememberModel extends Model
1516
{
1617
use CheckQueryReturnTrait;
1718

18-
protected $table = SHIELD_TABLES['remember_tokens'];
1919
protected $primaryKey = 'id';
2020
protected $returnType = 'object';
2121
protected $useSoftDeletes = false;
@@ -27,6 +27,14 @@ class RememberModel extends Model
2727
];
2828
protected $useTimestamps = true;
2929

30+
public function initialize(): void
31+
{
32+
/** @var Auth $authConfig */
33+
$authConfig = config('Auth');
34+
35+
$this->table = $authConfig->tables['remember_tokens'];
36+
}
37+
3038
public function fake(Generator &$faker): stdClass
3139
{
3240
return (object) [

src/Models/TokenLoginModel.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,19 @@
55
namespace CodeIgniter\Shield\Models;
66

77
use CodeIgniter\I18n\Time;
8+
use CodeIgniter\Shield\Config\Auth;
89
use CodeIgniter\Shield\Entities\Login;
910
use Faker\Generator;
1011

1112
class TokenLoginModel extends LoginModel
1213
{
13-
protected $table = SHIELD_TABLES['token_logins'];
14+
public function initialize(): void
15+
{
16+
/** @var Auth $authConfig */
17+
$authConfig = config('Auth');
18+
19+
$this->table = $authConfig->tables['token_logins'];
20+
}
1421

1522
/**
1623
* Generate a fake login for testing

src/Models/UserIdentityModel.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use CodeIgniter\Shield\Authentication\Authenticators\AccessTokens;
1010
use CodeIgniter\Shield\Authentication\Authenticators\Session;
1111
use CodeIgniter\Shield\Authentication\Passwords;
12+
use CodeIgniter\Shield\Config\Auth;
1213
use CodeIgniter\Shield\Entities\AccessToken;
1314
use CodeIgniter\Shield\Entities\User;
1415
use CodeIgniter\Shield\Entities\UserIdentity;
@@ -20,7 +21,6 @@ class UserIdentityModel extends Model
2021
{
2122
use CheckQueryReturnTrait;
2223

23-
protected $table = SHIELD_TABLES['identities'];
2424
protected $primaryKey = 'id';
2525
protected $returnType = UserIdentity::class;
2626
protected $useSoftDeletes = false;
@@ -37,6 +37,14 @@ class UserIdentityModel extends Model
3737
];
3838
protected $useTimestamps = true;
3939

40+
public function initialize(): void
41+
{
42+
/** @var Auth $authConfig */
43+
$authConfig = config('Auth');
44+
45+
$this->table = $authConfig->tables['identities'];
46+
}
47+
4048
/**
4149
* Inserts a record
4250
*

0 commit comments

Comments
 (0)