Skip to content

Commit

Permalink
Merge pull request #7 from voicixs/l8-support
Browse files Browse the repository at this point in the history
Support Laravel 8
  • Loading branch information
cliche23 authored Apr 22, 2022
2 parents 1ea9fbc + 901830e commit 4dd7940
Show file tree
Hide file tree
Showing 11 changed files with 106 additions and 238 deletions.
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": "^8"
},
"extra": {
"laravel": {
Expand Down
14 changes: 2 additions & 12 deletions database/migrations/2017_11_15_074656_create_admin_log_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,7 @@

class CreateAdminLogTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
public function up(): void
{
Schema::create('admin_log', function (Blueprint $table) {
$table->increments('id');
Expand All @@ -27,12 +22,7 @@ public function up()
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
public function down(): void
{
Schema::dropIfExists('admin_log');
}
Expand Down
15 changes: 2 additions & 13 deletions database/migrations/2017_12_06_075619_add_columns_to_admin_log.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,7 @@

class AddColumnsToAdminLog extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
public function up(): void
{
Schema::table('admin_log', function (Blueprint $table) {
$table->dropColumn('forwarded_ip');
Expand All @@ -22,16 +17,10 @@ public function up()
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
public function down(): void
{
Schema::table('admin_log', function (Blueprint $table) {
$table->string('forwarded_ip')->nullable();

$table->dropColumn('ips');
$table->dropColumn('http_content_type');
$table->dropColumn('session');
Expand Down
5 changes: 1 addition & 4 deletions src/Admin/Form/Serialization.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@

class Serialization extends AbstractField
{
/**
* @return Element
*/
public function render()
public function render(): Element
{
return (new SerializationRenderer($this))->render();
}
Expand Down
14 changes: 0 additions & 14 deletions src/Admin/Form/SerializationRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,11 @@

class SerializationRenderer extends GenericRenderer
{
/**
* SerializationRenderer constructor.
* @param FieldInterface $field
*/
public function __construct(FieldInterface $field)
{
$this->field = $field;
}

/**
* @return Element
*/
protected function getInput(): Element
{
$content = unserialize($this->field->getValue());
Expand All @@ -45,18 +38,11 @@ protected function getInput(): Element
]);
}

/**
* @return Element
*/
public function render(): Element
{
return $this->getInput();
}

/**
* @param array|null $style
* @return string
*/
public function getStyle(?array $style = []): string
{
$return = '';
Expand Down
15 changes: 2 additions & 13 deletions src/AdminLogServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,7 @@

class AdminLogServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* @param Router $router
* @return void
*/
public function boot(Router $router)
public function boot(Router $router): void
{
if ($router->hasMiddlewareGroup('admin')) {
$router->pushMiddlewareToGroup('admin', LogAdminRequests::class);
Expand All @@ -40,12 +34,7 @@ public function boot(Router $router)
}
}

/**
* Register the application services.
*
* @return void
*/
public function register()
public function register(): void
{
$this->mergeConfigFrom(__DIR__.'/../config/admin-log.php', 'admin-log');
}
Expand Down
19 changes: 3 additions & 16 deletions src/Console/Commands/CleanupAdminLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,13 @@

class CleanupAdminLog extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
/** @var string */
protected $signature = 'arbory:cleanup-admin-log';

/**
* The console command description.
*
* @var string
*/
/** @var string */
protected $description = 'Cleanup Arbory admin log table';

/**
* Execute the console command.
*
* @return void
*/
public function handle()
public function handle(): void
{
$retainFor = config('admin-log.cleanup.retain_for_days', 0);

Expand Down
28 changes: 10 additions & 18 deletions src/Http/Controllers/Admin/AdminLogController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,19 @@
use Arbory\Base\Admin\Form\FieldSet;
use Arbory\Base\Admin\Grid;
use Arbory\Base\Admin\Traits\Crudify;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Routing\Redirector;

class AdminLogController extends Controller
{
use Crudify;

/**
* @var string
*/
/** @var string */
protected $resource = AdminLog::class;

/**
* @param Form $form
* @return Form
*/
protected function form(Form $form)
protected function form(Form $form): Form
{
$form->setFields(function (FieldSet $fieldSet) {

Expand Down Expand Up @@ -76,10 +73,7 @@ protected function form(Form $form)
return $form;
}

/**
* @return Grid
*/
public function grid(Grid $grid)
public function grid(Grid $grid): Grid
{
$grid->column('user_name', trans('admin-log::common.user_name'))
->sortable();
Expand All @@ -102,16 +96,14 @@ public function grid(Grid $grid)
return $grid;
}

/**
* @param Request $request
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function update(Request $request)
public function update(Request $request, $resourceId): JsonResponse|RedirectResponse|Redirector
{
if ($request->ajax()) {
return response()->json(['ok']);
}

return $this->getAfterEditResponse($request);
$resource = $this->findOrNew($resourceId);

return $this->getAfterEditResponse($request, $resource);
}
}
140 changes: 63 additions & 77 deletions src/Http/Middleware/LogAdminRequests.php
Original file line number Diff line number Diff line change
@@ -1,77 +1,63 @@
<?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\Config\Repository;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;

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

/** @var mixed|Repository */
protected $config;

/** @var array */
protected $hideFields = [
'password',
'password_confirmation',
];

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

public function handle(Request $request, Closure $next): mixed
{
$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);
}

private function getSession(): array
{
$session = request()->session()->all();

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

0 comments on commit 4dd7940

Please sign in to comment.