|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* WebHemi. |
|
4
|
|
|
* |
|
5
|
|
|
* PHP version 5.6 |
|
6
|
|
|
* |
|
7
|
|
|
* @copyright 2012 - 2016 Gixx-web (http://www.gixx-web.com) |
|
8
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License (MIT) |
|
9
|
|
|
* |
|
10
|
|
|
* @link http://www.gixx-web.com |
|
11
|
|
|
*/ |
|
12
|
|
|
namespace WebHemi\Middleware\Security; |
|
13
|
|
|
|
|
14
|
|
|
use WebHemi\Adapter\Auth\AuthAdapterInterface; |
|
15
|
|
|
use WebHemi\Adapter\Http\ResponseInterface; |
|
16
|
|
|
use WebHemi\Adapter\Http\ServerRequestInterface; |
|
17
|
|
|
use WebHemi\Adapter\Log\LogAdapterInterface; |
|
18
|
|
|
use WebHemi\Application\EnvironmentManager; |
|
19
|
|
|
use WebHemi\Data\Entity\User\UserEntity; |
|
20
|
|
|
use WebHemi\Middleware\MiddlewareInterface; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Class AccessLogMiddleware |
|
24
|
|
|
*/ |
|
25
|
|
|
class AccessLogMiddleware implements MiddlewareInterface |
|
26
|
|
|
{ |
|
27
|
|
|
/** @var LogAdapterInterface */ |
|
28
|
|
|
private $logger; |
|
29
|
|
|
/** @var AuthAdapterInterface */ |
|
30
|
|
|
private $authAdapter; |
|
31
|
|
|
/** @var EnvironmentManager */ |
|
32
|
|
|
private $environmentManager; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* EventLogMiddleware constructor. |
|
36
|
|
|
* |
|
37
|
|
|
* @param LogAdapterInterface $logger |
|
38
|
|
|
* @param AuthAdapterInterface $authAdapter |
|
39
|
|
|
* @param EnvironmentManager $environmentManager |
|
40
|
|
|
*/ |
|
41
|
|
|
public function __construct( |
|
42
|
|
|
LogAdapterInterface $logger, |
|
43
|
|
|
AuthAdapterInterface $authAdapter, |
|
44
|
|
|
EnvironmentManager $environmentManager |
|
45
|
|
|
) { |
|
46
|
|
|
$this->logger = $logger; |
|
47
|
|
|
$this->authAdapter = $authAdapter; |
|
48
|
|
|
$this->environmentManager = $environmentManager; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* A middleware is a callable. It can do whatever is appropriate with the Request and Response objects. |
|
53
|
|
|
* The only hard requirement is that a middleware MUST return an instance of \Psr\Http\Message\ResponseInterface. |
|
54
|
|
|
* Each middleware SHOULD invoke the next middleware and pass it Request and Response objects as arguments. |
|
55
|
|
|
* |
|
56
|
|
|
* @param ServerRequestInterface $request |
|
57
|
|
|
* @param ResponseInterface $response |
|
58
|
|
|
* @return ResponseInterface |
|
59
|
|
|
*/ |
|
60
|
|
|
public function __invoke(ServerRequestInterface &$request, ResponseInterface $response) |
|
61
|
|
|
{ |
|
62
|
|
|
$identity = 'Unauthenticated user'; |
|
63
|
|
|
$requestAttributes = $request->getAttributes(); |
|
64
|
|
|
$actionMiddleware = isset($requestAttributes[ServerRequestInterface::REQUEST_ATTR_RESOLVED_ACTION_CLASS]) |
|
65
|
|
|
? $requestAttributes[ServerRequestInterface::REQUEST_ATTR_RESOLVED_ACTION_CLASS] |
|
66
|
|
|
: 'N/A'; |
|
67
|
|
|
|
|
68
|
|
|
if ($this->authAdapter->hasIdentity()) { |
|
69
|
|
|
/** @var UserEntity $userEntity */ |
|
70
|
|
|
$userEntity = $this->authAdapter->getIdentity(); |
|
71
|
|
|
$identity = $userEntity->getEmail(); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
$data = [ |
|
75
|
|
|
'User' => $identity, |
|
76
|
|
|
'IP' => $this->environmentManager->getClientIp(), |
|
77
|
|
|
'RequestUri' => $request->getUri()->getPath().'?'.$request->getUri()->getQuery(), |
|
78
|
|
|
'RequestMethod' => $request->getMethod(), |
|
79
|
|
|
'Action' => $actionMiddleware, |
|
80
|
|
|
'Parameters' => $request->getParsedBody() |
|
81
|
|
|
]; |
|
82
|
|
|
|
|
83
|
|
|
$this->logger->log('info', json_encode($data)); |
|
84
|
|
|
|
|
85
|
|
|
return $response; |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|