Skip to content

Commit 2e4b0e9

Browse files
committed
fix(audit-logging): add missing case for getting uesr info
1 parent 75cea8c commit 2e4b0e9

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

app/Audit/AuditEventListener.php

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,14 @@
1919
use Auth\User;
2020
use Doctrine\ORM\Event\OnFlushEventArgs;
2121
use Illuminate\Support\Facades\App;
22+
use Illuminate\Support\Facades\Auth;
2223
use Illuminate\Support\Facades\Log;
2324
use Illuminate\Support\Facades\Route;
2425
use Illuminate\Http\Request;
2526
use OAuth2\IResourceServerContext;
27+
use OAuth2\Models\IClient;
28+
use Services\OAuth2\ResourceServerContext;
29+
2630
/**
2731
* Class AuditEventListener
2832
* @package App\Audit
@@ -95,17 +99,32 @@ private function getAuditStrategy($em): ?IAuditStrategy
9599

96100
private function buildAuditContext(): AuditContext
97101
{
98-
$userId = app(IResourceServerContext::class)->getCurrentUserId();
102+
/***
103+
* here we have 2 cases
104+
* 1. we are connecting to the IDP using an external APi ( under oauth2 ) so the
105+
* resource context have a client id and have a user id
106+
* 2. we are logged at idp and using the UI ( $user = Auth::user() )
107+
***/
99108

100-
/**
101-
* @var User|null $user
102-
*/
103-
$user = $userId ? app(IUserRepository::class)->getById($userId) : null;
109+
$resource_server_context = app(IResourceServerContext::class);
110+
$oauth2_current_client_id = $resource_server_context->getCurrentClientId();
111+
112+
if(!empty($oauth2_current_client_id)) {
113+
$userId = $resource_server_context->getCurrentUserId();
114+
// here $userId can be null bc
115+
// $resource_server_context->getApplicationType() == IClient::ApplicationType_Service
116+
$user = $userId ? app(IUserRepository::class)->getById($userId) : null;
117+
}
118+
else{
119+
// 2. we are at IDP UI
120+
$user = Auth::user();
121+
}
104122

105123
$defaultUiContext = [
106124
'app' => null,
107125
'flow' => null
108126
];
127+
109128
$uiContext = [
110129
...$defaultUiContext,
111130
// ...app()->bound('ui.context') ? app('ui.context') : [],

0 commit comments

Comments
 (0)