Skip to content

Commit

Permalink
Merge pull request #34 from insenseanalytics/add_nova_action_events
Browse files Browse the repository at this point in the history
Add ability to record impersonation actions in Nova action_events
  • Loading branch information
KABBOUCHI authored Dec 13, 2018
2 parents d7e3b90 + ccf135e commit 0a2b59e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
6 changes: 5 additions & 1 deletion config/nova-impersonate.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
* Redirect path
*/
'redirect_to' => '/',
/**
* Set true to record impersonation actions in Nova's action_events table
*/
'actionable' => env('NOVA_IMPERSONATE_ACTIONABLE', false),
/**
* Bind on key press to impersonate user in details page
*/
Expand All @@ -30,4 +34,4 @@
*/
'leave' => 'auth',
],
];
];
28 changes: 28 additions & 0 deletions src/Http/Controllers/ImpersonateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Illuminate\Support\Str;
use Laravel\Nova\Actions\ActionEvent;
use Lab404\Impersonate\Services\ImpersonateManager;

class ImpersonateController extends Controller
Expand Down Expand Up @@ -33,6 +35,10 @@ public function take(Request $request, $id)
abort(403);
}

if (config('nova-impersonate.actionable')) {
$this->recordAction($request->user()->getKey(), $user_to_impersonate, 'Impersonate');
}

$this->manager->take($request->user(), $user_to_impersonate);

$redirectBack = config('nova-impersonate.redirect_back');
Expand All @@ -47,11 +53,33 @@ public function leave()
{

if ($this->manager->isImpersonating()) {
if (config('nova-impersonate.actionable')) {
$this->recordAction($this->manager->getImpersonatorId(), auth()->user(), 'Leave Impersonation');
}

$this->manager->leave();

return redirect()->to(session()->pull('leave_redirect_to') ?? config('nova.path'));
}

return redirect()->to('/');
}

protected function recordAction($userId, $user_to_impersonate, $actionName)
{
ActionEvent::create([
'batch_id' => (string)Str::orderedUuid(),
'user_id' => $userId,
'name' => $actionName,
'actionable_type' => $user_to_impersonate->getMorphClass(),
'actionable_id' => $user_to_impersonate->getKey(),
'target_type' => $user_to_impersonate->getMorphClass(),
'target_id' => $user_to_impersonate->getKey(),
'model_type' => $user_to_impersonate->getMorphClass(),
'model_id' => $user_to_impersonate->getKey(),
'fields' => '',
'status' => 'finished',
'exception' => '',
]);
}
}

0 comments on commit 0a2b59e

Please sign in to comment.