From 049bffd527da70d1ea75920306a06adb353ed617 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Dadashi Date: Mon, 6 Feb 2023 11:32:35 +0330 Subject: [PATCH 01/15] feat: add `$authTables` for customize shield table names --- src/Config/Auth.php | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/Config/Auth.php b/src/Config/Auth.php index 3f05045c7..05513060a 100644 --- a/src/Config/Auth.php +++ b/src/Config/Auth.php @@ -37,6 +37,36 @@ class Auth extends BaseConfig 'magic-link-email' => '\CodeIgniter\Shield\Views\Email\magic_link_email', ]; + /** + * -------------------------------------------------------------------- + * Customize Name of Shield Tables + * -------------------------------------------------------------------- + * Only change if you want to rename the default Shield table names + * + * It may be necessary to change the names of the tables for + * security reasons, to prevent the conflict of table names, + * the internal policy of the companies or any other reason. + * + * - users Auth Users Table, the users info is stored. + * - auth_identities Auth Identities Table, Used for storage of passwords, access tokens, social login identities, etc. + * - auth_logins Auth Login Attempts, Table records login attempts. + * - auth_token_logins Auth Token Login Attempts Table, Records Bearer Token type login attempts. + * - auth_remember_tokens Auth Remember Tokens (remember-me) Table. + * - auth_groups_users Groups Users Table. + * - auth_permissions_users Users Permissions Table. + * + * @var array + */ + public array $authTables = [ + 'users' => 'users', + 'auth_identities' => 'auth_identities', + 'auth_logins' => 'auth_logins', + 'auth_token_logins' => 'auth_token_logins', + 'auth_remember_tokens' => 'auth_remember_tokens', + 'auth_groups_users' => 'auth_groups_users', + 'auth_permissions_users' => 'auth_permissions_users', + ]; + /** * -------------------------------------------------------------------- * Redirect URLs From b49c1b3cb25f507642a982ed56acb3b9db3c67dd Mon Sep 17 00:00:00 2001 From: Pooya Parsa Dadashi Date: Mon, 6 Feb 2023 11:52:25 +0330 Subject: [PATCH 02/15] add: include `Constants.php` file for load --- src/Config/Registrar.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Config/Registrar.php b/src/Config/Registrar.php index d3bcf8672..10e2cd50b 100644 --- a/src/Config/Registrar.php +++ b/src/Config/Registrar.php @@ -14,6 +14,8 @@ use CodeIgniter\Shield\Filters\SessionAuth; use CodeIgniter\Shield\Filters\TokenAuth; +include_once __DIR__ . '/Constants.php'; + class Registrar { /** From eca017a0d0e5f6ec077cbba025709b0244af8e0d Mon Sep 17 00:00:00 2001 From: Pooya Parsa Dadashi Date: Mon, 6 Feb 2023 11:54:08 +0330 Subject: [PATCH 03/15] add: `Constants.php` file for easy access in all files. --- src/Config/Constants.php | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 src/Config/Constants.php diff --git a/src/Config/Constants.php b/src/Config/Constants.php new file mode 100644 index 000000000..0a2c6425e --- /dev/null +++ b/src/Config/Constants.php @@ -0,0 +1,8 @@ +authTables); From 9c061ee03794bc8ab0d5ff355bb1de5c3f720316 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Dadashi Date: Mon, 6 Feb 2023 19:09:30 +0330 Subject: [PATCH 04/15] replace `users` with `SHIELD_TABLES['users']` --- docs/customization.md | 4 ++-- src/Controllers/RegisterController.php | 2 +- .../2020-12-28-223112_create_auth_tables.php | 12 ++++++------ src/Models/UserModel.php | 8 ++++---- tests/Controllers/ActionsTest.php | 2 +- tests/Controllers/RegisterTest.php | 4 ++-- tests/Unit/UserModelTest.php | 12 ++++++------ tests/Unit/UserTest.php | 8 ++++---- 8 files changed, 26 insertions(+), 26 deletions(-) diff --git a/docs/customization.md b/docs/customization.md index 10e6a1da7..23a31307c 100644 --- a/docs/customization.md +++ b/docs/customization.md @@ -149,7 +149,7 @@ Shield has the following rules for registration: [ 'username' => [ 'label' => 'Auth.username', - 'rules' => 'required|max_length[30]|min_length[3]|regex_match[/\A[a-zA-Z0-9\.]+\z/]|is_unique[users.username]', + 'rules' => 'required|max_length[30]|min_length[3]|regex_match[/\A[a-zA-Z0-9\.]+\z/]|is_unique[' . SHIELD_TABLES['users'] . '.username]', ], 'email' => [ 'label' => 'Auth.email', @@ -175,7 +175,7 @@ If you need a different set of rules for registration, you can specify them in y public $registration = [ 'username' => [ 'label' => 'Auth.username', - 'rules' => 'required|max_length[30]|min_length[3]|regex_match[/\A[a-zA-Z0-9\.]+\z/]|is_unique[users.username]', + 'rules' => 'required|max_length[30]|min_length[3]|regex_match[/\A[a-zA-Z0-9\.]+\z/]|is_unique[' . SHIELD_TABLES['users'] . '.username]', ], 'email' => [ 'label' => 'Auth.email', diff --git a/src/Controllers/RegisterController.php b/src/Controllers/RegisterController.php index 784f785bd..dc7f071b9 100644 --- a/src/Controllers/RegisterController.php +++ b/src/Controllers/RegisterController.php @@ -153,7 +153,7 @@ protected function getValidationRules(): array { $registrationUsernameRules = array_merge( config('AuthSession')->usernameValidationRules, - ['is_unique[users.username]'] + [sprintf('is_unique[%s.username]', SHIELD_TABLES['users'])] ); $registrationEmailRules = array_merge( config('AuthSession')->emailValidationRules, diff --git a/src/Database/Migrations/2020-12-28-223112_create_auth_tables.php b/src/Database/Migrations/2020-12-28-223112_create_auth_tables.php index f44eb41df..555bbf64a 100644 --- a/src/Database/Migrations/2020-12-28-223112_create_auth_tables.php +++ b/src/Database/Migrations/2020-12-28-223112_create_auth_tables.php @@ -24,7 +24,7 @@ public function up(): void ]); $this->forge->addPrimaryKey('id'); $this->forge->addUniqueKey('username'); - $this->forge->createTable('users'); + $this->forge->createTable(SHIELD_TABLES['users']); /* * Auth Identities Table @@ -47,7 +47,7 @@ public function up(): void $this->forge->addPrimaryKey('id'); $this->forge->addUniqueKey(['type', 'secret']); $this->forge->addKey('user_id'); - $this->forge->addForeignKey('user_id', 'users', 'id', '', 'CASCADE'); + $this->forge->addForeignKey('user_id', SHIELD_TABLES['users'], 'id', '', 'CASCADE'); $this->forge->createTable('auth_identities'); /** @@ -106,7 +106,7 @@ public function up(): void ]); $this->forge->addPrimaryKey('id'); $this->forge->addUniqueKey('selector'); - $this->forge->addForeignKey('user_id', 'users', 'id', '', 'CASCADE'); + $this->forge->addForeignKey('user_id', SHIELD_TABLES['users'], 'id', '', 'CASCADE'); $this->forge->createTable('auth_remember_tokens'); // Groups Users Table @@ -117,7 +117,7 @@ public function up(): void 'created_at' => ['type' => 'datetime', 'null' => false], ]); $this->forge->addPrimaryKey('id'); - $this->forge->addForeignKey('user_id', 'users', 'id', '', 'CASCADE'); + $this->forge->addForeignKey('user_id', SHIELD_TABLES['users'], 'id', '', 'CASCADE'); $this->forge->createTable('auth_groups_users'); // Users Permissions Table @@ -128,7 +128,7 @@ public function up(): void 'created_at' => ['type' => 'datetime', 'null' => false], ]); $this->forge->addPrimaryKey('id'); - $this->forge->addForeignKey('user_id', 'users', 'id', '', 'CASCADE'); + $this->forge->addForeignKey('user_id', SHIELD_TABLES['users'], 'id', '', 'CASCADE'); $this->forge->createTable('auth_permissions_users'); } @@ -144,7 +144,7 @@ public function down(): void $this->forge->dropTable('auth_identities', true); $this->forge->dropTable('auth_groups_users', true); $this->forge->dropTable('auth_permissions_users', true); - $this->forge->dropTable('users', true); + $this->forge->dropTable(SHIELD_TABLES['users'], true); $this->db->enableForeignKeyChecks(); } diff --git a/src/Models/UserModel.php b/src/Models/UserModel.php index a1da4cf06..4a5bd3db5 100644 --- a/src/Models/UserModel.php +++ b/src/Models/UserModel.php @@ -21,7 +21,7 @@ class UserModel extends Model { use CheckQueryReturnTrait; - protected $table = 'users'; + protected $table = SHIELD_TABLES['users']; protected $primaryKey = 'id'; protected $returnType = User::class; protected $useSoftDeletes = true; @@ -184,16 +184,16 @@ public function findByCredentials(array $credentials): ?User // any of the credentials used should be case-insensitive foreach ($credentials as $key => $value) { $this->where( - 'LOWER(' . $this->db->protectIdentifiers("users.{$key}") . ')', + 'LOWER(' . $this->db->protectIdentifiers(SHIELD_TABLES['users'] . ".{$key}") . ')', strtolower($value) ); } if ($email !== null) { $data = $this->select( - 'users.*, auth_identities.secret as email, auth_identities.secret2 as password_hash' + sprintf('%1$s.*, auth_identities.secret as email, auth_identities.secret2 as password_hash', SHIELD_TABLES['users']) ) - ->join('auth_identities', 'auth_identities.user_id = users.id') + ->join('auth_identities', 'auth_identities.user_id = %1$s.id', SHIELD_TABLES['users']) ->where('auth_identities.type', Session::ID_TYPE_EMAIL_PASSWORD) ->where( 'LOWER(' . $this->db->protectIdentifiers('auth_identities.secret') . ')', diff --git a/tests/Controllers/ActionsTest.php b/tests/Controllers/ActionsTest.php index 77b22dc99..26396934b 100644 --- a/tests/Controllers/ActionsTest.php +++ b/tests/Controllers/ActionsTest.php @@ -268,7 +268,7 @@ public function testEmailActivateVerify(): void $result->assertSessionMissing('auth_action'); // User should have been set as active - $this->seeInDatabase('users', [ + $this->seeInDatabase(SHIELD_TABLES['users'], [ 'id' => $this->user->id, 'active' => 1, ]); diff --git a/tests/Controllers/RegisterTest.php b/tests/Controllers/RegisterTest.php index 3ef6cce9b..a6e38473c 100644 --- a/tests/Controllers/RegisterTest.php +++ b/tests/Controllers/RegisterTest.php @@ -60,7 +60,7 @@ public function testRegisterActionSuccess(): void $this->assertSame(site_url(), $result->getRedirectUrl()); // User saved to DB - $this->seeInDatabase('users', [ + $this->seeInDatabase(SHIELD_TABLES['users'], [ 'username' => 'JohnDoe', ]); @@ -141,7 +141,7 @@ public function testRegisterRedirectsToActionIfDefined(): void $result->assertRedirectTo('/auth/a/show'); // Should NOT have activated the user - $this->seeInDatabase('users', [ + $this->seeInDatabase(SHIELD_TABLES['users'], [ 'username' => 'foo', 'active' => 0, ]); diff --git a/tests/Unit/UserModelTest.php b/tests/Unit/UserModelTest.php index 65eee5b27..d242db617 100644 --- a/tests/Unit/UserModelTest.php +++ b/tests/Unit/UserModelTest.php @@ -39,7 +39,7 @@ public function testSaveInsertUser(): void 'user_id' => $user->id, 'secret' => 'foo@bar.com', ]); - $this->seeInDatabase('users', [ + $this->seeInDatabase(SHIELD_TABLES['users'], [ 'id' => $user->id, 'active' => 0, ]); @@ -74,7 +74,7 @@ public function testInsertUserObject(): void 'user_id' => $user->id, 'secret' => 'foo@bar.com', ]); - $this->seeInDatabase('users', [ + $this->seeInDatabase(SHIELD_TABLES['users'], [ 'id' => $user->id, 'active' => 0, ]); @@ -120,7 +120,7 @@ public function testInsertUserArray(): void 'user_id' => $id, 'secret' => 'foo@bar.com', ]); - $this->seeInDatabase('users', [ + $this->seeInDatabase(SHIELD_TABLES['users'], [ 'id' => $id, 'active' => 0, ]); @@ -155,7 +155,7 @@ public function testSaveUpdateUserObjectWithUserDataToUpdate(): void 'user_id' => $user->id, 'secret' => 'bar@bar.com', ]); - $this->seeInDatabase('users', [ + $this->seeInDatabase(SHIELD_TABLES['users'], [ 'id' => $user->id, 'active' => 1, ]); @@ -179,7 +179,7 @@ public function testUpdateUserObjectWithUserDataToUpdate(): void 'user_id' => $user->id, 'secret' => 'bar@bar.com', ]); - $this->seeInDatabase('users', [ + $this->seeInDatabase(SHIELD_TABLES['users'], [ 'id' => $user->id, 'active' => 1, ]); @@ -215,7 +215,7 @@ public function testUpdateUserArrayWithUserDataToUpdate(): void 'user_id' => $user->id, 'secret' => 'bar@bar.com', ]); - $this->seeInDatabase('users', [ + $this->seeInDatabase(SHIELD_TABLES['users'], [ 'id' => $user->id, 'active' => 1, ]); diff --git a/tests/Unit/UserTest.php b/tests/Unit/UserTest.php index 311105db7..52956faf7 100644 --- a/tests/Unit/UserTest.php +++ b/tests/Unit/UserTest.php @@ -263,7 +263,7 @@ public function testActivate(): void $this->user->active = false; model(UserModel::class)->save($this->user); - $this->seeInDatabase('users', [ + $this->seeInDatabase(SHIELD_TABLES['users'], [ 'id' => $this->user->id, 'active' => 0, ]); @@ -274,7 +274,7 @@ public function testActivate(): void $this->user = model(UserModel::class)->find($this->user->id); $this->assertTrue($this->user->active); - $this->seeInDatabase('users', [ + $this->seeInDatabase(SHIELD_TABLES['users'], [ 'id' => $this->user->id, 'active' => 1, ]); @@ -285,7 +285,7 @@ public function testDeactivate(): void $this->user->active = true; model(UserModel::class)->save($this->user); - $this->seeInDatabase('users', [ + $this->seeInDatabase(SHIELD_TABLES['users'], [ 'id' => $this->user->id, 'active' => 1, ]); @@ -296,7 +296,7 @@ public function testDeactivate(): void $this->user = model(UserModel::class)->find($this->user->id); $this->assertFalse($this->user->active); - $this->seeInDatabase('users', [ + $this->seeInDatabase(SHIELD_TABLES['users'], [ 'id' => $this->user->id, 'active' => 0, ]); From 98afe42bc6d5069a5529028bb8fae286dea03c78 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Dadashi Date: Mon, 6 Feb 2023 19:39:48 +0330 Subject: [PATCH 05/15] replaced `auth_identities` with `SHIELD_TABLES['auth_identities']` --- docs/customization.md | 4 ++-- src/Controllers/RegisterController.php | 2 +- .../2020-12-28-223112_create_auth_tables.php | 4 ++-- src/Models/UserIdentityModel.php | 2 +- src/Models/UserModel.php | 8 ++++---- .../AccessTokenAuthenticatorTest.php | 2 +- .../Authenticators/SessionAuthenticatorTest.php | 2 +- tests/Authentication/HasAccessTokensTest.php | 2 +- tests/Authentication/MagicLinkTest.php | 2 +- tests/Controllers/ActionsTest.php | 6 +++--- tests/Controllers/RegisterTest.php | 2 +- tests/Unit/UserModelTest.php | 16 ++++++++-------- tests/Unit/UserTest.php | 4 ++-- 13 files changed, 28 insertions(+), 28 deletions(-) diff --git a/docs/customization.md b/docs/customization.md index 23a31307c..cf10747a9 100644 --- a/docs/customization.md +++ b/docs/customization.md @@ -153,7 +153,7 @@ Shield has the following rules for registration: ], 'email' => [ 'label' => 'Auth.email', - 'rules' => 'required|max_length[254]|valid_email|is_unique[auth_identities.secret]', + 'rules' => 'required|max_length[254]|valid_email|is_unique[' . SHIELD_TABLES['auth_identities'] . '.secret]', ], 'password' => [ 'label' => 'Auth.password', @@ -179,7 +179,7 @@ If you need a different set of rules for registration, you can specify them in y ], 'email' => [ 'label' => 'Auth.email', - 'rules' => 'required|max_length[254]|valid_email|is_unique[auth_identities.secret]', + 'rules' => 'required|max_length[254]|valid_email|is_unique[' . SHIELD_TABLES['auth_identities'] . '.secret]', ], 'password' => [ 'label' => 'Auth.password', diff --git a/src/Controllers/RegisterController.php b/src/Controllers/RegisterController.php index dc7f071b9..bfeb91185 100644 --- a/src/Controllers/RegisterController.php +++ b/src/Controllers/RegisterController.php @@ -157,7 +157,7 @@ protected function getValidationRules(): array ); $registrationEmailRules = array_merge( config('AuthSession')->emailValidationRules, - ['is_unique[auth_identities.secret]'] + [sprintf('is_unique[%s.secret]', SHIELD_TABLES['auth_identities'])] ); return setting('Validation.registration') ?? [ diff --git a/src/Database/Migrations/2020-12-28-223112_create_auth_tables.php b/src/Database/Migrations/2020-12-28-223112_create_auth_tables.php index 555bbf64a..c2cc89d8e 100644 --- a/src/Database/Migrations/2020-12-28-223112_create_auth_tables.php +++ b/src/Database/Migrations/2020-12-28-223112_create_auth_tables.php @@ -48,7 +48,7 @@ public function up(): void $this->forge->addUniqueKey(['type', 'secret']); $this->forge->addKey('user_id'); $this->forge->addForeignKey('user_id', SHIELD_TABLES['users'], 'id', '', 'CASCADE'); - $this->forge->createTable('auth_identities'); + $this->forge->createTable(SHIELD_TABLES['auth_identities']); /** * Auth Login Attempts Table @@ -141,7 +141,7 @@ public function down(): void $this->forge->dropTable('auth_logins', true); $this->forge->dropTable('auth_token_logins', true); $this->forge->dropTable('auth_remember_tokens', true); - $this->forge->dropTable('auth_identities', true); + $this->forge->dropTable(SHIELD_TABLES['auth_identities'], true); $this->forge->dropTable('auth_groups_users', true); $this->forge->dropTable('auth_permissions_users', true); $this->forge->dropTable(SHIELD_TABLES['users'], true); diff --git a/src/Models/UserIdentityModel.php b/src/Models/UserIdentityModel.php index 1877235a0..1b1da2fb7 100644 --- a/src/Models/UserIdentityModel.php +++ b/src/Models/UserIdentityModel.php @@ -20,7 +20,7 @@ class UserIdentityModel extends Model { use CheckQueryReturnTrait; - protected $table = 'auth_identities'; + protected $table = SHIELD_TABLES['auth_identities']; protected $primaryKey = 'id'; protected $returnType = UserIdentity::class; protected $useSoftDeletes = false; diff --git a/src/Models/UserModel.php b/src/Models/UserModel.php index 4a5bd3db5..301f58def 100644 --- a/src/Models/UserModel.php +++ b/src/Models/UserModel.php @@ -191,12 +191,12 @@ public function findByCredentials(array $credentials): ?User if ($email !== null) { $data = $this->select( - sprintf('%1$s.*, auth_identities.secret as email, auth_identities.secret2 as password_hash', SHIELD_TABLES['users']) + sprintf('%1$s.*, %2$s.secret as email, %2$s.secret2 as password_hash', SHIELD_TABLES['users'], SHIELD_TABLES['auth_identities']) ) - ->join('auth_identities', 'auth_identities.user_id = %1$s.id', SHIELD_TABLES['users']) - ->where('auth_identities.type', Session::ID_TYPE_EMAIL_PASSWORD) + ->join(SHIELD_TABLES['auth_identities'], sprintf('%1$s.user_id = %2$s.id', SHIELD_TABLES['auth_identities'], SHIELD_TABLES['users'])) + ->where(SHIELD_TABLES['auth_identities'].'.type', Session::ID_TYPE_EMAIL_PASSWORD) ->where( - 'LOWER(' . $this->db->protectIdentifiers('auth_identities.secret') . ')', + 'LOWER(' . $this->db->protectIdentifiers(SHIELD_TABLES['auth_identities'].'.secret') . ')', strtolower($email) ) ->asArray() diff --git a/tests/Authentication/Authenticators/AccessTokenAuthenticatorTest.php b/tests/Authentication/Authenticators/AccessTokenAuthenticatorTest.php index c829295dc..a8ba4c8a0 100644 --- a/tests/Authentication/Authenticators/AccessTokenAuthenticatorTest.php +++ b/tests/Authentication/Authenticators/AccessTokenAuthenticatorTest.php @@ -143,7 +143,7 @@ public function testCheckSuccess(): void $user = fake(UserModel::class); $token = $user->generateAccessToken('foo'); - $this->seeInDatabase('auth_identities', [ + $this->seeInDatabase(SHIELD_TABLES['auth_identities'], [ 'user_id' => $user->id, 'type' => 'access_token', 'last_used_at' => null, diff --git a/tests/Authentication/Authenticators/SessionAuthenticatorTest.php b/tests/Authentication/Authenticators/SessionAuthenticatorTest.php index e3d87d623..a87ed1a17 100644 --- a/tests/Authentication/Authenticators/SessionAuthenticatorTest.php +++ b/tests/Authentication/Authenticators/SessionAuthenticatorTest.php @@ -46,7 +46,7 @@ protected function setUp(): void $this->events = new MockEvents(); Services::injectMock('events', $this->events); - $this->db->table('auth_identities')->truncate(); + $this->db->table(SHIELD_TABLES['auth_identities'])->truncate(); } public function testLoggedInFalse(): void diff --git a/tests/Authentication/HasAccessTokensTest.php b/tests/Authentication/HasAccessTokensTest.php index 4d0025fe4..223bdf3f2 100644 --- a/tests/Authentication/HasAccessTokensTest.php +++ b/tests/Authentication/HasAccessTokensTest.php @@ -22,7 +22,7 @@ protected function setUp(): void parent::setUp(); $this->user = fake(UserModel::class); - $this->db->table('auth_identities')->truncate(); + $this->db->table(SHIELD_TABLES['auth_identities'])->truncate(); } public function testGenerateToken(): void diff --git a/tests/Authentication/MagicLinkTest.php b/tests/Authentication/MagicLinkTest.php index 2be743ac7..e2b31b548 100644 --- a/tests/Authentication/MagicLinkTest.php +++ b/tests/Authentication/MagicLinkTest.php @@ -81,7 +81,7 @@ public function testMagicLinkSubmitSuccess(): void $result->assertOK(); $result->assertSee(lang('Auth.checkYourEmail')); - $this->seeInDatabase('auth_identities', [ + $this->seeInDatabase(SHIELD_TABLES['auth_identities'], [ 'user_id' => $user->id, 'type' => Session::ID_TYPE_MAGIC_LINK, ]); diff --git a/tests/Controllers/ActionsTest.php b/tests/Controllers/ActionsTest.php index 26396934b..341cdc47f 100644 --- a/tests/Controllers/ActionsTest.php +++ b/tests/Controllers/ActionsTest.php @@ -155,7 +155,7 @@ public function testEmail2FAVerify(): void $this->assertSame(site_url(), $result->getRedirectUrl()); // Identity should have been removed - $this->dontSeeInDatabase('auth_identities', [ + $this->dontSeeInDatabase(SHIELD_TABLES['auth_identities'], [ 'user_id' => $this->user->id, 'type' => Session::ID_TYPE_EMAIL_2FA, ]); @@ -174,7 +174,7 @@ public function testShowEmail2FACreatesIdentity(): void $result->assertOK(); - $this->seeInDatabase('auth_identities', [ + $this->seeInDatabase(SHIELD_TABLES['auth_identities'], [ 'user_id' => $this->user->id, 'type' => Session::ID_TYPE_EMAIL_2FA, 'name' => 'login', @@ -259,7 +259,7 @@ public function testEmailActivateVerify(): void $this->assertSame(site_url(), $result->getRedirectUrl()); // Identity should have been removed - $this->dontSeeInDatabase('auth_identities', [ + $this->dontSeeInDatabase(SHIELD_TABLES['auth_identities'], [ 'user_id' => $this->user->id, 'type' => Session::ID_TYPE_EMAIL_2FA, ]); diff --git a/tests/Controllers/RegisterTest.php b/tests/Controllers/RegisterTest.php index a6e38473c..4fc1602b2 100644 --- a/tests/Controllers/RegisterTest.php +++ b/tests/Controllers/RegisterTest.php @@ -68,7 +68,7 @@ public function testRegisterActionSuccess(): void /** @var User $user */ $user = model(UserModel::class)->where('username', 'JohnDoe')->first(); - $this->seeInDatabase('auth_identities', [ + $this->seeInDatabase(SHIELD_TABLES['auth_identities'], [ 'user_id' => $user->id, 'type' => Session::ID_TYPE_EMAIL_PASSWORD, 'secret' => 'john.doe@example.com', diff --git a/tests/Unit/UserModelTest.php b/tests/Unit/UserModelTest.php index d242db617..e5232b364 100644 --- a/tests/Unit/UserModelTest.php +++ b/tests/Unit/UserModelTest.php @@ -35,7 +35,7 @@ public function testSaveInsertUser(): void $users->save($user); $user = $users->findByCredentials(['email' => 'foo@bar.com']); - $this->seeInDatabase('auth_identities', [ + $this->seeInDatabase(SHIELD_TABLES['auth_identities'], [ 'user_id' => $user->id, 'secret' => 'foo@bar.com', ]); @@ -70,7 +70,7 @@ public function testInsertUserObject(): void $users->insert($user); $user = $users->findByCredentials(['email' => 'foo@bar.com']); - $this->seeInDatabase('auth_identities', [ + $this->seeInDatabase(SHIELD_TABLES['auth_identities'], [ 'user_id' => $user->id, 'secret' => 'foo@bar.com', ]); @@ -116,7 +116,7 @@ public function testInsertUserArray(): void $id = $users->insert($userArray); - $this->dontSeeInDatabase('auth_identities', [ + $this->dontSeeInDatabase(SHIELD_TABLES['auth_identities'], [ 'user_id' => $id, 'secret' => 'foo@bar.com', ]); @@ -151,7 +151,7 @@ public function testSaveUpdateUserObjectWithUserDataToUpdate(): void $users->save($user); - $this->seeInDatabase('auth_identities', [ + $this->seeInDatabase(SHIELD_TABLES['auth_identities'], [ 'user_id' => $user->id, 'secret' => 'bar@bar.com', ]); @@ -175,7 +175,7 @@ public function testUpdateUserObjectWithUserDataToUpdate(): void $users->update($user->id, $user); - $this->seeInDatabase('auth_identities', [ + $this->seeInDatabase(SHIELD_TABLES['auth_identities'], [ 'user_id' => $user->id, 'secret' => 'bar@bar.com', ]); @@ -211,7 +211,7 @@ public function testUpdateUserArrayWithUserDataToUpdate(): void $users->update($user->id, $userArray); - $this->dontSeeInDatabase('auth_identities', [ + $this->dontSeeInDatabase(SHIELD_TABLES['auth_identities'], [ 'user_id' => $user->id, 'secret' => 'bar@bar.com', ]); @@ -233,7 +233,7 @@ public function testSaveUpdateUserObjectWithoutUserDataToUpdate(): void $users->save($user); - $this->seeInDatabase('auth_identities', [ + $this->seeInDatabase(SHIELD_TABLES['auth_identities'], [ 'user_id' => $user->id, 'secret' => 'bar@bar.com', ]); @@ -251,7 +251,7 @@ public function testUpdateUserObjectWithoutUserDataToUpdate(): void $users->update(null, $user); - $this->seeInDatabase('auth_identities', [ + $this->seeInDatabase(SHIELD_TABLES['auth_identities'], [ 'user_id' => $user->id, 'secret' => 'bar@bar.com', ]); diff --git a/tests/Unit/UserTest.php b/tests/Unit/UserTest.php index 52956faf7..468ebcb95 100644 --- a/tests/Unit/UserTest.php +++ b/tests/Unit/UserTest.php @@ -184,7 +184,7 @@ public function testUpdateEmail(): void $user = $users->find($this->user->id); - $this->seeInDatabase('auth_identities', [ + $this->seeInDatabase(SHIELD_TABLES['auth_identities'], [ 'user_id' => $user->id, 'secret' => 'foo@bar.com', ]); @@ -225,7 +225,7 @@ public function testUpdatePasswordHash(): void $user = $users->find($this->user->id); - $this->seeInDatabase('auth_identities', [ + $this->seeInDatabase(SHIELD_TABLES['auth_identities'], [ 'user_id' => $user->id, 'secret' => 'foo@bar.com', 'secret2' => $hash, From 67899ffb569388ccbce99a245df89ecc17f4e801 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Dadashi Date: Mon, 6 Feb 2023 19:45:31 +0330 Subject: [PATCH 06/15] replaced `auth_logins` with `SHIELD_TABLES['auth_logins']` --- .../Migrations/2020-12-28-223112_create_auth_tables.php | 4 ++-- src/Models/LoginModel.php | 2 +- .../Authenticators/SessionAuthenticatorTest.php | 8 ++++---- tests/Controllers/LoginTest.php | 6 +++--- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/Database/Migrations/2020-12-28-223112_create_auth_tables.php b/src/Database/Migrations/2020-12-28-223112_create_auth_tables.php index c2cc89d8e..16aad9760 100644 --- a/src/Database/Migrations/2020-12-28-223112_create_auth_tables.php +++ b/src/Database/Migrations/2020-12-28-223112_create_auth_tables.php @@ -69,7 +69,7 @@ public function up(): void $this->forge->addKey(['id_type', 'identifier']); $this->forge->addKey('user_id'); // NOTE: Do NOT delete the user_id or identifier when the user is deleted for security audits - $this->forge->createTable('auth_logins'); + $this->forge->createTable(SHIELD_TABLES['auth_logins']); /* * Auth Token Login Attempts Table @@ -138,7 +138,7 @@ public function down(): void { $this->db->disableForeignKeyChecks(); - $this->forge->dropTable('auth_logins', true); + $this->forge->dropTable(SHIELD_TABLES['auth_logins'], true); $this->forge->dropTable('auth_token_logins', true); $this->forge->dropTable('auth_remember_tokens', true); $this->forge->dropTable(SHIELD_TABLES['auth_identities'], true); diff --git a/src/Models/LoginModel.php b/src/Models/LoginModel.php index d30981fb0..a9b2740e0 100644 --- a/src/Models/LoginModel.php +++ b/src/Models/LoginModel.php @@ -15,7 +15,7 @@ class LoginModel extends Model { use CheckQueryReturnTrait; - protected $table = 'auth_logins'; + protected $table = SHIELD_TABLES['auth_logins']; protected $primaryKey = 'id'; protected $returnType = Login::class; protected $useSoftDeletes = false; diff --git a/tests/Authentication/Authenticators/SessionAuthenticatorTest.php b/tests/Authentication/Authenticators/SessionAuthenticatorTest.php index a87ed1a17..7243ac562 100644 --- a/tests/Authentication/Authenticators/SessionAuthenticatorTest.php +++ b/tests/Authentication/Authenticators/SessionAuthenticatorTest.php @@ -316,7 +316,7 @@ public function testAttemptCannotFindUser(): void $this->assertSame(lang('Auth.badAttempt'), $result->reason()); // A login attempt should have always been recorded - $this->seeInDatabase('auth_logins', [ + $this->seeInDatabase(SHIELD_TABLES['auth_logins'], [ 'identifier' => 'johnsmith@example.com', 'success' => 0, ]); @@ -346,7 +346,7 @@ public function testAttemptSuccess(): void $this->assertSame($this->user->id, $_SESSION['user']['id']); // A login attempt should have been recorded - $this->seeInDatabase('auth_logins', [ + $this->seeInDatabase(SHIELD_TABLES['auth_logins'], [ 'identifier' => $this->user->email, 'success' => 1, ]); @@ -396,7 +396,7 @@ public function testAttemptCaseInsensitive(): void $this->assertSame($this->user->id, $_SESSION['user']['id']); // A login attempt should have been recorded - $this->seeInDatabase('auth_logins', [ + $this->seeInDatabase(SHIELD_TABLES['auth_logins'], [ 'identifier' => 'foo@example.COM', 'success' => 1, ]); @@ -428,7 +428,7 @@ public function testAttemptUsernameOnly(): void $this->assertSame($user->id, $_SESSION['user']['id']); // A login attempt should have been recorded - $this->seeInDatabase('auth_logins', [ + $this->seeInDatabase(SHIELD_TABLES['auth_logins'], [ 'identifier' => 'fooROG', 'success' => 1, ]); diff --git a/tests/Controllers/LoginTest.php b/tests/Controllers/LoginTest.php index 2ffe35341..46434c878 100644 --- a/tests/Controllers/LoginTest.php +++ b/tests/Controllers/LoginTest.php @@ -53,7 +53,7 @@ public function testLoginBadEmail(): void $this->assertSame(site_url('/login'), $result->getRedirectUrl()); // Login should have been recorded successfully - $this->seeInDatabase('auth_logins', [ + $this->seeInDatabase(SHIELD_TABLES['auth_logins'], [ 'identifier' => 'fooled@example.com', 'user_id' => null, 'success' => 0, @@ -87,7 +87,7 @@ public function testLoginActionEmailSuccess(): void $this->assertSame(site_url(), $result->getRedirectUrl()); // Login should have been recorded successfully - $this->seeInDatabase('auth_logins', [ + $this->seeInDatabase(SHIELD_TABLES['auth_logins'], [ 'identifier' => 'foo@example.com', 'user_id' => $this->user->id, 'success' => 1, @@ -159,7 +159,7 @@ public function testLoginActionUsernameSuccess(): void $this->assertSame(site_url(), $result->getRedirectUrl()); // Login should have been recorded successfully - $this->seeInDatabase('auth_logins', [ + $this->seeInDatabase(SHIELD_TABLES['auth_logins'], [ 'identifier' => $this->user->username, 'user_id' => $this->user->id, 'success' => 1, From 21789c52b189e5751c5b715ecf973c17d412a347 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Dadashi Date: Mon, 6 Feb 2023 19:48:16 +0330 Subject: [PATCH 07/15] replaced `auth_token_logins` with `SHIELD_TABLES['auth_token_logins']` --- .../Migrations/2020-12-28-223112_create_auth_tables.php | 4 ++-- src/Models/TokenLoginModel.php | 2 +- .../Authenticators/AccessTokenAuthenticatorTest.php | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Database/Migrations/2020-12-28-223112_create_auth_tables.php b/src/Database/Migrations/2020-12-28-223112_create_auth_tables.php index 16aad9760..514dd6c0a 100644 --- a/src/Database/Migrations/2020-12-28-223112_create_auth_tables.php +++ b/src/Database/Migrations/2020-12-28-223112_create_auth_tables.php @@ -89,7 +89,7 @@ public function up(): void $this->forge->addKey(['id_type', 'identifier']); $this->forge->addKey('user_id'); // NOTE: Do NOT delete the user_id or identifier when the user is deleted for security audits - $this->forge->createTable('auth_token_logins'); + $this->forge->createTable(SHIELD_TABLES['auth_token_logins']); /* * Auth Remember Tokens (remember-me) Table @@ -139,7 +139,7 @@ public function down(): void $this->db->disableForeignKeyChecks(); $this->forge->dropTable(SHIELD_TABLES['auth_logins'], true); - $this->forge->dropTable('auth_token_logins', true); + $this->forge->dropTable(SHIELD_TABLES['auth_token_logins'], true); $this->forge->dropTable('auth_remember_tokens', true); $this->forge->dropTable(SHIELD_TABLES['auth_identities'], true); $this->forge->dropTable('auth_groups_users', true); diff --git a/src/Models/TokenLoginModel.php b/src/Models/TokenLoginModel.php index a6234bb38..8dfc6d544 100644 --- a/src/Models/TokenLoginModel.php +++ b/src/Models/TokenLoginModel.php @@ -10,7 +10,7 @@ class TokenLoginModel extends LoginModel { - protected $table = 'auth_token_logins'; + protected $table = SHIELD_TABLES['auth_token_logins']; /** * Generate a fake login for testing diff --git a/tests/Authentication/Authenticators/AccessTokenAuthenticatorTest.php b/tests/Authentication/Authenticators/AccessTokenAuthenticatorTest.php index a8ba4c8a0..91255e106 100644 --- a/tests/Authentication/Authenticators/AccessTokenAuthenticatorTest.php +++ b/tests/Authentication/Authenticators/AccessTokenAuthenticatorTest.php @@ -201,7 +201,7 @@ public function testAttemptSuccess(): void $this->assertSame($token->token, $foundUser->currentAccessToken()->token); // A login attempt should have been recorded - $this->seeInDatabase('auth_token_logins', [ + $this->seeInDatabase(SHIELD_TABLES['auth_token_logins'], [ 'id_type' => AccessTokens::ID_TYPE_ACCESS_TOKEN, 'identifier' => $token->raw_token, 'success' => 1, From 1731bad0a02d272a7f121407e20fe6114628fe93 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Dadashi Date: Mon, 6 Feb 2023 19:53:13 +0330 Subject: [PATCH 08/15] replaced `auth_remember_tokens` with `SHIELD_TABLES['auth_remember_tokens']` --- .../2020-12-28-223112_create_auth_tables.php | 4 ++-- src/Models/RememberModel.php | 2 +- .../SessionAuthenticatorTest.php | 20 +++++++++---------- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/Database/Migrations/2020-12-28-223112_create_auth_tables.php b/src/Database/Migrations/2020-12-28-223112_create_auth_tables.php index 514dd6c0a..313f585b7 100644 --- a/src/Database/Migrations/2020-12-28-223112_create_auth_tables.php +++ b/src/Database/Migrations/2020-12-28-223112_create_auth_tables.php @@ -107,7 +107,7 @@ public function up(): void $this->forge->addPrimaryKey('id'); $this->forge->addUniqueKey('selector'); $this->forge->addForeignKey('user_id', SHIELD_TABLES['users'], 'id', '', 'CASCADE'); - $this->forge->createTable('auth_remember_tokens'); + $this->forge->createTable(SHIELD_TABLES['auth_remember_tokens']); // Groups Users Table $this->forge->addField([ @@ -140,7 +140,7 @@ public function down(): void $this->forge->dropTable(SHIELD_TABLES['auth_logins'], true); $this->forge->dropTable(SHIELD_TABLES['auth_token_logins'], true); - $this->forge->dropTable('auth_remember_tokens', true); + $this->forge->dropTable(SHIELD_TABLES['auth_remember_tokens'], true); $this->forge->dropTable(SHIELD_TABLES['auth_identities'], true); $this->forge->dropTable('auth_groups_users', true); $this->forge->dropTable('auth_permissions_users', true); diff --git a/src/Models/RememberModel.php b/src/Models/RememberModel.php index ea855cbaa..95c91a45b 100644 --- a/src/Models/RememberModel.php +++ b/src/Models/RememberModel.php @@ -15,7 +15,7 @@ class RememberModel extends Model { use CheckQueryReturnTrait; - protected $table = 'auth_remember_tokens'; + protected $table = SHIELD_TABLES['auth_remember_tokens']; protected $primaryKey = 'id'; protected $returnType = 'object'; protected $useSoftDeletes = false; diff --git a/tests/Authentication/Authenticators/SessionAuthenticatorTest.php b/tests/Authentication/Authenticators/SessionAuthenticatorTest.php index 7243ac562..768f98adf 100644 --- a/tests/Authentication/Authenticators/SessionAuthenticatorTest.php +++ b/tests/Authentication/Authenticators/SessionAuthenticatorTest.php @@ -147,7 +147,7 @@ public function testLoginNoRemember(): void $this->assertSame($this->user->id, $_SESSION['user']['id']); - $this->dontSeeInDatabase('auth_remember_tokens', [ + $this->dontSeeInDatabase(SHIELD_TABLES['auth_remember_tokens'], [ 'user_id' => $this->user->id, ]); } @@ -160,7 +160,7 @@ public function testLoginWithRemember(): void $this->assertSame($this->user->id, $_SESSION['user']['id']); - $this->seeInDatabase('auth_remember_tokens', [ + $this->seeInDatabase(SHIELD_TABLES['auth_remember_tokens'], [ 'user_id' => $this->user->id, ]); @@ -174,12 +174,12 @@ public function testLogout(): void $this->user->createEmailIdentity(['email' => 'foo@example.com', 'password' => 'secret']); $this->auth->remember()->login($this->user); - $this->seeInDatabase('auth_remember_tokens', ['user_id' => $this->user->id]); + $this->seeInDatabase(SHIELD_TABLES['auth_remember_tokens'], ['user_id' => $this->user->id]); $this->auth->logout(); $this->assertArrayNotHasKey('user', $_SESSION); - $this->dontSeeInDatabase('auth_remember_tokens', ['user_id' => $this->user->id]); + $this->dontSeeInDatabase(SHIELD_TABLES['auth_remember_tokens'], ['user_id' => $this->user->id]); } public function testLogoutOnlyLogoutCalled(): void @@ -207,7 +207,7 @@ public function testLoginById(): void $this->assertSame($this->user->id, $_SESSION['user']['id']); - $this->dontSeeInDatabase('auth_remember_tokens', ['user_id' => $this->user->id]); + $this->dontSeeInDatabase(SHIELD_TABLES['auth_remember_tokens'], ['user_id' => $this->user->id]); } public function testLoginByIdRemember(): void @@ -218,7 +218,7 @@ public function testLoginByIdRemember(): void $this->assertSame($this->user->id, $_SESSION['user']['id']); - $this->seeInDatabase('auth_remember_tokens', ['user_id' => $this->user->id]); + $this->seeInDatabase(SHIELD_TABLES['auth_remember_tokens'], ['user_id' => $this->user->id]); } public function testForgetCurrentUser(): void @@ -227,22 +227,22 @@ public function testForgetCurrentUser(): void $this->auth->remember()->loginById($this->user->id); $this->assertSame($this->user->id, $_SESSION['user']['id']); - $this->seeInDatabase('auth_remember_tokens', ['user_id' => $this->user->id]); + $this->seeInDatabase(SHIELD_TABLES['auth_remember_tokens'], ['user_id' => $this->user->id]); $this->auth->forget(); - $this->dontSeeInDatabase('auth_remember_tokens', ['user_id' => $this->user->id]); + $this->dontSeeInDatabase(SHIELD_TABLES['auth_remember_tokens'], ['user_id' => $this->user->id]); } public function testForgetAnotherUser(): void { fake(RememberModel::class, ['user_id' => $this->user->id]); - $this->seeInDatabase('auth_remember_tokens', ['user_id' => $this->user->id]); + $this->seeInDatabase(SHIELD_TABLES['auth_remember_tokens'], ['user_id' => $this->user->id]); $this->auth->forget($this->user); - $this->dontSeeInDatabase('auth_remember_tokens', ['user_id' => $this->user->id]); + $this->dontSeeInDatabase(SHIELD_TABLES['auth_remember_tokens'], ['user_id' => $this->user->id]); } public function testCheckNoPassword(): void From 9e794f5f72f81bcfb311163858d76e486ec5c68e Mon Sep 17 00:00:00 2001 From: Pooya Parsa Dadashi Date: Mon, 6 Feb 2023 19:56:47 +0330 Subject: [PATCH 09/15] replaced `auth_groups_users` with `SHIELD_TABLES['auth_groups_users']` --- .../2020-12-28-223112_create_auth_tables.php | 4 +-- src/Models/GroupModel.php | 2 +- tests/Authorization/AuthorizableTest.php | 34 +++++++++---------- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/Database/Migrations/2020-12-28-223112_create_auth_tables.php b/src/Database/Migrations/2020-12-28-223112_create_auth_tables.php index 313f585b7..bfb3fa540 100644 --- a/src/Database/Migrations/2020-12-28-223112_create_auth_tables.php +++ b/src/Database/Migrations/2020-12-28-223112_create_auth_tables.php @@ -118,7 +118,7 @@ public function up(): void ]); $this->forge->addPrimaryKey('id'); $this->forge->addForeignKey('user_id', SHIELD_TABLES['users'], 'id', '', 'CASCADE'); - $this->forge->createTable('auth_groups_users'); + $this->forge->createTable(SHIELD_TABLES['auth_groups_users']); // Users Permissions Table $this->forge->addField([ @@ -142,7 +142,7 @@ public function down(): void $this->forge->dropTable(SHIELD_TABLES['auth_token_logins'], true); $this->forge->dropTable(SHIELD_TABLES['auth_remember_tokens'], true); $this->forge->dropTable(SHIELD_TABLES['auth_identities'], true); - $this->forge->dropTable('auth_groups_users', true); + $this->forge->dropTable(SHIELD_TABLES['auth_groups_users'], true); $this->forge->dropTable('auth_permissions_users', true); $this->forge->dropTable(SHIELD_TABLES['users'], true); diff --git a/src/Models/GroupModel.php b/src/Models/GroupModel.php index 63b426f3c..053dceed5 100644 --- a/src/Models/GroupModel.php +++ b/src/Models/GroupModel.php @@ -11,7 +11,7 @@ class GroupModel extends Model { use CheckQueryReturnTrait; - protected $table = 'auth_groups_users'; + protected $table = SHIELD_TABLES['auth_groups_users']; protected $primaryKey = 'id'; protected $returnType = 'array'; protected $useSoftDeletes = false; diff --git a/tests/Authorization/AuthorizableTest.php b/tests/Authorization/AuthorizableTest.php index 7f8c2f502..c84e369af 100644 --- a/tests/Authorization/AuthorizableTest.php +++ b/tests/Authorization/AuthorizableTest.php @@ -29,7 +29,7 @@ protected function setUp(): void parent::setUp(); // Refresh should take care of this.... - db_connect()->table('auth_groups_users')->truncate(); + db_connect()->table(SHIELD_TABLES['auth_groups_users'])->truncate(); db_connect()->table('auth_permissions_users')->truncate(); } @@ -39,11 +39,11 @@ public function testAddGroupWithNoExistingGroups(): void // Make sure it doesn't record duplicates $this->user->addGroup('admin', 'beta'); - $this->seeInDatabase('auth_groups_users', [ + $this->seeInDatabase(SHIELD_TABLES['auth_groups_users'], [ 'user_id' => $this->user->id, 'group' => 'admin', ]); - $this->seeInDatabase('auth_groups_users', [ + $this->seeInDatabase(SHIELD_TABLES['auth_groups_users'], [ 'user_id' => $this->user->id, 'group' => 'beta', ]); @@ -55,12 +55,12 @@ public function testAddGroupWithNoExistingGroups(): void public function testAddGroupWithExistingGroups(): void { - $this->hasInDatabase('auth_groups_users', [ + $this->hasInDatabase(SHIELD_TABLES['auth_groups_users'], [ 'user_id' => $this->user->id, 'group' => 'admin', 'created_at' => Time::now()->toDateTimeString(), ]); - $this->hasInDatabase('auth_groups_users', [ + $this->hasInDatabase(SHIELD_TABLES['auth_groups_users'], [ 'user_id' => $this->user->id, 'group' => 'superadmin', 'created_at' => Time::now()->toDateTimeString(), @@ -70,15 +70,15 @@ public function testAddGroupWithExistingGroups(): void // Make sure it doesn't record duplicates $this->user->addGroup('admin', 'beta'); - $this->seeInDatabase('auth_groups_users', [ + $this->seeInDatabase(SHIELD_TABLES['auth_groups_users'], [ 'user_id' => $this->user->id, 'group' => 'admin', ]); - $this->seeInDatabase('auth_groups_users', [ + $this->seeInDatabase(SHIELD_TABLES['auth_groups_users'], [ 'user_id' => $this->user->id, 'group' => 'superadmin', ]); - $this->seeInDatabase('auth_groups_users', [ + $this->seeInDatabase(SHIELD_TABLES['auth_groups_users'], [ 'user_id' => $this->user->id, 'group' => 'beta', ]); @@ -102,14 +102,14 @@ public function testRemoveGroupNoGroups(): void public function testRemoveGroupExistingGroup(): void { - $this->hasInDatabase('auth_groups_users', [ + $this->hasInDatabase(SHIELD_TABLES['auth_groups_users'], [ 'user_id' => $this->user->id, 'group' => 'admin', 'created_at' => Time::now()->toDateTimeString(), ]); $otherUser = fake(UserModel::class); - $this->hasInDatabase('auth_groups_users', [ + $this->hasInDatabase(SHIELD_TABLES['auth_groups_users'], [ 'user_id' => $otherUser->id, 'group' => 'admin', 'created_at' => Time::now()->toDateTimeString(), @@ -117,13 +117,13 @@ public function testRemoveGroupExistingGroup(): void $this->user->removeGroup('admin'); $this->assertEmpty($this->user->getGroups()); - $this->dontSeeInDatabase('auth_groups_users', [ + $this->dontSeeInDatabase(SHIELD_TABLES['auth_groups_users'], [ 'user_id' => $this->user->id, 'group' => 'admin', ]); // Make sure we didn't delete the group from anyone else - $this->seeInDatabase('auth_groups_users', [ + $this->seeInDatabase(SHIELD_TABLES['auth_groups_users'], [ 'user_id' => $otherUser->id, 'group' => 'admin', ]); @@ -131,12 +131,12 @@ public function testRemoveGroupExistingGroup(): void public function testSyncGroups(): void { - $this->hasInDatabase('auth_groups_users', [ + $this->hasInDatabase(SHIELD_TABLES['auth_groups_users'], [ 'user_id' => $this->user->id, 'group' => 'admin', 'created_at' => Time::now()->toDateTimeString(), ]); - $this->hasInDatabase('auth_groups_users', [ + $this->hasInDatabase(SHIELD_TABLES['auth_groups_users'], [ 'user_id' => $this->user->id, 'group' => 'superadmin', 'created_at' => Time::now()->toDateTimeString(), @@ -144,11 +144,11 @@ public function testSyncGroups(): void $this->user->syncGroups('admin', 'beta'); $this->assertSame(['admin', 'beta'], $this->user->getGroups()); - $this->seeInDatabase('auth_groups_users', [ + $this->seeInDatabase(SHIELD_TABLES['auth_groups_users'], [ 'user_id' => $this->user->id, 'group' => 'admin', ]); - $this->seeInDatabase('auth_groups_users', [ + $this->seeInDatabase(SHIELD_TABLES['auth_groups_users'], [ 'user_id' => $this->user->id, 'group' => 'beta', ]); @@ -319,7 +319,7 @@ public function testCreatedAtIfDefaultLocaleSetFaWithAddGroup(): void $this->user->addGroup('admin'); - $this->seeInDatabase('auth_groups_users', [ + $this->seeInDatabase(SHIELD_TABLES['auth_groups_users'], [ 'user_id' => $this->user->id, 'group' => 'admin', 'created_at' => '2017-03-10 00:00:00', From 81a7e7e7b281732ca5d5dd5ecb57f5fb9a4fde4b Mon Sep 17 00:00:00 2001 From: Pooya Parsa Dadashi Date: Mon, 6 Feb 2023 20:02:01 +0330 Subject: [PATCH 10/15] replaced `auth_permissions_users` with `SHIELD_TABLES['auth_permissions_users']` --- .../2020-12-28-223112_create_auth_tables.php | 4 +-- src/Models/PermissionModel.php | 2 +- tests/Authorization/AuthorizableTest.php | 32 +++++++++---------- 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/Database/Migrations/2020-12-28-223112_create_auth_tables.php b/src/Database/Migrations/2020-12-28-223112_create_auth_tables.php index bfb3fa540..099ad8373 100644 --- a/src/Database/Migrations/2020-12-28-223112_create_auth_tables.php +++ b/src/Database/Migrations/2020-12-28-223112_create_auth_tables.php @@ -129,7 +129,7 @@ public function up(): void ]); $this->forge->addPrimaryKey('id'); $this->forge->addForeignKey('user_id', SHIELD_TABLES['users'], 'id', '', 'CASCADE'); - $this->forge->createTable('auth_permissions_users'); + $this->forge->createTable(SHIELD_TABLES['auth_permissions_users']); } // -------------------------------------------------------------------- @@ -143,7 +143,7 @@ public function down(): void $this->forge->dropTable(SHIELD_TABLES['auth_remember_tokens'], true); $this->forge->dropTable(SHIELD_TABLES['auth_identities'], true); $this->forge->dropTable(SHIELD_TABLES['auth_groups_users'], true); - $this->forge->dropTable('auth_permissions_users', true); + $this->forge->dropTable(SHIELD_TABLES['auth_permissions_users'], true); $this->forge->dropTable(SHIELD_TABLES['users'], true); $this->db->enableForeignKeyChecks(); diff --git a/src/Models/PermissionModel.php b/src/Models/PermissionModel.php index 1c648623b..c2b072d14 100644 --- a/src/Models/PermissionModel.php +++ b/src/Models/PermissionModel.php @@ -11,7 +11,7 @@ class PermissionModel extends Model { use CheckQueryReturnTrait; - protected $table = 'auth_permissions_users'; + protected $table = SHIELD_TABLES['auth_permissions_users']; protected $primaryKey = 'id'; protected $returnType = 'array'; protected $useSoftDeletes = false; diff --git a/tests/Authorization/AuthorizableTest.php b/tests/Authorization/AuthorizableTest.php index c84e369af..d345f1a5c 100644 --- a/tests/Authorization/AuthorizableTest.php +++ b/tests/Authorization/AuthorizableTest.php @@ -30,7 +30,7 @@ protected function setUp(): void // Refresh should take care of this.... db_connect()->table(SHIELD_TABLES['auth_groups_users'])->truncate(); - db_connect()->table('auth_permissions_users')->truncate(); + db_connect()->table(SHIELD_TABLES['auth_permissions_users'])->truncate(); } public function testAddGroupWithNoExistingGroups(): void @@ -160,11 +160,11 @@ public function testAddPermissionWithNoExistingPermissions(): void // Make sure it doesn't record duplicates $this->user->addPermission('admin.access', 'beta.access'); - $this->seeInDatabase('auth_permissions_users', [ + $this->seeInDatabase(SHIELD_TABLES['auth_permissions_users'], [ 'user_id' => $this->user->id, 'permission' => 'admin.access', ]); - $this->seeInDatabase('auth_permissions_users', [ + $this->seeInDatabase(SHIELD_TABLES['auth_permissions_users'], [ 'user_id' => $this->user->id, 'permission' => 'beta.access', ]); @@ -176,12 +176,12 @@ public function testAddPermissionWithNoExistingPermissions(): void public function testAddPermissionWithExistingPermissions(): void { - $this->hasInDatabase('auth_permissions_users', [ + $this->hasInDatabase(SHIELD_TABLES['auth_permissions_users'], [ 'user_id' => $this->user->id, 'permission' => 'admin.access', 'created_at' => Time::now()->toDateTimeString(), ]); - $this->hasInDatabase('auth_permissions_users', [ + $this->hasInDatabase(SHIELD_TABLES['auth_permissions_users'], [ 'user_id' => $this->user->id, 'permission' => 'users.manage', 'created_at' => Time::now()->toDateTimeString(), @@ -191,15 +191,15 @@ public function testAddPermissionWithExistingPermissions(): void // Make sure it doesn't record duplicates $this->user->addPermission('admin.access', 'beta.access'); - $this->seeInDatabase('auth_permissions_users', [ + $this->seeInDatabase(SHIELD_TABLES['auth_permissions_users'], [ 'user_id' => $this->user->id, 'permission' => 'admin.access', ]); - $this->seeInDatabase('auth_permissions_users', [ + $this->seeInDatabase(SHIELD_TABLES['auth_permissions_users'], [ 'user_id' => $this->user->id, 'permission' => 'users.manage', ]); - $this->seeInDatabase('auth_permissions_users', [ + $this->seeInDatabase(SHIELD_TABLES['auth_permissions_users'], [ 'user_id' => $this->user->id, 'permission' => 'beta.access', ]); @@ -223,14 +223,14 @@ public function testRemovePermissionNoPermissions(): void public function testRemovePermissionExistingPermissions(): void { - $this->hasInDatabase('auth_permissions_users', [ + $this->hasInDatabase(SHIELD_TABLES['auth_permissions_users'], [ 'user_id' => $this->user->id, 'permission' => 'admin.access', 'created_at' => Time::now()->toDateTimeString(), ]); $otherUser = fake(UserModel::class); - $this->hasInDatabase('auth_permissions_users', [ + $this->hasInDatabase(SHIELD_TABLES['auth_permissions_users'], [ 'user_id' => $otherUser->id, 'permission' => 'admin.access', 'created_at' => Time::now()->toDateTimeString(), @@ -238,13 +238,13 @@ public function testRemovePermissionExistingPermissions(): void $this->user->removePermission('admin.access'); $this->assertEmpty($this->user->getPermissions()); - $this->dontSeeInDatabase('auth_permissions_users', [ + $this->dontSeeInDatabase(SHIELD_TABLES['auth_permissions_users'], [ 'user_id' => $this->user->id, 'permission' => 'admin.access', ]); // Make sure it didn't delete the other user's permission - $this->seeInDatabase('auth_permissions_users', [ + $this->seeInDatabase(SHIELD_TABLES['auth_permissions_users'], [ 'user_id' => $otherUser->id, 'permission' => 'admin.access', ]); @@ -252,12 +252,12 @@ public function testRemovePermissionExistingPermissions(): void public function testSyncPermissions(): void { - $this->hasInDatabase('auth_permissions_users', [ + $this->hasInDatabase(SHIELD_TABLES['auth_permissions_users'], [ 'user_id' => $this->user->id, 'permission' => 'admin.access', 'created_at' => Time::now()->toDateTimeString(), ]); - $this->hasInDatabase('auth_permissions_users', [ + $this->hasInDatabase(SHIELD_TABLES['auth_permissions_users'], [ 'user_id' => $this->user->id, 'permission' => 'superadmin.access', 'created_at' => Time::now()->toDateTimeString(), @@ -265,11 +265,11 @@ public function testSyncPermissions(): void $this->user->syncPermissions('admin.access', 'beta.access'); $this->assertSame(['admin.access', 'beta.access'], $this->user->getPermissions()); - $this->seeInDatabase('auth_permissions_users', [ + $this->seeInDatabase(SHIELD_TABLES['auth_permissions_users'], [ 'user_id' => $this->user->id, 'permission' => 'admin.access', ]); - $this->seeInDatabase('auth_permissions_users', [ + $this->seeInDatabase(SHIELD_TABLES['auth_permissions_users'], [ 'user_id' => $this->user->id, 'permission' => 'beta.access', ]); From b9358ef961a3bf42b1988442093ede379f4acb22 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Dadashi Date: Mon, 6 Feb 2023 20:26:43 +0330 Subject: [PATCH 11/15] fix: rector error --- rector.php | 1 + 1 file changed, 1 insertion(+) diff --git a/rector.php b/rector.php index bbc490708..b9907353d 100644 --- a/rector.php +++ b/rector.php @@ -99,6 +99,7 @@ __DIR__ . '/src/Helpers', __DIR__ . '/src/Language', __DIR__ . '/tests/_support', + __DIR__ . '/src/Config/Constants.php', ], // May load view files directly when detecting classes From 52423f1bc50efca046d44c76f95093aae180e1f6 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Dadashi Date: Mon, 6 Feb 2023 20:33:15 +0330 Subject: [PATCH 12/15] style: run cs-fix --- src/Models/UserModel.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Models/UserModel.php b/src/Models/UserModel.php index 301f58def..fdd687998 100644 --- a/src/Models/UserModel.php +++ b/src/Models/UserModel.php @@ -194,9 +194,9 @@ public function findByCredentials(array $credentials): ?User sprintf('%1$s.*, %2$s.secret as email, %2$s.secret2 as password_hash', SHIELD_TABLES['users'], SHIELD_TABLES['auth_identities']) ) ->join(SHIELD_TABLES['auth_identities'], sprintf('%1$s.user_id = %2$s.id', SHIELD_TABLES['auth_identities'], SHIELD_TABLES['users'])) - ->where(SHIELD_TABLES['auth_identities'].'.type', Session::ID_TYPE_EMAIL_PASSWORD) + ->where(SHIELD_TABLES['auth_identities'] . '.type', Session::ID_TYPE_EMAIL_PASSWORD) ->where( - 'LOWER(' . $this->db->protectIdentifiers(SHIELD_TABLES['auth_identities'].'.secret') . ')', + 'LOWER(' . $this->db->protectIdentifiers(SHIELD_TABLES['auth_identities'] . '.secret') . ')', strtolower($email) ) ->asArray() From 70259856923b94f6a3aafd646a7e1d0f7e27cdf8 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Dadashi Date: Wed, 8 Feb 2023 02:30:20 +0330 Subject: [PATCH 13/15] rename `$authTables` to `$tables` and delete `auth_` from array keys --- docs/customization.md | 4 +- src/Config/Auth.php | 2 +- src/Config/Constants.php | 2 +- src/Controllers/RegisterController.php | 2 +- .../2020-12-28-223112_create_auth_tables.php | 24 +++---- src/Models/GroupModel.php | 2 +- src/Models/LoginModel.php | 2 +- src/Models/PermissionModel.php | 2 +- src/Models/RememberModel.php | 2 +- src/Models/TokenLoginModel.php | 2 +- src/Models/UserIdentityModel.php | 2 +- src/Models/UserModel.php | 8 +-- .../AccessTokenAuthenticatorTest.php | 4 +- .../SessionAuthenticatorTest.php | 30 ++++----- tests/Authentication/HasAccessTokensTest.php | 2 +- tests/Authentication/MagicLinkTest.php | 2 +- tests/Authorization/AuthorizableTest.php | 66 +++++++++---------- tests/Controllers/ActionsTest.php | 6 +- tests/Controllers/LoginTest.php | 6 +- tests/Controllers/RegisterTest.php | 2 +- tests/Unit/UserModelTest.php | 16 ++--- tests/Unit/UserTest.php | 4 +- 22 files changed, 96 insertions(+), 96 deletions(-) diff --git a/docs/customization.md b/docs/customization.md index cf10747a9..8b3ad3aed 100644 --- a/docs/customization.md +++ b/docs/customization.md @@ -153,7 +153,7 @@ Shield has the following rules for registration: ], 'email' => [ 'label' => 'Auth.email', - 'rules' => 'required|max_length[254]|valid_email|is_unique[' . SHIELD_TABLES['auth_identities'] . '.secret]', + 'rules' => 'required|max_length[254]|valid_email|is_unique[' . SHIELD_TABLES['identities'] . '.secret]', ], 'password' => [ 'label' => 'Auth.password', @@ -179,7 +179,7 @@ If you need a different set of rules for registration, you can specify them in y ], 'email' => [ 'label' => 'Auth.email', - 'rules' => 'required|max_length[254]|valid_email|is_unique[' . SHIELD_TABLES['auth_identities'] . '.secret]', + 'rules' => 'required|max_length[254]|valid_email|is_unique[' . SHIELD_TABLES['identities'] . '.secret]', ], 'password' => [ 'label' => 'Auth.password', diff --git a/src/Config/Auth.php b/src/Config/Auth.php index 05513060a..4b5e55f5d 100644 --- a/src/Config/Auth.php +++ b/src/Config/Auth.php @@ -57,7 +57,7 @@ class Auth extends BaseConfig * * @var array */ - public array $authTables = [ + public array $tables = [ 'users' => 'users', 'auth_identities' => 'auth_identities', 'auth_logins' => 'auth_logins', diff --git a/src/Config/Constants.php b/src/Config/Constants.php index 0a2c6425e..a13deb5cd 100644 --- a/src/Config/Constants.php +++ b/src/Config/Constants.php @@ -5,4 +5,4 @@ /** * Constants Name of Shield Tables */ -define('SHIELD_TABLES', config('Auth')->authTables); +define('SHIELD_TABLES', config('Auth')->tables); diff --git a/src/Controllers/RegisterController.php b/src/Controllers/RegisterController.php index bfeb91185..2160f40d6 100644 --- a/src/Controllers/RegisterController.php +++ b/src/Controllers/RegisterController.php @@ -157,7 +157,7 @@ protected function getValidationRules(): array ); $registrationEmailRules = array_merge( config('AuthSession')->emailValidationRules, - [sprintf('is_unique[%s.secret]', SHIELD_TABLES['auth_identities'])] + [sprintf('is_unique[%s.secret]', SHIELD_TABLES['identities'])] ); return setting('Validation.registration') ?? [ diff --git a/src/Database/Migrations/2020-12-28-223112_create_auth_tables.php b/src/Database/Migrations/2020-12-28-223112_create_auth_tables.php index 099ad8373..dc719daad 100644 --- a/src/Database/Migrations/2020-12-28-223112_create_auth_tables.php +++ b/src/Database/Migrations/2020-12-28-223112_create_auth_tables.php @@ -48,7 +48,7 @@ public function up(): void $this->forge->addUniqueKey(['type', 'secret']); $this->forge->addKey('user_id'); $this->forge->addForeignKey('user_id', SHIELD_TABLES['users'], 'id', '', 'CASCADE'); - $this->forge->createTable(SHIELD_TABLES['auth_identities']); + $this->forge->createTable(SHIELD_TABLES['identities']); /** * Auth Login Attempts Table @@ -69,7 +69,7 @@ public function up(): void $this->forge->addKey(['id_type', 'identifier']); $this->forge->addKey('user_id'); // NOTE: Do NOT delete the user_id or identifier when the user is deleted for security audits - $this->forge->createTable(SHIELD_TABLES['auth_logins']); + $this->forge->createTable(SHIELD_TABLES['logins']); /* * Auth Token Login Attempts Table @@ -89,7 +89,7 @@ public function up(): void $this->forge->addKey(['id_type', 'identifier']); $this->forge->addKey('user_id'); // NOTE: Do NOT delete the user_id or identifier when the user is deleted for security audits - $this->forge->createTable(SHIELD_TABLES['auth_token_logins']); + $this->forge->createTable(SHIELD_TABLES['token_logins']); /* * Auth Remember Tokens (remember-me) Table @@ -107,7 +107,7 @@ public function up(): void $this->forge->addPrimaryKey('id'); $this->forge->addUniqueKey('selector'); $this->forge->addForeignKey('user_id', SHIELD_TABLES['users'], 'id', '', 'CASCADE'); - $this->forge->createTable(SHIELD_TABLES['auth_remember_tokens']); + $this->forge->createTable(SHIELD_TABLES['remember_tokens']); // Groups Users Table $this->forge->addField([ @@ -118,7 +118,7 @@ public function up(): void ]); $this->forge->addPrimaryKey('id'); $this->forge->addForeignKey('user_id', SHIELD_TABLES['users'], 'id', '', 'CASCADE'); - $this->forge->createTable(SHIELD_TABLES['auth_groups_users']); + $this->forge->createTable(SHIELD_TABLES['groups_users']); // Users Permissions Table $this->forge->addField([ @@ -129,7 +129,7 @@ public function up(): void ]); $this->forge->addPrimaryKey('id'); $this->forge->addForeignKey('user_id', SHIELD_TABLES['users'], 'id', '', 'CASCADE'); - $this->forge->createTable(SHIELD_TABLES['auth_permissions_users']); + $this->forge->createTable(SHIELD_TABLES['permissions_users']); } // -------------------------------------------------------------------- @@ -138,12 +138,12 @@ public function down(): void { $this->db->disableForeignKeyChecks(); - $this->forge->dropTable(SHIELD_TABLES['auth_logins'], true); - $this->forge->dropTable(SHIELD_TABLES['auth_token_logins'], true); - $this->forge->dropTable(SHIELD_TABLES['auth_remember_tokens'], true); - $this->forge->dropTable(SHIELD_TABLES['auth_identities'], true); - $this->forge->dropTable(SHIELD_TABLES['auth_groups_users'], true); - $this->forge->dropTable(SHIELD_TABLES['auth_permissions_users'], true); + $this->forge->dropTable(SHIELD_TABLES['logins'], true); + $this->forge->dropTable(SHIELD_TABLES['token_logins'], true); + $this->forge->dropTable(SHIELD_TABLES['remember_tokens'], true); + $this->forge->dropTable(SHIELD_TABLES['identities'], true); + $this->forge->dropTable(SHIELD_TABLES['groups_users'], true); + $this->forge->dropTable(SHIELD_TABLES['permissions_users'], true); $this->forge->dropTable(SHIELD_TABLES['users'], true); $this->db->enableForeignKeyChecks(); diff --git a/src/Models/GroupModel.php b/src/Models/GroupModel.php index 053dceed5..34844605c 100644 --- a/src/Models/GroupModel.php +++ b/src/Models/GroupModel.php @@ -11,7 +11,7 @@ class GroupModel extends Model { use CheckQueryReturnTrait; - protected $table = SHIELD_TABLES['auth_groups_users']; + protected $table = SHIELD_TABLES['groups_users']; protected $primaryKey = 'id'; protected $returnType = 'array'; protected $useSoftDeletes = false; diff --git a/src/Models/LoginModel.php b/src/Models/LoginModel.php index a9b2740e0..121122cd9 100644 --- a/src/Models/LoginModel.php +++ b/src/Models/LoginModel.php @@ -15,7 +15,7 @@ class LoginModel extends Model { use CheckQueryReturnTrait; - protected $table = SHIELD_TABLES['auth_logins']; + protected $table = SHIELD_TABLES['logins']; protected $primaryKey = 'id'; protected $returnType = Login::class; protected $useSoftDeletes = false; diff --git a/src/Models/PermissionModel.php b/src/Models/PermissionModel.php index c2b072d14..40cee0698 100644 --- a/src/Models/PermissionModel.php +++ b/src/Models/PermissionModel.php @@ -11,7 +11,7 @@ class PermissionModel extends Model { use CheckQueryReturnTrait; - protected $table = SHIELD_TABLES['auth_permissions_users']; + protected $table = SHIELD_TABLES['permissions_users']; protected $primaryKey = 'id'; protected $returnType = 'array'; protected $useSoftDeletes = false; diff --git a/src/Models/RememberModel.php b/src/Models/RememberModel.php index 95c91a45b..110068b52 100644 --- a/src/Models/RememberModel.php +++ b/src/Models/RememberModel.php @@ -15,7 +15,7 @@ class RememberModel extends Model { use CheckQueryReturnTrait; - protected $table = SHIELD_TABLES['auth_remember_tokens']; + protected $table = SHIELD_TABLES['remember_tokens']; protected $primaryKey = 'id'; protected $returnType = 'object'; protected $useSoftDeletes = false; diff --git a/src/Models/TokenLoginModel.php b/src/Models/TokenLoginModel.php index 8dfc6d544..a1f13257b 100644 --- a/src/Models/TokenLoginModel.php +++ b/src/Models/TokenLoginModel.php @@ -10,7 +10,7 @@ class TokenLoginModel extends LoginModel { - protected $table = SHIELD_TABLES['auth_token_logins']; + protected $table = SHIELD_TABLES['token_logins']; /** * Generate a fake login for testing diff --git a/src/Models/UserIdentityModel.php b/src/Models/UserIdentityModel.php index 1b1da2fb7..63fe87f5f 100644 --- a/src/Models/UserIdentityModel.php +++ b/src/Models/UserIdentityModel.php @@ -20,7 +20,7 @@ class UserIdentityModel extends Model { use CheckQueryReturnTrait; - protected $table = SHIELD_TABLES['auth_identities']; + protected $table = SHIELD_TABLES['identities']; protected $primaryKey = 'id'; protected $returnType = UserIdentity::class; protected $useSoftDeletes = false; diff --git a/src/Models/UserModel.php b/src/Models/UserModel.php index fdd687998..b256b9e0a 100644 --- a/src/Models/UserModel.php +++ b/src/Models/UserModel.php @@ -191,12 +191,12 @@ public function findByCredentials(array $credentials): ?User if ($email !== null) { $data = $this->select( - sprintf('%1$s.*, %2$s.secret as email, %2$s.secret2 as password_hash', SHIELD_TABLES['users'], SHIELD_TABLES['auth_identities']) + sprintf('%1$s.*, %2$s.secret as email, %2$s.secret2 as password_hash', SHIELD_TABLES['users'], SHIELD_TABLES['identities']) ) - ->join(SHIELD_TABLES['auth_identities'], sprintf('%1$s.user_id = %2$s.id', SHIELD_TABLES['auth_identities'], SHIELD_TABLES['users'])) - ->where(SHIELD_TABLES['auth_identities'] . '.type', Session::ID_TYPE_EMAIL_PASSWORD) + ->join(SHIELD_TABLES['identities'], sprintf('%1$s.user_id = %2$s.id', SHIELD_TABLES['identities'], SHIELD_TABLES['users'])) + ->where(SHIELD_TABLES['identities'] . '.type', Session::ID_TYPE_EMAIL_PASSWORD) ->where( - 'LOWER(' . $this->db->protectIdentifiers(SHIELD_TABLES['auth_identities'] . '.secret') . ')', + 'LOWER(' . $this->db->protectIdentifiers(SHIELD_TABLES['identities'] . '.secret') . ')', strtolower($email) ) ->asArray() diff --git a/tests/Authentication/Authenticators/AccessTokenAuthenticatorTest.php b/tests/Authentication/Authenticators/AccessTokenAuthenticatorTest.php index 91255e106..d778ef5d1 100644 --- a/tests/Authentication/Authenticators/AccessTokenAuthenticatorTest.php +++ b/tests/Authentication/Authenticators/AccessTokenAuthenticatorTest.php @@ -143,7 +143,7 @@ public function testCheckSuccess(): void $user = fake(UserModel::class); $token = $user->generateAccessToken('foo'); - $this->seeInDatabase(SHIELD_TABLES['auth_identities'], [ + $this->seeInDatabase(SHIELD_TABLES['identities'], [ 'user_id' => $user->id, 'type' => 'access_token', 'last_used_at' => null, @@ -201,7 +201,7 @@ public function testAttemptSuccess(): void $this->assertSame($token->token, $foundUser->currentAccessToken()->token); // A login attempt should have been recorded - $this->seeInDatabase(SHIELD_TABLES['auth_token_logins'], [ + $this->seeInDatabase(SHIELD_TABLES['token_logins'], [ 'id_type' => AccessTokens::ID_TYPE_ACCESS_TOKEN, 'identifier' => $token->raw_token, 'success' => 1, diff --git a/tests/Authentication/Authenticators/SessionAuthenticatorTest.php b/tests/Authentication/Authenticators/SessionAuthenticatorTest.php index 768f98adf..3c3a897cf 100644 --- a/tests/Authentication/Authenticators/SessionAuthenticatorTest.php +++ b/tests/Authentication/Authenticators/SessionAuthenticatorTest.php @@ -46,7 +46,7 @@ protected function setUp(): void $this->events = new MockEvents(); Services::injectMock('events', $this->events); - $this->db->table(SHIELD_TABLES['auth_identities'])->truncate(); + $this->db->table(SHIELD_TABLES['identities'])->truncate(); } public function testLoggedInFalse(): void @@ -147,7 +147,7 @@ public function testLoginNoRemember(): void $this->assertSame($this->user->id, $_SESSION['user']['id']); - $this->dontSeeInDatabase(SHIELD_TABLES['auth_remember_tokens'], [ + $this->dontSeeInDatabase(SHIELD_TABLES['remember_tokens'], [ 'user_id' => $this->user->id, ]); } @@ -160,7 +160,7 @@ public function testLoginWithRemember(): void $this->assertSame($this->user->id, $_SESSION['user']['id']); - $this->seeInDatabase(SHIELD_TABLES['auth_remember_tokens'], [ + $this->seeInDatabase(SHIELD_TABLES['remember_tokens'], [ 'user_id' => $this->user->id, ]); @@ -174,12 +174,12 @@ public function testLogout(): void $this->user->createEmailIdentity(['email' => 'foo@example.com', 'password' => 'secret']); $this->auth->remember()->login($this->user); - $this->seeInDatabase(SHIELD_TABLES['auth_remember_tokens'], ['user_id' => $this->user->id]); + $this->seeInDatabase(SHIELD_TABLES['remember_tokens'], ['user_id' => $this->user->id]); $this->auth->logout(); $this->assertArrayNotHasKey('user', $_SESSION); - $this->dontSeeInDatabase(SHIELD_TABLES['auth_remember_tokens'], ['user_id' => $this->user->id]); + $this->dontSeeInDatabase(SHIELD_TABLES['remember_tokens'], ['user_id' => $this->user->id]); } public function testLogoutOnlyLogoutCalled(): void @@ -207,7 +207,7 @@ public function testLoginById(): void $this->assertSame($this->user->id, $_SESSION['user']['id']); - $this->dontSeeInDatabase(SHIELD_TABLES['auth_remember_tokens'], ['user_id' => $this->user->id]); + $this->dontSeeInDatabase(SHIELD_TABLES['remember_tokens'], ['user_id' => $this->user->id]); } public function testLoginByIdRemember(): void @@ -218,7 +218,7 @@ public function testLoginByIdRemember(): void $this->assertSame($this->user->id, $_SESSION['user']['id']); - $this->seeInDatabase(SHIELD_TABLES['auth_remember_tokens'], ['user_id' => $this->user->id]); + $this->seeInDatabase(SHIELD_TABLES['remember_tokens'], ['user_id' => $this->user->id]); } public function testForgetCurrentUser(): void @@ -227,22 +227,22 @@ public function testForgetCurrentUser(): void $this->auth->remember()->loginById($this->user->id); $this->assertSame($this->user->id, $_SESSION['user']['id']); - $this->seeInDatabase(SHIELD_TABLES['auth_remember_tokens'], ['user_id' => $this->user->id]); + $this->seeInDatabase(SHIELD_TABLES['remember_tokens'], ['user_id' => $this->user->id]); $this->auth->forget(); - $this->dontSeeInDatabase(SHIELD_TABLES['auth_remember_tokens'], ['user_id' => $this->user->id]); + $this->dontSeeInDatabase(SHIELD_TABLES['remember_tokens'], ['user_id' => $this->user->id]); } public function testForgetAnotherUser(): void { fake(RememberModel::class, ['user_id' => $this->user->id]); - $this->seeInDatabase(SHIELD_TABLES['auth_remember_tokens'], ['user_id' => $this->user->id]); + $this->seeInDatabase(SHIELD_TABLES['remember_tokens'], ['user_id' => $this->user->id]); $this->auth->forget($this->user); - $this->dontSeeInDatabase(SHIELD_TABLES['auth_remember_tokens'], ['user_id' => $this->user->id]); + $this->dontSeeInDatabase(SHIELD_TABLES['remember_tokens'], ['user_id' => $this->user->id]); } public function testCheckNoPassword(): void @@ -316,7 +316,7 @@ public function testAttemptCannotFindUser(): void $this->assertSame(lang('Auth.badAttempt'), $result->reason()); // A login attempt should have always been recorded - $this->seeInDatabase(SHIELD_TABLES['auth_logins'], [ + $this->seeInDatabase(SHIELD_TABLES['logins'], [ 'identifier' => 'johnsmith@example.com', 'success' => 0, ]); @@ -346,7 +346,7 @@ public function testAttemptSuccess(): void $this->assertSame($this->user->id, $_SESSION['user']['id']); // A login attempt should have been recorded - $this->seeInDatabase(SHIELD_TABLES['auth_logins'], [ + $this->seeInDatabase(SHIELD_TABLES['logins'], [ 'identifier' => $this->user->email, 'success' => 1, ]); @@ -396,7 +396,7 @@ public function testAttemptCaseInsensitive(): void $this->assertSame($this->user->id, $_SESSION['user']['id']); // A login attempt should have been recorded - $this->seeInDatabase(SHIELD_TABLES['auth_logins'], [ + $this->seeInDatabase(SHIELD_TABLES['logins'], [ 'identifier' => 'foo@example.COM', 'success' => 1, ]); @@ -428,7 +428,7 @@ public function testAttemptUsernameOnly(): void $this->assertSame($user->id, $_SESSION['user']['id']); // A login attempt should have been recorded - $this->seeInDatabase(SHIELD_TABLES['auth_logins'], [ + $this->seeInDatabase(SHIELD_TABLES['logins'], [ 'identifier' => 'fooROG', 'success' => 1, ]); diff --git a/tests/Authentication/HasAccessTokensTest.php b/tests/Authentication/HasAccessTokensTest.php index 223bdf3f2..284930fda 100644 --- a/tests/Authentication/HasAccessTokensTest.php +++ b/tests/Authentication/HasAccessTokensTest.php @@ -22,7 +22,7 @@ protected function setUp(): void parent::setUp(); $this->user = fake(UserModel::class); - $this->db->table(SHIELD_TABLES['auth_identities'])->truncate(); + $this->db->table(SHIELD_TABLES['identities'])->truncate(); } public function testGenerateToken(): void diff --git a/tests/Authentication/MagicLinkTest.php b/tests/Authentication/MagicLinkTest.php index e2b31b548..fc1a16bda 100644 --- a/tests/Authentication/MagicLinkTest.php +++ b/tests/Authentication/MagicLinkTest.php @@ -81,7 +81,7 @@ public function testMagicLinkSubmitSuccess(): void $result->assertOK(); $result->assertSee(lang('Auth.checkYourEmail')); - $this->seeInDatabase(SHIELD_TABLES['auth_identities'], [ + $this->seeInDatabase(SHIELD_TABLES['identities'], [ 'user_id' => $user->id, 'type' => Session::ID_TYPE_MAGIC_LINK, ]); diff --git a/tests/Authorization/AuthorizableTest.php b/tests/Authorization/AuthorizableTest.php index d345f1a5c..d463ee513 100644 --- a/tests/Authorization/AuthorizableTest.php +++ b/tests/Authorization/AuthorizableTest.php @@ -29,8 +29,8 @@ protected function setUp(): void parent::setUp(); // Refresh should take care of this.... - db_connect()->table(SHIELD_TABLES['auth_groups_users'])->truncate(); - db_connect()->table(SHIELD_TABLES['auth_permissions_users'])->truncate(); + db_connect()->table(SHIELD_TABLES['groups_users'])->truncate(); + db_connect()->table(SHIELD_TABLES['permissions_users'])->truncate(); } public function testAddGroupWithNoExistingGroups(): void @@ -39,11 +39,11 @@ public function testAddGroupWithNoExistingGroups(): void // Make sure it doesn't record duplicates $this->user->addGroup('admin', 'beta'); - $this->seeInDatabase(SHIELD_TABLES['auth_groups_users'], [ + $this->seeInDatabase(SHIELD_TABLES['groups_users'], [ 'user_id' => $this->user->id, 'group' => 'admin', ]); - $this->seeInDatabase(SHIELD_TABLES['auth_groups_users'], [ + $this->seeInDatabase(SHIELD_TABLES['groups_users'], [ 'user_id' => $this->user->id, 'group' => 'beta', ]); @@ -55,12 +55,12 @@ public function testAddGroupWithNoExistingGroups(): void public function testAddGroupWithExistingGroups(): void { - $this->hasInDatabase(SHIELD_TABLES['auth_groups_users'], [ + $this->hasInDatabase(SHIELD_TABLES['groups_users'], [ 'user_id' => $this->user->id, 'group' => 'admin', 'created_at' => Time::now()->toDateTimeString(), ]); - $this->hasInDatabase(SHIELD_TABLES['auth_groups_users'], [ + $this->hasInDatabase(SHIELD_TABLES['groups_users'], [ 'user_id' => $this->user->id, 'group' => 'superadmin', 'created_at' => Time::now()->toDateTimeString(), @@ -70,15 +70,15 @@ public function testAddGroupWithExistingGroups(): void // Make sure it doesn't record duplicates $this->user->addGroup('admin', 'beta'); - $this->seeInDatabase(SHIELD_TABLES['auth_groups_users'], [ + $this->seeInDatabase(SHIELD_TABLES['groups_users'], [ 'user_id' => $this->user->id, 'group' => 'admin', ]); - $this->seeInDatabase(SHIELD_TABLES['auth_groups_users'], [ + $this->seeInDatabase(SHIELD_TABLES['groups_users'], [ 'user_id' => $this->user->id, 'group' => 'superadmin', ]); - $this->seeInDatabase(SHIELD_TABLES['auth_groups_users'], [ + $this->seeInDatabase(SHIELD_TABLES['groups_users'], [ 'user_id' => $this->user->id, 'group' => 'beta', ]); @@ -102,14 +102,14 @@ public function testRemoveGroupNoGroups(): void public function testRemoveGroupExistingGroup(): void { - $this->hasInDatabase(SHIELD_TABLES['auth_groups_users'], [ + $this->hasInDatabase(SHIELD_TABLES['groups_users'], [ 'user_id' => $this->user->id, 'group' => 'admin', 'created_at' => Time::now()->toDateTimeString(), ]); $otherUser = fake(UserModel::class); - $this->hasInDatabase(SHIELD_TABLES['auth_groups_users'], [ + $this->hasInDatabase(SHIELD_TABLES['groups_users'], [ 'user_id' => $otherUser->id, 'group' => 'admin', 'created_at' => Time::now()->toDateTimeString(), @@ -117,13 +117,13 @@ public function testRemoveGroupExistingGroup(): void $this->user->removeGroup('admin'); $this->assertEmpty($this->user->getGroups()); - $this->dontSeeInDatabase(SHIELD_TABLES['auth_groups_users'], [ + $this->dontSeeInDatabase(SHIELD_TABLES['groups_users'], [ 'user_id' => $this->user->id, 'group' => 'admin', ]); // Make sure we didn't delete the group from anyone else - $this->seeInDatabase(SHIELD_TABLES['auth_groups_users'], [ + $this->seeInDatabase(SHIELD_TABLES['groups_users'], [ 'user_id' => $otherUser->id, 'group' => 'admin', ]); @@ -131,12 +131,12 @@ public function testRemoveGroupExistingGroup(): void public function testSyncGroups(): void { - $this->hasInDatabase(SHIELD_TABLES['auth_groups_users'], [ + $this->hasInDatabase(SHIELD_TABLES['groups_users'], [ 'user_id' => $this->user->id, 'group' => 'admin', 'created_at' => Time::now()->toDateTimeString(), ]); - $this->hasInDatabase(SHIELD_TABLES['auth_groups_users'], [ + $this->hasInDatabase(SHIELD_TABLES['groups_users'], [ 'user_id' => $this->user->id, 'group' => 'superadmin', 'created_at' => Time::now()->toDateTimeString(), @@ -144,11 +144,11 @@ public function testSyncGroups(): void $this->user->syncGroups('admin', 'beta'); $this->assertSame(['admin', 'beta'], $this->user->getGroups()); - $this->seeInDatabase(SHIELD_TABLES['auth_groups_users'], [ + $this->seeInDatabase(SHIELD_TABLES['groups_users'], [ 'user_id' => $this->user->id, 'group' => 'admin', ]); - $this->seeInDatabase(SHIELD_TABLES['auth_groups_users'], [ + $this->seeInDatabase(SHIELD_TABLES['groups_users'], [ 'user_id' => $this->user->id, 'group' => 'beta', ]); @@ -160,11 +160,11 @@ public function testAddPermissionWithNoExistingPermissions(): void // Make sure it doesn't record duplicates $this->user->addPermission('admin.access', 'beta.access'); - $this->seeInDatabase(SHIELD_TABLES['auth_permissions_users'], [ + $this->seeInDatabase(SHIELD_TABLES['permissions_users'], [ 'user_id' => $this->user->id, 'permission' => 'admin.access', ]); - $this->seeInDatabase(SHIELD_TABLES['auth_permissions_users'], [ + $this->seeInDatabase(SHIELD_TABLES['permissions_users'], [ 'user_id' => $this->user->id, 'permission' => 'beta.access', ]); @@ -176,12 +176,12 @@ public function testAddPermissionWithNoExistingPermissions(): void public function testAddPermissionWithExistingPermissions(): void { - $this->hasInDatabase(SHIELD_TABLES['auth_permissions_users'], [ + $this->hasInDatabase(SHIELD_TABLES['permissions_users'], [ 'user_id' => $this->user->id, 'permission' => 'admin.access', 'created_at' => Time::now()->toDateTimeString(), ]); - $this->hasInDatabase(SHIELD_TABLES['auth_permissions_users'], [ + $this->hasInDatabase(SHIELD_TABLES['permissions_users'], [ 'user_id' => $this->user->id, 'permission' => 'users.manage', 'created_at' => Time::now()->toDateTimeString(), @@ -191,15 +191,15 @@ public function testAddPermissionWithExistingPermissions(): void // Make sure it doesn't record duplicates $this->user->addPermission('admin.access', 'beta.access'); - $this->seeInDatabase(SHIELD_TABLES['auth_permissions_users'], [ + $this->seeInDatabase(SHIELD_TABLES['permissions_users'], [ 'user_id' => $this->user->id, 'permission' => 'admin.access', ]); - $this->seeInDatabase(SHIELD_TABLES['auth_permissions_users'], [ + $this->seeInDatabase(SHIELD_TABLES['permissions_users'], [ 'user_id' => $this->user->id, 'permission' => 'users.manage', ]); - $this->seeInDatabase(SHIELD_TABLES['auth_permissions_users'], [ + $this->seeInDatabase(SHIELD_TABLES['permissions_users'], [ 'user_id' => $this->user->id, 'permission' => 'beta.access', ]); @@ -223,14 +223,14 @@ public function testRemovePermissionNoPermissions(): void public function testRemovePermissionExistingPermissions(): void { - $this->hasInDatabase(SHIELD_TABLES['auth_permissions_users'], [ + $this->hasInDatabase(SHIELD_TABLES['permissions_users'], [ 'user_id' => $this->user->id, 'permission' => 'admin.access', 'created_at' => Time::now()->toDateTimeString(), ]); $otherUser = fake(UserModel::class); - $this->hasInDatabase(SHIELD_TABLES['auth_permissions_users'], [ + $this->hasInDatabase(SHIELD_TABLES['permissions_users'], [ 'user_id' => $otherUser->id, 'permission' => 'admin.access', 'created_at' => Time::now()->toDateTimeString(), @@ -238,13 +238,13 @@ public function testRemovePermissionExistingPermissions(): void $this->user->removePermission('admin.access'); $this->assertEmpty($this->user->getPermissions()); - $this->dontSeeInDatabase(SHIELD_TABLES['auth_permissions_users'], [ + $this->dontSeeInDatabase(SHIELD_TABLES['permissions_users'], [ 'user_id' => $this->user->id, 'permission' => 'admin.access', ]); // Make sure it didn't delete the other user's permission - $this->seeInDatabase(SHIELD_TABLES['auth_permissions_users'], [ + $this->seeInDatabase(SHIELD_TABLES['permissions_users'], [ 'user_id' => $otherUser->id, 'permission' => 'admin.access', ]); @@ -252,12 +252,12 @@ public function testRemovePermissionExistingPermissions(): void public function testSyncPermissions(): void { - $this->hasInDatabase(SHIELD_TABLES['auth_permissions_users'], [ + $this->hasInDatabase(SHIELD_TABLES['permissions_users'], [ 'user_id' => $this->user->id, 'permission' => 'admin.access', 'created_at' => Time::now()->toDateTimeString(), ]); - $this->hasInDatabase(SHIELD_TABLES['auth_permissions_users'], [ + $this->hasInDatabase(SHIELD_TABLES['permissions_users'], [ 'user_id' => $this->user->id, 'permission' => 'superadmin.access', 'created_at' => Time::now()->toDateTimeString(), @@ -265,11 +265,11 @@ public function testSyncPermissions(): void $this->user->syncPermissions('admin.access', 'beta.access'); $this->assertSame(['admin.access', 'beta.access'], $this->user->getPermissions()); - $this->seeInDatabase(SHIELD_TABLES['auth_permissions_users'], [ + $this->seeInDatabase(SHIELD_TABLES['permissions_users'], [ 'user_id' => $this->user->id, 'permission' => 'admin.access', ]); - $this->seeInDatabase(SHIELD_TABLES['auth_permissions_users'], [ + $this->seeInDatabase(SHIELD_TABLES['permissions_users'], [ 'user_id' => $this->user->id, 'permission' => 'beta.access', ]); @@ -319,7 +319,7 @@ public function testCreatedAtIfDefaultLocaleSetFaWithAddGroup(): void $this->user->addGroup('admin'); - $this->seeInDatabase(SHIELD_TABLES['auth_groups_users'], [ + $this->seeInDatabase(SHIELD_TABLES['groups_users'], [ 'user_id' => $this->user->id, 'group' => 'admin', 'created_at' => '2017-03-10 00:00:00', diff --git a/tests/Controllers/ActionsTest.php b/tests/Controllers/ActionsTest.php index 341cdc47f..9b4ef02fc 100644 --- a/tests/Controllers/ActionsTest.php +++ b/tests/Controllers/ActionsTest.php @@ -155,7 +155,7 @@ public function testEmail2FAVerify(): void $this->assertSame(site_url(), $result->getRedirectUrl()); // Identity should have been removed - $this->dontSeeInDatabase(SHIELD_TABLES['auth_identities'], [ + $this->dontSeeInDatabase(SHIELD_TABLES['identities'], [ 'user_id' => $this->user->id, 'type' => Session::ID_TYPE_EMAIL_2FA, ]); @@ -174,7 +174,7 @@ public function testShowEmail2FACreatesIdentity(): void $result->assertOK(); - $this->seeInDatabase(SHIELD_TABLES['auth_identities'], [ + $this->seeInDatabase(SHIELD_TABLES['identities'], [ 'user_id' => $this->user->id, 'type' => Session::ID_TYPE_EMAIL_2FA, 'name' => 'login', @@ -259,7 +259,7 @@ public function testEmailActivateVerify(): void $this->assertSame(site_url(), $result->getRedirectUrl()); // Identity should have been removed - $this->dontSeeInDatabase(SHIELD_TABLES['auth_identities'], [ + $this->dontSeeInDatabase(SHIELD_TABLES['identities'], [ 'user_id' => $this->user->id, 'type' => Session::ID_TYPE_EMAIL_2FA, ]); diff --git a/tests/Controllers/LoginTest.php b/tests/Controllers/LoginTest.php index 46434c878..5d1ffb1bb 100644 --- a/tests/Controllers/LoginTest.php +++ b/tests/Controllers/LoginTest.php @@ -53,7 +53,7 @@ public function testLoginBadEmail(): void $this->assertSame(site_url('/login'), $result->getRedirectUrl()); // Login should have been recorded successfully - $this->seeInDatabase(SHIELD_TABLES['auth_logins'], [ + $this->seeInDatabase(SHIELD_TABLES['logins'], [ 'identifier' => 'fooled@example.com', 'user_id' => null, 'success' => 0, @@ -87,7 +87,7 @@ public function testLoginActionEmailSuccess(): void $this->assertSame(site_url(), $result->getRedirectUrl()); // Login should have been recorded successfully - $this->seeInDatabase(SHIELD_TABLES['auth_logins'], [ + $this->seeInDatabase(SHIELD_TABLES['logins'], [ 'identifier' => 'foo@example.com', 'user_id' => $this->user->id, 'success' => 1, @@ -159,7 +159,7 @@ public function testLoginActionUsernameSuccess(): void $this->assertSame(site_url(), $result->getRedirectUrl()); // Login should have been recorded successfully - $this->seeInDatabase(SHIELD_TABLES['auth_logins'], [ + $this->seeInDatabase(SHIELD_TABLES['logins'], [ 'identifier' => $this->user->username, 'user_id' => $this->user->id, 'success' => 1, diff --git a/tests/Controllers/RegisterTest.php b/tests/Controllers/RegisterTest.php index 4fc1602b2..14698f644 100644 --- a/tests/Controllers/RegisterTest.php +++ b/tests/Controllers/RegisterTest.php @@ -68,7 +68,7 @@ public function testRegisterActionSuccess(): void /** @var User $user */ $user = model(UserModel::class)->where('username', 'JohnDoe')->first(); - $this->seeInDatabase(SHIELD_TABLES['auth_identities'], [ + $this->seeInDatabase(SHIELD_TABLES['identities'], [ 'user_id' => $user->id, 'type' => Session::ID_TYPE_EMAIL_PASSWORD, 'secret' => 'john.doe@example.com', diff --git a/tests/Unit/UserModelTest.php b/tests/Unit/UserModelTest.php index e5232b364..16d0d46ee 100644 --- a/tests/Unit/UserModelTest.php +++ b/tests/Unit/UserModelTest.php @@ -35,7 +35,7 @@ public function testSaveInsertUser(): void $users->save($user); $user = $users->findByCredentials(['email' => 'foo@bar.com']); - $this->seeInDatabase(SHIELD_TABLES['auth_identities'], [ + $this->seeInDatabase(SHIELD_TABLES['identities'], [ 'user_id' => $user->id, 'secret' => 'foo@bar.com', ]); @@ -70,7 +70,7 @@ public function testInsertUserObject(): void $users->insert($user); $user = $users->findByCredentials(['email' => 'foo@bar.com']); - $this->seeInDatabase(SHIELD_TABLES['auth_identities'], [ + $this->seeInDatabase(SHIELD_TABLES['identities'], [ 'user_id' => $user->id, 'secret' => 'foo@bar.com', ]); @@ -116,7 +116,7 @@ public function testInsertUserArray(): void $id = $users->insert($userArray); - $this->dontSeeInDatabase(SHIELD_TABLES['auth_identities'], [ + $this->dontSeeInDatabase(SHIELD_TABLES['identities'], [ 'user_id' => $id, 'secret' => 'foo@bar.com', ]); @@ -151,7 +151,7 @@ public function testSaveUpdateUserObjectWithUserDataToUpdate(): void $users->save($user); - $this->seeInDatabase(SHIELD_TABLES['auth_identities'], [ + $this->seeInDatabase(SHIELD_TABLES['identities'], [ 'user_id' => $user->id, 'secret' => 'bar@bar.com', ]); @@ -175,7 +175,7 @@ public function testUpdateUserObjectWithUserDataToUpdate(): void $users->update($user->id, $user); - $this->seeInDatabase(SHIELD_TABLES['auth_identities'], [ + $this->seeInDatabase(SHIELD_TABLES['identities'], [ 'user_id' => $user->id, 'secret' => 'bar@bar.com', ]); @@ -211,7 +211,7 @@ public function testUpdateUserArrayWithUserDataToUpdate(): void $users->update($user->id, $userArray); - $this->dontSeeInDatabase(SHIELD_TABLES['auth_identities'], [ + $this->dontSeeInDatabase(SHIELD_TABLES['identities'], [ 'user_id' => $user->id, 'secret' => 'bar@bar.com', ]); @@ -233,7 +233,7 @@ public function testSaveUpdateUserObjectWithoutUserDataToUpdate(): void $users->save($user); - $this->seeInDatabase(SHIELD_TABLES['auth_identities'], [ + $this->seeInDatabase(SHIELD_TABLES['identities'], [ 'user_id' => $user->id, 'secret' => 'bar@bar.com', ]); @@ -251,7 +251,7 @@ public function testUpdateUserObjectWithoutUserDataToUpdate(): void $users->update(null, $user); - $this->seeInDatabase(SHIELD_TABLES['auth_identities'], [ + $this->seeInDatabase(SHIELD_TABLES['identities'], [ 'user_id' => $user->id, 'secret' => 'bar@bar.com', ]); diff --git a/tests/Unit/UserTest.php b/tests/Unit/UserTest.php index 468ebcb95..a909c5e9e 100644 --- a/tests/Unit/UserTest.php +++ b/tests/Unit/UserTest.php @@ -184,7 +184,7 @@ public function testUpdateEmail(): void $user = $users->find($this->user->id); - $this->seeInDatabase(SHIELD_TABLES['auth_identities'], [ + $this->seeInDatabase(SHIELD_TABLES['identities'], [ 'user_id' => $user->id, 'secret' => 'foo@bar.com', ]); @@ -225,7 +225,7 @@ public function testUpdatePasswordHash(): void $user = $users->find($this->user->id); - $this->seeInDatabase(SHIELD_TABLES['auth_identities'], [ + $this->seeInDatabase(SHIELD_TABLES['identities'], [ 'user_id' => $user->id, 'secret' => 'foo@bar.com', 'secret2' => $hash, From 9dc0525e383e1a624e7d83612d899ac0bd6a75af Mon Sep 17 00:00:00 2001 From: Pooya Parsa Dadashi Date: Wed, 8 Feb 2023 03:13:00 +0330 Subject: [PATCH 14/15] delete `auth_` from `$tables` keys --- src/Config/Auth.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Config/Auth.php b/src/Config/Auth.php index 4b5e55f5d..57862293a 100644 --- a/src/Config/Auth.php +++ b/src/Config/Auth.php @@ -58,13 +58,13 @@ class Auth extends BaseConfig * @var array */ public array $tables = [ - 'users' => 'users', - 'auth_identities' => 'auth_identities', - 'auth_logins' => 'auth_logins', - 'auth_token_logins' => 'auth_token_logins', - 'auth_remember_tokens' => 'auth_remember_tokens', - 'auth_groups_users' => 'auth_groups_users', - 'auth_permissions_users' => 'auth_permissions_users', + 'users' => 'users', + 'identities' => 'auth_identities', + 'logins' => 'auth_logins', + 'token_logins' => 'auth_token_logins', + 'remember_tokens' => 'auth_remember_tokens', + 'groups_users' => 'auth_groups_users', + 'permissions_users' => 'auth_permissions_users', ]; /** From 07c85e1116b8a219a4ded278ea4fee9c0f677cfa Mon Sep 17 00:00:00 2001 From: Pooya Parsa Dadashi Date: Thu, 9 Feb 2023 23:14:20 +0330 Subject: [PATCH 15/15] fix: use `new()` for pass phpstan error --- src/Config/Constants.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/Config/Constants.php b/src/Config/Constants.php index a13deb5cd..db54b613a 100644 --- a/src/Config/Constants.php +++ b/src/Config/Constants.php @@ -2,7 +2,6 @@ declare(strict_types=1); -/** - * Constants Name of Shield Tables - */ -define('SHIELD_TABLES', config('Auth')->tables); +use CodeIgniter\Shield\Config\Auth as ShieldAuth; + +define('SHIELD_TABLES', (new ShieldAuth())->tables);