diff --git a/app/Http/Controllers/ActivityController.php b/app/Http/Controllers/ActivityController.php new file mode 100644 index 0000000..9d33f54 --- /dev/null +++ b/app/Http/Controllers/ActivityController.php @@ -0,0 +1,26 @@ +only('email', 'password'))) { + activity() ->causedBy(auth()->user())->log('User logged into the portal'); // Log activity message return redirect('dashboard')->with('success', 'Welcome '.auth()->user()->name); } diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index a0c2312..77694a2 100644 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -35,13 +35,15 @@ public function create() public function store(StoreUserRequest $request) { + activity()->causedBy(auth()->user())->log('User ' . $request->name . ' created'); $user = $this->userRepository->store($request); - return redirect()->route('user.index')->with('success', 'User '.$user->name.' created'); + return redirect()->route('user.index')->with('success', 'User ' . $user->name . ' created'); } public function show(User $user) { + activity()->causedBy(auth()->user())->log('User ' . $user->name . ' viewed'); if ($user->role === 'Customer') { $customer = Customer::where('user_id', $user->id)->first(); @@ -62,6 +64,7 @@ public function edit(User $user) public function update(User $user, UpdateCustomerRequest $request) { + activity()->causedBy(auth()->user())->log('User ' . $user->name . ' updated'); $user->update($request->all()); if ($user->isCustomer()) { @@ -70,17 +73,18 @@ public function update(User $user, UpdateCustomerRequest $request) ]); } - return redirect()->route('user.index')->with('success', 'User '.$user->name.' udpated!'); + return redirect()->route('user.index')->with('success', 'User ' . $user->name . ' udpated!'); } public function destroy(User $user) { + activity()->causedBy(auth()->user())->log('User ' . $user->name . ' updated'); try { $user->delete(); - return redirect()->route('user.index')->with('success', 'User '.$user->name.' deleted!'); + return redirect()->route('user.index')->with('success', 'User ' . $user->name . ' deleted!'); } catch (\Exception $e) { - return redirect()->route('user.index')->with('failed', 'Customer '.$user->name.' cannot be deleted! Error Code:'.$e->errorInfo[1]); + return redirect()->route('user.index')->with('failed', 'Customer ' . $user->name . ' cannot be deleted! Error Code:' . $e->errorInfo[1]); } } } diff --git a/composer.json b/composer.json index a96fb4c..7b937a4 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,8 @@ "laravel/framework": "^11.0", "laravel/reverb": "@beta", "laravel/tinker": "^2.5", - "pusher/pusher-php-server": "^7.2" + "pusher/pusher-php-server": "^7.2", + "spatie/laravel-activitylog": "^4.8" }, "require-dev": { "barryvdh/laravel-debugbar": "^3.13", diff --git a/composer.lock b/composer.lock index 145a992..df6aeaf 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "79646ae26439512388301b85d0c275af", + "content-hash": "9c6c60e8d5cd6ca5cfb22eb3c170ea36", "packages": [ { "name": "brick/math", @@ -4230,6 +4230,157 @@ ], "time": "2023-06-16T10:52:11+00:00" }, + { + "name": "spatie/laravel-activitylog", + "version": "4.8.0", + "source": { + "type": "git", + "url": "https://github.com/spatie/laravel-activitylog.git", + "reference": "eb6f37dd40af950ce10cf5280f0acfa3e08c3bff" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/spatie/laravel-activitylog/zipball/eb6f37dd40af950ce10cf5280f0acfa3e08c3bff", + "reference": "eb6f37dd40af950ce10cf5280f0acfa3e08c3bff", + "shasum": "" + }, + "require": { + "illuminate/config": "^8.0 || ^9.0 || ^10.0 || ^11.0", + "illuminate/database": "^8.69 || ^9.27 || ^10.0 || ^11.0", + "illuminate/support": "^8.0 || ^9.0 || ^10.0 || ^11.0", + "php": "^8.1", + "spatie/laravel-package-tools": "^1.6.3" + }, + "require-dev": { + "ext-json": "*", + "orchestra/testbench": "^6.23 || ^7.0 || ^8.0 || ^9.0", + "pestphp/pest": "^1.20 || ^2.0" + }, + "type": "library", + "extra": { + "laravel": { + "providers": [ + "Spatie\\Activitylog\\ActivitylogServiceProvider" + ] + } + }, + "autoload": { + "files": [ + "src/helpers.php" + ], + "psr-4": { + "Spatie\\Activitylog\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Freek Van der Herten", + "email": "freek@spatie.be", + "homepage": "https://spatie.be", + "role": "Developer" + }, + { + "name": "Sebastian De Deyne", + "email": "sebastian@spatie.be", + "homepage": "https://spatie.be", + "role": "Developer" + }, + { + "name": "Tom Witkowski", + "email": "dev.gummibeer@gmail.com", + "homepage": "https://gummibeer.de", + "role": "Developer" + } + ], + "description": "A very simple activity logger to monitor the users of your website or application", + "homepage": "https://github.com/spatie/activitylog", + "keywords": [ + "activity", + "laravel", + "log", + "spatie", + "user" + ], + "support": { + "issues": "https://github.com/spatie/laravel-activitylog/issues", + "source": "https://github.com/spatie/laravel-activitylog/tree/4.8.0" + }, + "funding": [ + { + "url": "https://spatie.be/open-source/support-us", + "type": "custom" + }, + { + "url": "https://github.com/spatie", + "type": "github" + } + ], + "time": "2024-03-08T22:28:17+00:00" + }, + { + "name": "spatie/laravel-package-tools", + "version": "1.16.5", + "source": { + "type": "git", + "url": "https://github.com/spatie/laravel-package-tools.git", + "reference": "c7413972cf22ffdff97b68499c22baa04eddb6a2" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/spatie/laravel-package-tools/zipball/c7413972cf22ffdff97b68499c22baa04eddb6a2", + "reference": "c7413972cf22ffdff97b68499c22baa04eddb6a2", + "shasum": "" + }, + "require": { + "illuminate/contracts": "^9.28|^10.0|^11.0", + "php": "^8.0" + }, + "require-dev": { + "mockery/mockery": "^1.5", + "orchestra/testbench": "^7.7|^8.0", + "pestphp/pest": "^1.22", + "phpunit/phpunit": "^9.5.24", + "spatie/pest-plugin-test-time": "^1.1" + }, + "type": "library", + "autoload": { + "psr-4": { + "Spatie\\LaravelPackageTools\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Freek Van der Herten", + "email": "freek@spatie.be", + "role": "Developer" + } + ], + "description": "Tools for creating Laravel packages", + "homepage": "https://github.com/spatie/laravel-package-tools", + "keywords": [ + "laravel-package-tools", + "spatie" + ], + "support": { + "issues": "https://github.com/spatie/laravel-package-tools/issues", + "source": "https://github.com/spatie/laravel-package-tools/tree/1.16.5" + }, + "funding": [ + { + "url": "https://github.com/spatie", + "type": "github" + } + ], + "time": "2024-08-27T18:56:10+00:00" + }, { "name": "symfony/clock", "version": "v7.0.5", @@ -9518,5 +9669,5 @@ "php": "^8.2" }, "platform-dev": [], - "plugin-api-version": "2.6.0" + "plugin-api-version": "2.3.0" } diff --git a/config/activitylog.php b/config/activitylog.php new file mode 100644 index 0000000..5f6afcf --- /dev/null +++ b/config/activitylog.php @@ -0,0 +1,52 @@ + env('ACTIVITY_LOGGER_ENABLED', true), + + /* + * When the clean-command is executed, all recording activities older than + * the number of days specified here will be deleted. + */ + 'delete_records_older_than_days' => 365, + + /* + * If no log name is passed to the activity() helper + * we use this default log name. + */ + 'default_log_name' => 'default', + + /* + * You can specify an auth driver here that gets user models. + * If this is null we'll use the current Laravel auth driver. + */ + 'default_auth_driver' => null, + + /* + * If set to true, the subject returns soft deleted models. + */ + 'subject_returns_soft_deleted_models' => false, + + /* + * This model will be used to log activity. + * It should implement the Spatie\Activitylog\Contracts\Activity interface + * and extend Illuminate\Database\Eloquent\Model. + */ + 'activity_model' => \Spatie\Activitylog\Models\Activity::class, + + /* + * This is the name of the table that will be created by the migration and + * used by the Activity model shipped with this package. + */ + 'table_name' => 'activity_log', + + /* + * This is the database connection that will be used by the migration and + * the Activity model shipped with this package. In case it's not set + * Laravel's database.default will be used instead. + */ + 'database_connection' => env('ACTIVITY_LOGGER_DB_CONNECTION'), +]; diff --git a/database/migrations/2024_09_24_191240_create_activity_log_table.php b/database/migrations/2024_09_24_191240_create_activity_log_table.php new file mode 100644 index 0000000..7c05bc8 --- /dev/null +++ b/database/migrations/2024_09_24_191240_create_activity_log_table.php @@ -0,0 +1,27 @@ +create(config('activitylog.table_name'), function (Blueprint $table) { + $table->bigIncrements('id'); + $table->string('log_name')->nullable(); + $table->text('description'); + $table->nullableMorphs('subject', 'subject'); + $table->nullableMorphs('causer', 'causer'); + $table->json('properties')->nullable(); + $table->timestamps(); + $table->index('log_name'); + }); + } + + public function down() + { + Schema::connection(config('activitylog.database_connection'))->dropIfExists(config('activitylog.table_name')); + } +} diff --git a/database/migrations/2024_09_24_191241_add_event_column_to_activity_log_table.php b/database/migrations/2024_09_24_191241_add_event_column_to_activity_log_table.php new file mode 100644 index 0000000..7b797fd --- /dev/null +++ b/database/migrations/2024_09_24_191241_add_event_column_to_activity_log_table.php @@ -0,0 +1,22 @@ +table(config('activitylog.table_name'), function (Blueprint $table) { + $table->string('event')->nullable()->after('subject_type'); + }); + } + + public function down() + { + Schema::connection(config('activitylog.database_connection'))->table(config('activitylog.table_name'), function (Blueprint $table) { + $table->dropColumn('event'); + }); + } +} diff --git a/database/migrations/2024_09_24_191242_add_batch_uuid_column_to_activity_log_table.php b/database/migrations/2024_09_24_191242_add_batch_uuid_column_to_activity_log_table.php new file mode 100644 index 0000000..8f7db66 --- /dev/null +++ b/database/migrations/2024_09_24_191242_add_batch_uuid_column_to_activity_log_table.php @@ -0,0 +1,22 @@ +table(config('activitylog.table_name'), function (Blueprint $table) { + $table->uuid('batch_uuid')->nullable()->after('properties'); + }); + } + + public function down() + { + Schema::connection(config('activitylog.database_connection'))->table(config('activitylog.table_name'), function (Blueprint $table) { + $table->dropColumn('batch_uuid'); + }); + } +} diff --git a/public/hot b/public/hot index b727929..f762bcf 100644 --- a/public/hot +++ b/public/hot @@ -1 +1 @@ -http://127.0.0.1:5173 \ No newline at end of file +http://[::1]:5173 \ No newline at end of file diff --git a/resources/views/activity_log/all.blade.php b/resources/views/activity_log/all.blade.php new file mode 100644 index 0000000..b1685be --- /dev/null +++ b/resources/views/activity_log/all.blade.php @@ -0,0 +1,29 @@ +@extends('template.master') +@section('title', 'User') +@section('content') +
+

User Activity Log

+ + + + + + + + + + + + @foreach ($activities as $activity) + + + + + + + @endforeach + +
#DescriptionByLogged At
{{ $loop->iteration }}{{ $activity->description }}{{ $activity->causer->name ?? 'System' }}{{ $activity->created_at->format('Y-m-d H:i:s') }}
+ +
+@endsection diff --git a/resources/views/activity_log/index.blade.php b/resources/views/activity_log/index.blade.php new file mode 100644 index 0000000..0d9887b --- /dev/null +++ b/resources/views/activity_log/index.blade.php @@ -0,0 +1,65 @@ +@extends('template.master') +@section('title', 'User') +@section('content') +
+

User Activity Log

+ + + + + + + + + + + + @foreach ($activities as $activity) + + + + + + + @endforeach + +
#DescriptionByLogged At
{{ $loop->iteration }}{{ $activity->description }}{{ $activity->causer->name ?? 'System' }}{{ $activity->created_at->format('Y-m-d H:i:s') }}
+ + + + + + See All +
+@endsection diff --git a/resources/views/template/include/_navbar.blade.php b/resources/views/template/include/_navbar.blade.php index d62fc39..b353177 100644 --- a/resources/views/template/include/_navbar.blade.php +++ b/resources/views/template/include/_navbar.blade.php @@ -83,7 +83,7 @@ class="float-start mb-2 ms-2">Mark all as read
  • Profil
  • -
  • Activity
  • +
  • Activity
  • Setting
  • diff --git a/routes/web.php b/routes/web.php index 8ad5608..5ad5066 100644 --- a/routes/web.php +++ b/routes/web.php @@ -19,6 +19,7 @@ use App\Http\Controllers\UserController; use App\Models\User; use Illuminate\Support\Facades\Route; +use App\Http\Controllers\ActivityController; /* |-------------------------------------------------------------------------- @@ -67,6 +68,8 @@ }); Route::group(['middleware' => ['auth', 'checkRole:Super,Admin,Customer']], function () { + Route::get('/activity-log', [ActivityController::class, 'index'])->name('activity-log.index'); + Route::get('/activity-log/all', [ActivityController::class, 'all'])->name('activity-log.all'); Route::resource('user', UserController::class)->only([ 'show', ]);