如何添加额外的代码到供应商的功能?



我有件事需要帮助。使用Lab404Impersonate I包允许某些用户在Laravel 9中模拟用户。然而,当发生这种情况时,我想用Auditlog包记录它:

AuditLog::create([
'description'  => 'user:impersonate',
'subject_id'   => $id ?? null,
'subject_type' => sprintf('%s#%s', 'AppModelsUser', $id) ?? null,
'user_id'      => auth()->id() ?? null,
'properties'   =>  null,
'host'         => request()->ip() ?? null,
]);

我已经找到了impersonatcontroller:


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函数,还是有更好的方法?

您可以编写一个侦听器来模拟laravel- #事件并在监听器的handle方法中生成日志:

public function handle(TakeImpersonation $event)
{
// do log here
}

最新更新