diff --git a/app/Http/Controllers/Admin/ActivityLogController.php b/app/Http/Controllers/Admin/ActivityLogController.php new file mode 100644 index 0000000..867e7c5 --- /dev/null +++ b/app/Http/Controllers/Admin/ActivityLogController.php @@ -0,0 +1,56 @@ +whereIn('model_type', [Tour::class, Flight::class]) + ->with('admin') + ->when($request->filled('model_type'), function ($query) use ($request) { + + $query->where('model_type', $request->input('model_type')); + + }) + ->when($request->filled('action'), function ($query) use ($request) { + + $query->where('action', $request->input('action')); + + }) + ->orderByDesc('created_at') + ->paginate(20) + ->withQueryString() + ->through(function ($log) { + + $modelClass = $log->model_type; + + $relatedModel = $modelClass::find($log->model_id); + + $log->model_title = $relatedModel->title ?? '(حذف شده)'; + + $log->model_label = $log->model_type === Tour::class + ? 'تور' + : 'پرواز'; + + $log->model_edit_route = null; + + if ($relatedModel) { + $log->model_edit_route = $log->model_type === Tour::class + ? route('admin.tours.edit', $log->model_id) + : route('admin.flights.edit', $log->model_id); + } + + return $log; + }); + + return view('admin.activity-logs.index', compact('logs')); + } +} diff --git a/app/Models/ActivityLog.php b/app/Models/ActivityLog.php new file mode 100644 index 0000000..d841664 --- /dev/null +++ b/app/Models/ActivityLog.php @@ -0,0 +1,32 @@ + 'array', + 'created_at' => 'datetime', + ]; + + /** + * رابطه: هر لاگ متعلق به یک ادمین است. + */ + public function admin() + { + return $this->belongsTo(Admin::class); + } +} diff --git a/app/Models/AdminPermission.php b/app/Models/AdminPermission.php new file mode 100644 index 0000000..2e26028 --- /dev/null +++ b/app/Models/AdminPermission.php @@ -0,0 +1,79 @@ + 'int', + 'admin_view' => 'bool', + 'admin_create' => 'bool', + 'admin_edit' => 'bool', + 'admin_delete' => 'bool', + 'user_view' => 'bool', + 'user_edit' => 'bool', + 'user_delete' => 'bool', + 'tour_view' => 'bool', + 'tour_create' => 'bool', + 'tour_edit' => 'bool', + 'tour_delete' => 'bool', + 'flight_view' => 'bool', + 'flight_create' => 'bool', + 'flight_edit' => 'bool', + 'flight_delete' => 'bool' + ]; + + protected $fillable = [ + 'admin_id', + 'admin_view', + 'admin_create', + 'admin_edit', + 'admin_delete', + 'user_view', + 'user_edit', + 'user_delete', + 'tour_view', + 'tour_create', + 'tour_edit', + 'tour_delete', + 'flight_view', + 'flight_create', + 'flight_edit', + 'flight_delete' + ]; +} diff --git a/app/Traits/ActivityLog.php b/app/Traits/ActivityLog.php new file mode 100644 index 0000000..5715b26 --- /dev/null +++ b/app/Traits/ActivityLog.php @@ -0,0 +1,8 @@ +writeActivityLog('created', $model->getAttributes()); + }); + + static::updated(function ($model) { + + $changes = $model->getChanges(); + + unset($changes['updated_at']); + + if (empty($changes)) { + return; + } + + $diff = []; + + foreach ($changes as $key => $newValue) { + $diff[$key] = [ + 'old' => $model->getOriginal($key), + 'new' => $newValue, + ]; + } + + $model->writeActivityLog('updated', $diff); + }); + } + + protected function writeActivityLog(string $action, array $data): void + { + ActivityLog::create([ + 'admin_id' => auth('admin')->id(), + 'model_type' => static::class, + 'model_id' => $this->getKey(), + 'action' => $action, + 'changes' => $data, + 'created_at' => now(), + ]); + } +}