Skip to content
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ business), so the add-on asks only whether a customer needs documents at all.
number) and Delete; one upload box; and Download all, one zip of the customer's documents for an audit. ID
numbers are shown masked (`xxxx xxxx 4821`); **Show** reveals the full number.
- **Nothing is lost by accident:** Delete hides a document; **Deleted documents** (a button in this add-on's
settings) restores it or erases it for good. Uploading a type the customer already has replaces the file (it asks
settings) opens it to look, restores it or erases it for good. Uploading a type the customer already has replaces the file (it asks
first on the customer page) and hides the old one the same way. Documents stay when a customer is deleted.
- **Everything is on record:** every view, Show, download, upload, change, delete, restore, erase and export is
written to the Activity log (never the number or the file).
Expand Down
1 change: 1 addition & 0 deletions resources/views/livewire/deleted/deleted.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<x-ui.td>{{ $row['type'] }}</x-ui.td>
<x-ui.td>{{ __(':date by :name', ['date' => $row['deleted'], 'name' => $row['by']]) }}</x-ui.td>
<x-ui.td class="text-end whitespace-nowrap">
<x-ui.button size="sm" variant="ghost" icon="eye" href="{{ route('documents.file', $row['id']) }}" target="_blank" rel="noopener">{{ __('View') }}</x-ui.button>
<x-ui.button size="sm" variant="ghost" icon="arrow-uturn-left" wire:click="restore({{ $row['id'] }})">{{ __('Restore') }}</x-ui.button>
@if ($this->canErase)
<x-ui.button
Expand Down
10 changes: 6 additions & 4 deletions src/Http/DocumentFileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,20 @@
use Symfony\Component\HttpFoundation\Response;

/**
* Opens one current document, decrypted, in the browser (pictures and PDFs) for staff with View customer documents.
* Every view goes to the Activity log.
* Opens one document, decrypted, in the browser (pictures and PDFs) for staff with View customer documents. A deleted
* (hidden) one opens only for staff who may restore it, so they can look before they restore or erase; to everyone
* else it does not exist. Every view goes to the Activity log, a deleted one marked as such.
*/
final class DocumentFileController
{
public function __invoke(int $document, StoredFile $files, CustomerDocuments $documents): Response
{
abort_unless(auth()->user()?->can(Abilities::VIEW), 403);
$row = Document::query()->with('type')->findOrFail($document);
$row = Document::withTrashed()->with('type')->findOrFail($document);
abort_if($row->trashed() && ! auth()->user()->can(Abilities::RESTORE), 404);
$account = Account::withTrashed()->findOrFail($row->account_id);

$documents->log('document-viewed', $account, $row->type->name, auth()->user());
$documents->log('document-viewed', $account, $row->type->name, auth()->user(), $row->trashed() ? ['deleted' => true] : []);

return PrivateFile::contentResponse(
$files->read($row),
Expand Down
16 changes: 15 additions & 1 deletion tests/Feature/CustomerCardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,23 @@ function card(Account $account): Testable
->and(Activity::query()->where('event', 'document-viewed')->sole()->subject_id)->toBe($this->account->id);
});

it('does not open a hidden document from the card route', function () {
it('opens a hidden document only for staff who may restore it, and logs it as deleted', function () {
app(CustomerDocuments::class)->delete($this->document, $this->admin);

$this->actingAs(staffWith(Abilities::VIEW));
$this->get(route('documents.file', $this->document))->assertNotFound();

$this->actingAs(staffWith(Abilities::RESTORE));
$response = $this->get(route('documents.file', $this->document))->assertOk()->assertHeader('Content-Type', 'image/png');

expect($response->getContent())->toBe(pngBytes())
->and(Activity::query()->where('event', 'document-viewed')->sole()->properties['deleted'])->toBeTrue();
});

it('does not open an erased document', function () {
$this->actingAs($this->admin);
app(CustomerDocuments::class)->delete($this->document, $this->admin);
app(CustomerDocuments::class)->erase(Document::onlyTrashed()->findOrFail($this->document->id), $this->admin);

$this->get(route('documents.file', $this->document))->assertNotFound();
});
Expand Down
8 changes: 8 additions & 0 deletions tests/Feature/ListPagesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@
expect($document->fresh())->not->toBeNull();
});

it('links each deleted document to its file', function () {
$this->actingAs($this->admin);
$document = $this->documents->upload(Account::factory()->create(), $this->aadhaar, pngUpload(), null, $this->admin);
$this->documents->delete($document, $this->admin);

Livewire::test('documents::deleted')->assertSee(route('documents.file', $document->id));
});

it('needs Restore deleted documents for the page', function () {
$this->actingAs(staffWith(Abilities::VIEW, Abilities::EDIT));

Expand Down