Skip to content

Commit a93d76e

Browse files
authored
Merge pull request #46 from tiagof/feature/add-canView
Add View Modal and corresponding canView authorization.
2 parents 6c7f7ec + 76aa44c commit a93d76e

4 files changed

Lines changed: 56 additions & 14 deletions

File tree

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,21 @@ protected static function getEditEventFormSchema(): array
350350

351351
## Authorizing actions
352352

353+
If you want to authorize the `view` action, you can override the default authorization methods that comes with this package.
354+
355+
```php
356+
public static function canView(?array $event = null): bool
357+
{
358+
// When event is null, MAKE SURE you allow View otherwise the entire widget/calendar won't be rendered
359+
if ($event === null) {
360+
return true;
361+
}
362+
363+
// Returning 'false' will not show the event Modal.
364+
return true;
365+
}
366+
```
367+
353368
If you want to authorize the `edit` or `create` action, you can override the default authorization methods that comes with this package.
354369

355370
```php

resources/views/components/edit-event-modal.blade.php

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
<x-filament::modal id="fullcalendar--edit-event-modal" :width="$this->getModalWidth()">
33
<x-slot name="header">
44
<x-filament::modal.heading>
5-
{{ __('filament::resources/pages/edit-record.title', ['label' => $this->getModalLabel()]) }}
5+
{{
6+
$this->editEventForm->isDisabled()
7+
? __('filament::resources/pages/view-record.title', ['label' => $this->getModalLabel()])
8+
: __('filament::resources/pages/edit-record.title', ['label' => $this->getModalLabel()])
9+
}}
610
</x-filament::modal.heading>
711
</x-slot>
812

@@ -13,19 +17,30 @@
1317
{{ $this->editEventForm }}
1418

1519
<x-slot name="footer">
16-
<x-filament::button type="submit" form="onEditEventSubmit">
17-
{{ __('filament::resources/pages/edit-record.form.actions.save.label') }}
18-
</x-filament::button>
20+
@if(!$this->editEventForm->isDisabled())
21+
<x-filament::button type="submit" form="onEditEventSubmit">
22+
{{ __('filament::resources/pages/edit-record.form.actions.save.label') }}
23+
</x-filament::button>
24+
@endif
1925

2026
@if($this->isListeningCancelledEditModal())
21-
<x-filament::button color="secondary" x-on:click="isOpen = false; Livewire.emit('cancelledFullcalendarEditEventModal')">
22-
{{ __('filament::resources/pages/edit-record.form.actions.cancel.label') }}
27+
<x-filament::button color="secondary"
28+
x-on:click="isOpen = false; Livewire.emit('cancelledFullcalendarEditEventModal')">
29+
{{
30+
$this->editEventForm->isDisabled()
31+
? __('filament-support::actions/view.single.modal.actions.close.label')
32+
: __('filament::resources/pages/edit-record.form.actions.cancel.label')
33+
}}
2334
</x-filament::button>
2435
@else
2536
<x-filament::button color="secondary" x-on:click="isOpen = false">
26-
{{ __('filament::resources/pages/edit-record.form.actions.cancel.label') }}
37+
{{
38+
$this->editEventForm->isDisabled()
39+
? __('filament-support::actions/view.single.modal.actions.close.label')
40+
: __('filament::resources/pages/edit-record.form.actions.cancel.label')
41+
}}
2742
</x-filament::button>
28-
@endif
43+
@endif
2944
</x-slot>
3045
</x-filament::modal>
3146
</x-filament::form>

src/Widgets/Concerns/AuthorizesActions.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@
44

55
trait AuthorizesActions
66
{
7+
public static function canView(?array $event = null): bool
8+
{
9+
// If we want to prevent breaking changes, we need to "fallback"
10+
// to "canEdit(Event)". Not doing that since the new behaviour
11+
// is likely the desired one. The fallback would be:
12+
// return static::canEdit($event);
13+
14+
return true;
15+
}
16+
717
public static function canCreate(): bool
818
{
919
return true;

src/Widgets/Concerns/CanManageEvents.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@
33
namespace Saade\FilamentFullCalendar\Widgets\Concerns;
44

55
use Closure;
6+
use Filament\Forms\ComponentContainer;
67
use Illuminate\Database\Eloquent\Model;
78
use Illuminate\Support\Carbon;
89
use Saade\FilamentFullCalendar\Widgets\Forms\CreateEventForm;
910
use Saade\FilamentFullCalendar\Widgets\Forms\EditEventForm;
1011
use Saade\FilamentFullCalendar\Widgets\FullCalendarWidget;
1112

1213
/**
13-
* @property \Filament\Forms\ComponentContainer $createEventForm
14-
* @property \Filament\Forms\ComponentContainer $editEventForm
14+
* @property ComponentContainer $createEventForm
15+
* @property ComponentContainer $editEventForm
1516
*/
1617
trait CanManageEvents
1718
{
@@ -22,7 +23,6 @@ trait CanManageEvents
2223
use EvaluateClosures;
2324

2425
public ?int $event_id = null;
25-
2626
public ?Model $event = null;
2727

2828
protected function setUpForms(): void
@@ -46,11 +46,13 @@ protected function getForms(): array
4646

4747
public function onEventClick($event): void
4848
{
49-
if (! static::canEdit($event)) {
49+
if (!static::canView($event)) {
5050
return;
5151
}
5252

53-
$this->editEventForm->fill($event);
53+
$this->editEventForm
54+
->disabled(!static::canView($event))
55+
->fill($event);
5456

5557
if (method_exists($this, 'resolveEventRecord')) {
5658
$this->event = $this->resolveEventRecord($event);
@@ -63,7 +65,7 @@ public function onEventClick($event): void
6365

6466
public function onCreateEventClick(array $date): void
6567
{
66-
if (! static::canCreate()) {
68+
if (!static::canCreate()) {
6769
return;
6870
}
6971

0 commit comments

Comments
 (0)