Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
44 changes: 28 additions & 16 deletions src/DataTablesEditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ abstract class DataTablesEditor
/**
* List of custom editor actions.
*
* @var string[]
* @var array<array-key, string|class-string>
*/
protected array $customActions = [];

Expand Down Expand Up @@ -89,25 +89,25 @@ abstract class DataTablesEditor

/**
* Process dataTables editor action request.
*
* @return JsonResponse
*
* @throws DataTablesEditorException
*/
public function process(Request $request): mixed
public function process(?Request $request = null): JsonResponse
{
if ($request->get('action') && is_string($request->get('action'))) {
$this->action = $request->get('action');
} else {
throw new DataTablesEditorException('Invalid action requested!');
}
$request ??= request();
$this->action = $request->get('action');

if (! in_array($this->action, array_merge($this->actions, $this->customActions))) {
throw new DataTablesEditorException(sprintf('Requested action (%s) not supported!', $this->action));
}
throw_unless(
$this->isValidAction($request),
DataTablesEditorException::class,
'Invalid action requested!'
);

try {
return $this->{$this->action}($request);
if (method_exists($this, $this->action)) {
return $this->{$this->action}($request);
}

// @phpstan-ignore-next-line method.nonObject
return resolve($this->customActions[$this->action], ['editor' => $this])->handle($request);
} catch (Exception $exception) {
$error = config('app.debug')
? '<strong>Server Error:</strong> '.$exception->getMessage()
Expand All @@ -119,6 +119,18 @@ public function process(Request $request): mixed
}
}

public function isValidAction(Request $request): bool
{
$validActions = $this->actions;
foreach ($this->customActions as $key => $action) {
$validActions[] = is_numeric($key) ? $action : $key;
}

return in_array($this->action, $validActions)
&& $request->get('action')
&& is_string($request->get('action'));
}

protected function getUseFriendlyErrorMessage(): string
{
return 'An error occurs while processing your request.';
Expand All @@ -127,7 +139,7 @@ protected function getUseFriendlyErrorMessage(): string
/**
* Display success data in dataTables editor format.
*/
protected function toJson(array $data, array $errors = [], string|array $error = ''): JsonResponse
public function toJson(array $data, array $errors = [], string|array $error = ''): JsonResponse
{
$code = 200;

Expand Down
16 changes: 16 additions & 0 deletions tests/Editors/Remove2FA.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Yajra\DataTables\Tests\Editors;

use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class Remove2FA
{
public function handle(Request $request): JsonResponse
{
return new JsonResponse([
'message' => '2FA has been removed successfully.',
]);
}
}
4 changes: 4 additions & 0 deletions tests/Editors/UsersDataTableEditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ class UsersDataTableEditor extends DataTablesEditor
{
protected $model = User::class;

protected array $customActions = [
'remove2fa' => Remove2FA::class,
];

/**
* Get create action validation rules.
*/
Expand Down
22 changes: 22 additions & 0 deletions tests/Feature/DataTablesEditorActionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Yajra\DataTables\Tests\Feature;

use PHPUnit\Framework\Attributes\Test;
use Yajra\DataTables\Tests\TestCase;

class DataTablesEditorActionTest extends TestCase
{
#[Test]
public function it_can_process_custom_action_request()
{
$response = $this->postJson('users', [
'action' => 'remove2fa',
'data' => [],
]);

$data = $response->json();
$this->assertArrayHasKey('message', $data);
$this->assertEquals('2FA has been removed successfully.', $data['message']);
}
}
2 changes: 1 addition & 1 deletion tests/Feature/DataTablesEditorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class DataTablesEditorTest extends TestCase
public function it_throws_exception_on_invalid_action()
{
$this->expectException(DataTablesEditorException::class);
$this->expectExceptionMessage('Requested action (invalid) not supported!');
$this->expectExceptionMessage('Invalid action requested!');

$editor = new UsersDataTableEditor;
request()->merge(['action' => 'invalid']);
Expand Down