-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Retrieve user ip and id in middleware [SLE-193]
- Loading branch information
1 parent
f851f5b
commit c1aff81
Showing
12 changed files
with
78 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
namespace App\Application\Data; | ||
|
||
/** | ||
* DTO that contains client's network data such as | ||
* IP address and user agent and user identity id. | ||
*/ | ||
class UserNetworkSessionData | ||
{ | ||
// Initialize vars with default values | ||
public ?string $ipAddress = null; | ||
public ?string $userAgent = null; | ||
public null|int|string $userId = null; | ||
} |
37 changes: 37 additions & 0 deletions
37
src/Application/Middleware/UserNetworkSessionDataMiddleware.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace App\Application\Middleware; | ||
|
||
use App\Application\Data\UserNetworkSessionData; | ||
use Odan\Session\SessionInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Http\Server\MiddlewareInterface; | ||
use Psr\Http\Server\RequestHandlerInterface; | ||
|
||
/** | ||
* Middleware that adds client network data such as IP address and user agent | ||
* as well as user identity id to the clientNetworkData DTO. | ||
*/ | ||
class UserNetworkSessionDataMiddleware implements MiddlewareInterface | ||
{ | ||
public function __construct( | ||
private readonly UserNetworkSessionData $clientNetworkData, | ||
private readonly SessionInterface $session, | ||
) { | ||
} | ||
|
||
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface | ||
{ | ||
// Server params will be null in testing | ||
$ipAddress = $request->getServerParams()['REMOTE_ADDR']; | ||
$userAgent = $request->getServerParams()['HTTP_USER_AGENT']; | ||
|
||
// Add ip address to the ipAddressData DTO object | ||
$this->clientNetworkData->ipAddress = $ipAddress; | ||
$this->clientNetworkData->userAgent = $userAgent; | ||
$this->clientNetworkData->userId = $this->session->get('user_id'); | ||
|
||
return $handler->handle($request); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters