Skip to content

Commit

Permalink
Merge pull request #744 from arashsaffari/customization_db_group
Browse files Browse the repository at this point in the history
feat: add 'DBGroup' for customization db group
  • Loading branch information
datamweb committed May 22, 2023
2 parents a17a09c + 7899218 commit 3dc2154
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 11 deletions.
2 changes: 1 addition & 1 deletion docs/addons/jwt.md
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down
2 changes: 1 addition & 1 deletion docs/guides/mobile_apps.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 7 additions & 0 deletions src/Config/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Controllers/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand Down
2 changes: 1 addition & 1 deletion src/Controllers/MagicLinkController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand Down
2 changes: 1 addition & 1 deletion src/Controllers/RegisterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'] : [];
}
Expand Down
18 changes: 14 additions & 4 deletions src/Models/BaseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit 3dc2154

Please sign in to comment.