|
19 | 19 | use Auth\User; |
20 | 20 | use Doctrine\ORM\Event\OnFlushEventArgs; |
21 | 21 | use Illuminate\Support\Facades\App; |
| 22 | +use Illuminate\Support\Facades\Auth; |
22 | 23 | use Illuminate\Support\Facades\Log; |
23 | 24 | use Illuminate\Support\Facades\Route; |
24 | 25 | use Illuminate\Http\Request; |
25 | 26 | use OAuth2\IResourceServerContext; |
| 27 | +use OAuth2\Models\IClient; |
| 28 | +use Services\OAuth2\ResourceServerContext; |
| 29 | + |
26 | 30 | /** |
27 | 31 | * Class AuditEventListener |
28 | 32 | * @package App\Audit |
@@ -95,17 +99,32 @@ private function getAuditStrategy($em): ?IAuditStrategy |
95 | 99 |
|
96 | 100 | private function buildAuditContext(): AuditContext |
97 | 101 | { |
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 | + ***/ |
99 | 108 |
|
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 | + } |
104 | 122 |
|
105 | 123 | $defaultUiContext = [ |
106 | 124 | 'app' => null, |
107 | 125 | 'flow' => null |
108 | 126 | ]; |
| 127 | + |
109 | 128 | $uiContext = [ |
110 | 129 | ...$defaultUiContext, |
111 | 130 | // ...app()->bound('ui.context') ? app('ui.context') : [], |
|
0 commit comments