我需要有关特定问题的帮助。使用Lab404\Impersonate a包,我允许某些用户在Laravel 9中模拟用户。但是,当发生这种情况时,我希望使用审计日志包记录此情况:
AuditLog::create([
'description' => 'user:impersonate',
'subject_id' => $id ?? null,
'subject_type' => sprintf('%s#%s', 'App\Models\User', $id) ?? null,
'user_id' => auth()->id() ?? null,
'properties' => null,
'host' => request()->ip() ?? null,
]);
我已找到模拟控制器:
public function take(Request $request, $id, $guardName = null)
{
$guardName = $guardName ?? $this->manager->getDefaultSessionGuard();
// Cannot impersonate yourself
if ($id == $request->user()->getAuthIdentifier() && ($this->manager->getCurrentAuthGuardName() == $guardName)) {
abort(403);
}
// Cannot impersonate again if you're already impersonate a user
if ($this->manager->isImpersonating()) {
abort(403);
}
if (!$request->user()->canImpersonate()) {
abort(403);
}
$userToImpersonate = $this->manager->findUserById($id, $guardName);
if ($userToImpersonate->canBeImpersonated()) {
if ($this->manager->take($request->user(), $userToImpersonate, $guardName)) {
$takeRedirect = $this->manager->getTakeRedirectTo();
if ($takeRedirect !== 'back') {
return redirect()->to($takeRedirect);
}
}
}
return redirect()->back();
}
我可以在这里添加代码,但是这当然不是正确的方法,我不确定正确的方法是什么,我应该重写take函数,还是有更好的方法?
1条答案
按热度按时间dldeef671#
您可以为laravel-impersonate#事件编写一个侦听器,并在侦听器的handle方法中记录日志: