Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions config/initial-event-propagation.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
'set_initial_event_http_middleware' => [
'default_user_type' => '',

/**
* If is set to `false` the middleware does not try to get current user
* Defaults to `true`.
*/
'try_auth' => true,

/**
* If is set to `true` the middleware does not override the InitialEvent if it was already set for current context earlier.
* Defaults to `false`.
Expand Down
11 changes: 8 additions & 3 deletions src/SetInitialEventHttpMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,23 @@ class SetInitialEventHttpMiddleware
{
public function handle(Request $request, Closure $next): mixed
{
$user = $request->user();
$config = config('initial-event-propagation', []);
$mc = config('initial-event-propagation.set_initial_event_http_middleware', []);
$holder = Container::getInstance()->make(InitialEventHolder::class);
$existingEvent = $holder->getInitialEvent();

if (!$existingEvent || empty($mc['preserve_existing_event'])) {

$user = null;
if ($mc['try_auth'] ?? true) {
$user = $request->user();
}

$initialEvent = InitialEventDTO::fromScratch(
userId: $user ? $user->getAuthIdentifier() : "",
userType: $user ? $mc['default_user_type'] : "",
app: !empty($mc['app_code_header']) ? $request->header($mc['app_code_header'], '') : ($config['app_code'] ?? ''),
entrypoint: $this->extractEntrypoint($request),
userId: $user ? $user->getAuthIdentifier() : "",
userType: $user ? $mc['default_user_type'] : "",
correlationId: !empty($mc['correlation_id_header']) ? $request->header($mc['correlation_id_header'], '') : '',
timestamp: !empty($mc['timestamp_header']) ? $request->header($mc['timestamp_header'], '') : ''
);
Expand Down