Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add 'DBGroup' for customization db group #744

Merged
merged 14 commits into from
May 22, 2023
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
10 changes: 10 additions & 0 deletions src/Config/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ class Auth extends BaseConfig
'magic-link-email' => '\CodeIgniter\Shield\Views\Email\magic_link_email',
];

/**
* --------------------------------------------------------------------
* Customize the DB group used for each model
* --------------------------------------------------------------------
* if no database connection instance is passed to the constructor,
* it will automatically connect to the default database group,
* as set in the configuration
arashsaffari marked this conversation as resolved.
Show resolved Hide resolved
*/
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 @@ -22,7 +22,12 @@ public function __construct(?Forge $forge = null)
parent::__construct($forge);

/** @var Auth $authConfig */
$authConfig = config('Auth');
$authConfig = config('Auth');

if ($authConfig->DBGroup !== null) {
$this->DBGroup = $authConfig->DBGroup;
}

datamweb marked this conversation as resolved.
Show resolved Hide resolved
$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;
arashsaffari marked this conversation as resolved.
Show resolved Hide resolved

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;
}
}