Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Admin-View of Activity logs // Roles by ID #1105

Merged
merged 5 commits into from
Jan 11, 2025
Merged
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
36 changes: 32 additions & 4 deletions app/Http/Controllers/Admin/ActivityLogController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\User;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\View\View;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Storage;
use Spatie\Activitylog\Models\Activity;
use Illuminate\Pagination\LengthAwarePaginator;

class ActivityLogController extends Controller
{
Expand All @@ -28,9 +28,35 @@ public function index(Request $request)
$cronLogs = Storage::disk('logs')->exists('cron.log') ? Storage::disk('logs')->get('cron.log') : null;

if ($request->input('search')) {
$query = Activity::whereHasMorph('causer', [User::class], function ($query) use ($request) {
$query->where('name', 'like', "%{$request->input('search')}%");
})->orderBy('created_at', 'desc')->paginate(20);
$searchTerm = $request->input('search');

// Pre-fetch logs and decode JSON properties
$logs = Activity::all()->filter(function ($log) use ($searchTerm) {
$properties = json_decode($log->properties, true);

// Check if search term exists in attributes or old values
$attributesMatch = isset($properties['attributes']) &&
collect($properties['attributes'])->contains(fn($value) => str_contains(strtolower($value), strtolower($searchTerm)));

$oldMatch = isset($properties['old']) &&
collect($properties['old'])->contains(fn($value) => str_contains(strtolower($value), strtolower($searchTerm)));

return str_contains(strtolower($log->description), strtolower($searchTerm)) ||
str_contains(strtolower(optional($log->causer)->name), strtolower($searchTerm)) ||
$attributesMatch || $oldMatch;
});

// Paginate manually
$perPage = 20;
$currentPage = LengthAwarePaginator::resolveCurrentPage();
$currentItems = $logs->slice(($currentPage - 1) * $perPage, $perPage);
$query = new LengthAwarePaginator(
$currentItems,
$logs->count(),
$perPage,
$currentPage,
['path' => LengthAwarePaginator::resolveCurrentPath()]
);
} else {
$query = Activity::orderBy('created_at', 'desc')->paginate(20);
}
Expand All @@ -39,6 +65,8 @@ public function index(Request $request)
'logs' => $query,
'cronlogs' => $cronLogs,
]);


}

/**
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/Auth/RegisterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ protected function create(array $data)
'pterodactyl_id' => Str::uuid(),
]);

$user->syncRoles(Role::findByName('User'));
$user->syncRoles(Role::findById(4)); //user

$response = $this->pterodactyl->application->post('/application/users', [
'external_id' => null,
Expand Down
8 changes: 4 additions & 4 deletions database/seeders/PermissionsSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,22 @@ public function run()

$users = User::all();
foreach($users as $user){
$user->assignRole(Role::findByName('user'));
$user->assignRole(Role::findById(4));
}

$admins = User::where("role","admin")->get();
foreach($admins as $admin) {
$admin->syncRoles(Role::findByName('Admin'));
$admin->syncRoles(Role::findById(1));
}

$mods = User::where("role","moderator")->get();
foreach($mods as $mod) {
$mod->syncRoles(Role::findByName('Support-Team'));
$mod->syncRoles(Role::findById(2));
}

$clients = User::where("role","client")->get();
foreach($clients as $client) {
$client->syncRoles(Role::findByName('Client'));
$client->syncRoles(Role::findById(3));
}
}

Expand Down
189 changes: 113 additions & 76 deletions themes/default/views/admin/activitylogs/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,86 +40,123 @@
</div>
</div>

<div class="card">
<div class="card-header">
<h5 class="card-title"><i class="fas fa-history mr-2"></i>{{ __('Activity Logs')}}</h5>
</div>
<div class="card-body table-responsive">

<div class="row">
<div class="col-lg-3 offset-lg-9 col-xl-2 offset-xl-10 col-md-6 offset-md-6">
<form method="get" action="{{route('admin.activitylogs.index')}}">
@csrf
<div class="input-group mb-3">
<input type="text" class="form-control form-control-sm" value="" name="search" placeholder="Search">
<div class="input-group-append">
<button class="btn btn-light btn-sm" type="submit"><i class="fa fa-search"></i></button>
</div>
</div>

<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
</div>
</div>
<div class="card">
<div class="card-header">
<h5 class="card-title"><i class="fas fa-history mr-2"></i>{{ __('Activity Logs')}}</h5>
</div>
<div class="card-body table-responsive">

<table class="table table-sm table-striped">
<thead>
<tr>
<th>{{ __('Causer') }}</th>
<th>{{ __('Description') }}</th>
<th>{{ __('Created at') }}</th>
</tr>
</thead>
<tbody>
@foreach($logs as $log)
<tr>
<td> @if($log->causer) <a href='/admin/users/{{$log->causer_id}}'> {{json_decode($log->causer)->name}}
@else
System
@endif</td>
<td>
<span>
@if(str_starts_with($log->description,"created"))
<small><i class="fas text-success fa-plus mr-2"></i></small>
@elseif(str_starts_with($log->description,"redeemed"))
<small><i class="fas text-success fa-money-check-alt mr-2"></i></small>
@elseif(str_starts_with($log->description,"deleted"))
<small><i class="fas text-danger fa-times mr-2"></i></small>
@elseif(str_starts_with($log->description,"gained"))
<small><i class="fas text-success fa-money-bill mr-2"></i></small>
@elseif(str_starts_with($log->description,"updated"))
<small><i class="fas text-info fa-pen mr-2"></i></small>
@endif
{{ explode("\\" , $log->subject_type)[2]}}
{{$log->description}}

@php $first=true @endphp
@foreach(json_decode($log->properties, true) as $properties)
@if($first)
@if(isset($properties['name']))
"{{$properties['name']}}"
@endif
@if(isset($properties['email']))
< {{$properties['email']}} >
@endif
@php $first=false @endphp
@endif
@endforeach
</span>
</td>

<td>{{$log->created_at->diffForHumans()}}</td>
</tr>
@endforeach
</tbody>
</table>

<div class="float-right">
{!! $logs->links() !!}
<div class="row">
<div class="col-lg-3 offset-lg-9 col-xl-2 offset-xl-10 col-md-6 offset-md-6">
<form method="get" action="{{route('admin.activitylogs.index')}}">
@csrf
<div class="input-group mb-3">
<input type="text" class="form-control form-control-sm" value="" name="search" placeholder="Search">
<div class="input-group-append">
<button class="btn btn-light btn-sm" type="submit"><i class="fa fa-search"></i></button>
</div>
</div>

<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
</div>
</div>

<table class="table table-sm table-striped">
<thead>
<tr>
<th>{{ __('Causer') }}</th>
<th>{{ __('Description') }}</th>
<th>{{ __('Created at') }}</th>
</tr>
</thead>
<tbody>
@foreach($logs as $log)
<tr>
<td>
@if($log->causer)
<a href='/admin/users/{{$log->causer_id}}'>{{json_decode($log->causer)->name}}</a>
@else
System
@endif
</td>
<td>
<span>
@if (str_starts_with($log->description, 'created'))
<small><i class="fas text-success fa-plus mr-2"></i></small>
@elseif(str_starts_with($log->description, 'redeemed'))
<small><i class="fas text-success fa-money-check-alt mr-2"></i></small>
@elseif(str_starts_with($log->description, 'deleted'))
<small><i class="fas text-danger fa-times mr-2"></i></small>
@elseif(str_starts_with($log->description, 'gained'))
<small><i class="fas text-success fa-money-bill mr-2"></i></small>
@elseif(str_starts_with($log->description, 'updated'))
<small><i class="fas text-info fa-pen mr-2"></i></small>
@endif
{{ explode('\\', $log->subject_type)[2] }}
{{ ucfirst($log->description) }}

@php
$properties = json_decode($log->properties, true);
@endphp

{{-- Handle Created Entries --}}
@if ($log->description === 'created' && isset($properties['attributes']))
<ul class="ml-3">
@foreach ($properties['attributes'] as $attribute => $value)
@if (!is_null($value))
<li>
<strong>{{ ucfirst($attribute) }}:</strong>
{{ $attribute === 'created_at' || $attribute === 'updated_at' ? \Carbon\Carbon::parse($value)->toDayDateTimeString() : $value }}
</li>
@endif
@endforeach
</ul>
@endif

{{-- Handle Updated Entries --}}
@if ($log->description === 'updated' && isset($properties['attributes'], $properties['old']))
<ul class="ml-3">
@foreach ($properties['attributes'] as $attribute => $newValue)
@if (array_key_exists($attribute, $properties['old']) && !is_null($newValue))
<li>
<strong>{{ ucfirst($attribute) }}:</strong>
{{ $attribute === 'created_at' || $attribute === 'updated_at' ?
\Carbon\Carbon::parse($properties['old'][$attribute])->toDayDateTimeString() . ' → ' . \Carbon\Carbon::parse($newValue)->toDayDateTimeString()
: $properties['old'][$attribute] . ' → ' . $newValue }}
</li>
@endif
@endforeach
</ul>
@endif

{{-- Handle Deleted Entries --}}
@if ($log->description === 'deleted' && isset($properties['old']))
<ul class="ml-3">
@foreach ($properties['old'] as $attribute => $value)
@if (!is_null($value))
<li>
<strong>{{ ucfirst($attribute) }}:</strong>
{{ $attribute === 'created_at' || $attribute === 'updated_at' ? \Carbon\Carbon::parse($value)->toDayDateTimeString() : $value }}
</li>
@endif
@endforeach
</ul>
@endif
</span>
</td>
<td>{{$log->created_at->diffForHumans()}}</td>
</tr>
@endforeach
</tbody>
</table>

<div class="float-right">
{!! $logs->links() !!}
</div>

</div>
</div>



</div>
Expand Down