diff --git a/docs/addons/jwt.md b/docs/addons/jwt.md index 3cf3f8ae3..f312e5919 100644 --- a/docs/addons/jwt.md +++ b/docs/addons/jwt.md @@ -163,7 +163,7 @@ class LoginController extends BaseController $rules = $this->getValidationRules(); // Validate credentials - if (! $this->validateData($this->request->getJSON(true), $rules)) { + if (! $this->validateData($this->request->getJSON(true), $rules, [], config('Auth')->DBGroup)) { return $this->fail( ['errors' => $this->validator->getErrors()], $this->codes['unauthorized'] diff --git a/docs/guides/mobile_apps.md b/docs/guides/mobile_apps.md index 237eeeafd..b6da507b4 100644 --- a/docs/guides/mobile_apps.md +++ b/docs/guides/mobile_apps.md @@ -38,7 +38,7 @@ class LoginController extends BaseController ], ]; - if (! $this->validateData($this->request->getPost(), $rules)) { + if (! $this->validateData($this->request->getPost(), $rules, [], config('Auth')->DBGroup)) { return $this->response ->setJSON(['errors' => $this->validator->getErrors()]) ->setStatusCode(401); diff --git a/src/Config/Auth.php b/src/Config/Auth.php index b4d9ebae0..1c7d57902 100644 --- a/src/Config/Auth.php +++ b/src/Config/Auth.php @@ -42,6 +42,13 @@ class Auth extends BaseConfig 'magic-link-email' => '\CodeIgniter\Shield\Views\Email\magic_link_email', ]; + /** + * -------------------------------------------------------------------- + * Customize the DB group used for each model + * -------------------------------------------------------------------- + */ + public ?string $DBGroup = null; + /** * -------------------------------------------------------------------- * Customize Name of Shield Tables diff --git a/src/Controllers/LoginController.php b/src/Controllers/LoginController.php index dd27b97bf..eab0aa316 100644 --- a/src/Controllers/LoginController.php +++ b/src/Controllers/LoginController.php @@ -47,7 +47,7 @@ public function loginAction(): RedirectResponse // like the password, can only be validated properly here. $rules = $this->getValidationRules(); - if (! $this->validateData($this->request->getPost(), $rules)) { + if (! $this->validateData($this->request->getPost(), $rules, [], config('Auth')->DBGroup)) { return redirect()->back()->withInput()->with('errors', $this->validator->getErrors()); } diff --git a/src/Controllers/MagicLinkController.php b/src/Controllers/MagicLinkController.php index 6ed442056..45cfb1060 100644 --- a/src/Controllers/MagicLinkController.php +++ b/src/Controllers/MagicLinkController.php @@ -68,7 +68,7 @@ public function loginAction() { // Validate email format $rules = $this->getValidationRules(); - if (! $this->validateData($this->request->getPost(), $rules)) { + if (! $this->validateData($this->request->getPost(), $rules, [], config('Auth')->DBGroup)) { return redirect()->route('magic-link')->with('errors', $this->validator->getErrors()); } diff --git a/src/Controllers/RegisterController.php b/src/Controllers/RegisterController.php index 500b1ea36..954742dc2 100644 --- a/src/Controllers/RegisterController.php +++ b/src/Controllers/RegisterController.php @@ -100,7 +100,7 @@ public function registerAction(): RedirectResponse // like the password, can only be validated properly here. $rules = $this->getValidationRules(); - if (! $this->validateData($this->request->getPost(), $rules)) { + if (! $this->validateData($this->request->getPost(), $rules, [], config('Auth')->DBGroup)) { return redirect()->back()->withInput()->with('errors', $this->validator->getErrors()); } 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 deb4570ae..792cdad3a 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 @@ -19,10 +19,15 @@ class CreateAuthTables extends Migration public function __construct(?Forge $forge = null) { + /** @var Auth $authConfig */ + $authConfig = config('Auth'); + + if ($authConfig->DBGroup !== null) { + $this->DBGroup = $authConfig->DBGroup; + } + parent::__construct($forge); - /** @var Auth $authConfig */ - $authConfig = config('Auth'); $this->tables = $authConfig->tables; $this->attributes = ($this->db->getPlatform() === 'MySQLi') ? ['ENGINE' => 'InnoDB'] : []; } diff --git a/src/Models/BaseModel.php b/src/Models/BaseModel.php index b15836da5..c266a6070 100644 --- a/src/Models/BaseModel.php +++ b/src/Models/BaseModel.php @@ -16,11 +16,21 @@ abstract class BaseModel extends Model */ protected array $tables; - protected function initialize(): void + protected Auth $authConfig; + + public function __construct() { - /** @var Auth $authConfig */ - $authConfig = config('Auth'); + $this->authConfig = config(Auth::class); - $this->tables = $authConfig->tables; + if ($this->authConfig->DBGroup !== null) { + $this->DBGroup = $this->authConfig->DBGroup; + } + + parent::__construct(); + } + + protected function initialize(): void + { + $this->tables = $this->authConfig->tables; } }