diff --git a/app/Http/Controllers/FrontEnd/DownloadController.php b/app/Http/Controllers/FrontEnd/DownloadController.php index e4e633434..3403afa99 100644 --- a/app/Http/Controllers/FrontEnd/DownloadController.php +++ b/app/Http/Controllers/FrontEnd/DownloadController.php @@ -36,6 +36,7 @@ use App\Models\Prosedur; use App\Models\Regulasi; use App\Models\FormDokumen; +use Illuminate\Support\Carbon; use Yajra\DataTables\DataTables; class DownloadController extends FrontEndController @@ -136,6 +137,13 @@ public function getDataByJenisDokumen($slug) public function downloadDokumen(FormDokumen $file) { + // Terapkan filter yang sama dengan daftar publik agar dokumen yang + // belum dipublikasi atau sudah kedaluwarsa tidak bisa diunduh (404). + $isPublished = (bool) $file->is_published; + $isActive = $file->expired_at === null || Carbon::parse($file->expired_at)->isFuture(); + + abort_unless($isPublished && $isActive, 404); + return response()->download($file->file_dokumen); } } diff --git a/catatan_rilis.md b/catatan_rilis.md index 258ba7b19..927c01b6b 100644 --- a/catatan_rilis.md +++ b/catatan_rilis.md @@ -23,6 +23,7 @@ Terimakasih [isi disini] yang telah berkontribusi langsung mengembangkan aplikas 12. [#1797](https://github.com/OpenSID/OpenDK/issues/1797) Persentase belanja barang/jasa salah dan division by zero di AnggaranRealisasiChartService. 13. [#1798](https://github.com/OpenSID/OpenDK/issues/1798) Perbaikan Cache listDesa terus diperpanjang dan tidak membedakan parameter $all. 14. [#1799](https://github.com/OpenSID/OpenDK/issues/1799) Perbaikan getFeeds() error jika item RSS tidak punya author dan gagal per feed. +15. [#1813](https://github.com/OpenSID/OpenDK/issues/1813) Perbaikan Batasi unduh dokumen hanya untuk dokumen terbit dan belum kedaluwarsa. #### TEKNIS diff --git a/tests/Feature/Frontend/DownloadDokumenVisibilityTest.php b/tests/Feature/Frontend/DownloadDokumenVisibilityTest.php new file mode 100644 index 000000000..2219fd525 --- /dev/null +++ b/tests/Feature/Frontend/DownloadDokumenVisibilityTest.php @@ -0,0 +1,66 @@ +filePath = tempnam(sys_get_temp_dir(), 'dokumen_'); + file_put_contents($this->filePath, 'isi dokumen'); +}); + +afterEach(function () { + if (isset($this->filePath) && is_file($this->filePath)) { + unlink($this->filePath); + } +}); + +function makeFormDokumen(array $attributes, string $filePath): FormDokumen +{ + return FormDokumen::create(array_merge([ + 'nama_dokumen' => 'Dokumen Uji', + 'description' => 'Deskripsi uji', + 'file_dokumen' => $filePath, + 'is_published' => true, + 'published_at' => now(), + 'expired_at' => null, + ], $attributes)); +} + +it('serves a published document that has no expiry date', function () { + $dokumen = makeFormDokumen([], $this->filePath); + + $this->get(route('unduhan.form-dokumen.download', $dokumen->id)) + ->assertOk() + ->assertDownload(); +}); + +it('serves a published document whose expiry date is in the future', function () { + $dokumen = makeFormDokumen(['expired_at' => now()->addDay()], $this->filePath); + + $this->get(route('unduhan.form-dokumen.download', $dokumen->id)) + ->assertOk() + ->assertDownload(); +}); + +it('returns 404 for an unpublished document', function () { + $dokumen = makeFormDokumen(['is_published' => false], $this->filePath); + + $this->get(route('unduhan.form-dokumen.download', $dokumen->id)) + ->assertNotFound(); +}); + +it('returns 404 for an expired document', function () { + $dokumen = makeFormDokumen(['expired_at' => now()->subDay()], $this->filePath); + + $this->get(route('unduhan.form-dokumen.download', $dokumen->id)) + ->assertNotFound(); +}); + +it('returns 404 for an unknown document id', function () { + $this->get(route('unduhan.form-dokumen.download', 999999)) + ->assertNotFound(); +});