Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
}
},
"require": {
"arbory/arbory": "^1.0"
"laravel/framework": "^6.0.0"
},
"extra": {
"laravel": {
Expand Down
155 changes: 78 additions & 77 deletions src/Http/Middleware/LogAdminRequests.php
Original file line number Diff line number Diff line change
@@ -1,77 +1,78 @@
<?php

namespace Arbory\AdminLog\Http\Middleware;

use Cartalyst\Sentinel\Sentinel;
use Closure;
use Arbory\AdminLog\Models\AdminLog;
use Arbory\AdminLog\Utils\Sanitizer;
use Illuminate\Http\Request;

class LogAdminRequests
{
/**
* @var Sentinel
*/
protected $sentinel;

/**
* The names of the attributes
* that should not be trimmed.
*
* @var array
*/
protected $hideFields = [
'password',
'password_confirmation',
];

/**
* @param Sentinel $sentinel
*/
public function __construct(Sentinel $sentinel)
{
$this->sentinel = $sentinel;
$this->config = config('admin-log');
}

/**
* @param Request $request
* @param Closure $next
* @return mixed
*/
public function handle(Request $request, Closure $next)
{
$sanitizer = new Sanitizer();
$user = $this->sentinel->getUser();

AdminLog::create([
'user_name' => $user ? $user->getUserLogin() : null,
'request_uri' => $sanitizer->sanitize($request->getUri()),
'ip' => $request->getClientIp(),
'ips' => join(',', $request->ips()),
'request_method' => $request->getRealMethod(),
'http_referer' => join(',', array_wrap($request->header('HTTP_REFERER', null))),
'user_agent' => $request->userAgent(),
'http_content_type' => $request->getContentType(),
'http_cookie' => serialize($sanitizer->sanitize($request->cookies->all())),
'session' => serialize($sanitizer->sanitize($this->getSession())),
'content' => serialize($sanitizer->sanitize($request->all())),
]);

return $next($request);
}

/**
* @return array
*/
private function getSession()
{
$session = request()->session()->all();

return collect($session)
->except($this->config['session_blacklist_keys'])
->toArray();
}

}
<?php

namespace Arbory\AdminLog\Http\Middleware;

use Cartalyst\Sentinel\Sentinel;
use Closure;
use Arbory\AdminLog\Models\AdminLog;
use Arbory\AdminLog\Utils\Sanitizer;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;

class LogAdminRequests
{
/**
* @var Sentinel
*/
protected $sentinel;

/**
* The names of the attributes
* that should not be trimmed.
*
* @var array
*/
protected $hideFields = [
'password',
'password_confirmation',
];

/**
* @param Sentinel $sentinel
*/
public function __construct(Sentinel $sentinel)
{
$this->sentinel = $sentinel;
$this->config = config('admin-log');
}

/**
* @param Request $request
* @param Closure $next
* @return mixed
*/
public function handle(Request $request, Closure $next)
{
$sanitizer = new Sanitizer();
$user = $this->sentinel->getUser();

AdminLog::create([
'user_name' => $user ? $user->getUserLogin() : null,
'request_uri' => $sanitizer->sanitize($request->getUri()),
'ip' => $request->getClientIp(),
'ips' => join(',', $request->ips()),
'request_method' => $request->getRealMethod(),
'http_referer' => join(',', Arr::wrap($request->header('HTTP_REFERER', null))),
'user_agent' => $request->userAgent(),
'http_content_type' => $request->getContentType(),
'http_cookie' => serialize($sanitizer->sanitize($request->cookies->all())),
'session' => serialize($sanitizer->sanitize($this->getSession())),
'content' => serialize($sanitizer->sanitize($request->all())),
]);

return $next($request);
}

/**
* @return array
*/
private function getSession()
{
$session = request()->session()->all();

return collect($session)
->except($this->config['session_blacklist_keys'])
->toArray();
}

}
12 changes: 7 additions & 5 deletions src/Utils/Sanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Arbory\AdminLog\Utils;

use Illuminate\Support\Arr;

class Sanitizer
{
/**
Expand Down Expand Up @@ -37,7 +39,7 @@ public function __construct()
*/
protected function setSensitiveStringPatterns()
{
$identifiers = array_get($this->config, 'sensitive_string_identifiers');
$identifiers = Arr::get($this->config, 'sensitive_string_identifiers');

$this->sensitiveStringPatterns = array_map(function ($identifier) {
return '/(?<=\b' . $identifier . '=)(.+)(\b)/U';
Expand All @@ -62,7 +64,7 @@ protected function getSensitiveStringPatterns()
protected function getRemovedValueNotification()
{
if (!$this->removeValueNotification) {
$this->removeValueNotification = array_get($this->config, 'removed_value_notification');
$this->removeValueNotification = Arr::get($this->config, 'removed_value_notification');
}

return $this->removeValueNotification;
Expand Down Expand Up @@ -162,11 +164,11 @@ protected function getSensitiveKeyPatterns()
{
if (!$this->sensitiveKeyPatterns) {
$this->sensitiveKeyPatterns = array_merge(
array_get($this->config, 'sensitive_key_patterns'),
array_get($this->config, 'sensitive_string_identifiers')
Arr::get($this->config, 'sensitive_key_patterns'),
Arr::get($this->config, 'sensitive_string_identifiers')
);
}

return $this->sensitiveKeyPatterns;
}
}
}