From 837bf2100172e22b2ac022e289053a50211d919f Mon Sep 17 00:00:00 2001 From: Lupacescu Eduard Date: Thu, 25 Nov 2021 15:34:00 +0200 Subject: [PATCH] conditional action log (#441) * fix: Fixing query matches. (#439) * fix: Use only query params for matching. * fix: Conditional action loggable. * Fix styling Co-authored-by: binaryk --- src/Actions/Action.php | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/src/Actions/Action.php b/src/Actions/Action.php index 0c455f4f..81c15afa 100644 --- a/src/Actions/Action.php +++ b/src/Actions/Action.php @@ -4,7 +4,7 @@ use Binaryk\LaravelRestify\Http\Requests\ActionRequest; use Binaryk\LaravelRestify\Http\Requests\RestifyRequest; -use Binaryk\LaravelRestify\Models\ActionLog; +use Binaryk\LaravelRestify\Models\Concerns\HasActionLogs; use Binaryk\LaravelRestify\Restify; use Binaryk\LaravelRestify\Traits\AuthorizedToSee; use Binaryk\LaravelRestify\Traits\Make; @@ -82,8 +82,8 @@ public function uriKey() /** * Determine if the action is executable for the given request. * - * @param Request $request - * @param Model $model + * @param Request $request + * @param Model $model * @return bool */ public function authorizedToRun(Request $request, $model) @@ -94,7 +94,7 @@ public function authorizedToRun(Request $request, $model) /** * Set the callback to be run to authorize running the action. * - * @param Closure $callback + * @param Closure $callback * @return $this */ public function canRun(Closure $callback) @@ -107,8 +107,8 @@ public function canRun(Closure $callback) /** * Get the payload available on the action. * - * @deprecated Use rules instead * @return array + * @deprecated Use rules instead */ public function payload(): array { @@ -128,7 +128,7 @@ public function rules(): array /** * Make current action being standalone. No model query will be performed. * - * @param bool $standalone + * @param bool $standalone * @return self */ public function standalone(bool $standalone = true): self @@ -167,7 +167,11 @@ public function handleRequest(ActionRequest $request) Transaction::run(function () use ($models, $request, &$response) { $response = $this->handle($request, $models); - $models->each(fn (Model $model) => ActionLog::forRepositoryAction($this, $model, $request->user())->save()); + $models->each(function (Model $model) use ($request) { + if (in_array(HasActionLogs::class, class_uses_recursive($model), true)) { + Restify::actionLog()::forRepositoryAction($this, $model, $request->user())->save(); + } + }); }); }); } else { @@ -179,11 +183,13 @@ public function handleRequest(ActionRequest $request) })->firstOrFail() ); - Restify::actionLog()::forRepositoryAction( - $this, - $model, - $request->user() - )->save(); + if (in_array(HasActionLogs::class, class_uses_recursive($model), true)) { + Restify::actionLog()::forRepositoryAction( + $this, + $model, + $request->user() + )->save(); + } }); }