Skip to content

Commit 3913d94

Browse files
author
Riccardo Dalla Via
authored
ADD canRun action (#14)
1 parent 27dbf33 commit 3913d94

11 files changed

+132
-75
lines changed

README.md

+49-9
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,33 @@ This is the contents of the published config file:
3030

3131
```php
3232
return [
33+
3334
/*
3435
|--------------------------------------------------------------------------
35-
| Sortable permission action
36+
| See sortable action permission
3637
|--------------------------------------------------------------------------
3738
|
3839
| Here you may specify the fully qualified class name of the invokable class
39-
| used to determine whether a user can see and perform sorts to a given model
40-
| or not.
40+
| used to determine whether a user can see sortable actions or not.
4141
| If null, all users who have access to Nova will have the permission.
4242
|
4343
*/
4444

4545
'can_see_sortable_action' => null,
4646

47+
/*
48+
|--------------------------------------------------------------------------
49+
| Run sortable action permission
50+
|--------------------------------------------------------------------------
51+
|
52+
| Here you may specify the fully qualified class name of the invokable class
53+
| used to determine whether a user can sort a given model or not.
54+
| If null, all users who have access to Nova will have the permission.
55+
|
56+
*/
57+
58+
'can_run_sortable_action' => null,
59+
4760
];
4861
```
4962

@@ -71,10 +84,10 @@ use Maize\NovaEloquentSortable\Actions\MoveToStartAction;
7184
public function actions(NovaRequest $request)
7285
{
7386
return [
74-
MoveOrderDownAction::for($this),
75-
MoveToEndAction::for($this),
76-
MoveOrderUpAction::for($this),
77-
MoveToStartAction::for($this),
87+
MoveOrderDownAction::make(),
88+
MoveToEndAction::make(),
89+
MoveOrderUpAction::make(),
90+
MoveToStartAction::make(),
7891
];
7992
}
8093
```
@@ -127,7 +140,7 @@ The action is automatically hidden when the model is already in the first positi
127140

128141
By default, all users who have access to Laravel Nova will be able to see all included sort actions.
129142

130-
If you want to restrict their visibility to some users, you can define a custom `CanSeeSortableAction` invokable class.
143+
If you want to restrict their visibility for some users, you can define a custom `CanSeeSortableAction` invokable class.
131144

132145
Here's an example class checking user's permissions:
133146

@@ -136,7 +149,7 @@ use Laravel\Nova\Http\Requests\NovaRequest;
136149

137150
class CanSeeSortableAction
138151
{
139-
public function __invoke(NovaRequest $request, $model = null, $resource = null): bool
152+
public function __invoke(NovaRequest $request): bool
140153
{
141154
return $request->user()->can('sort_models');
142155
}
@@ -149,6 +162,33 @@ Once done, all you have to do is reference your custom class in `can_see_sortabl
149162
'can_see_sortable_action' => \Path\To\CanSeeSortableAction::class,
150163
```
151164

165+
## Define a custom run permission
166+
167+
By default, all users who have access to Laravel Nova will be able to run all included sort actions.
168+
169+
If you want to restrict the permission for some users, you can define a custom `CanRunSortableAction` invokable class.
170+
171+
Here's an example class checking user's permissions:
172+
173+
```php
174+
use Illuminate\Database\Eloquent\Model;
175+
use Laravel\Nova\Http\Requests\NovaRequest;
176+
177+
class CanRunSortableAction
178+
{
179+
public function __invoke(NovaRequest $request, Model $model): bool
180+
{
181+
return $request->user()->can('sort_model', $model);
182+
}
183+
}
184+
```
185+
186+
Once done, all you have to do is reference your custom class in `can_run_sortable_action` attribute under `config/nova-eloquent-sortable.php`:
187+
188+
``` php
189+
'can_run_sortable_action' => \Path\To\CanRunSortableAction::class,
190+
```
191+
152192
## Testing
153193

154194
```bash

config/nova-eloquent-sortable.php

+16-3
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,31 @@
11
<?php
22

33
return [
4+
45
/*
56
|--------------------------------------------------------------------------
6-
| Sortable permission action
7+
| See sortable action permission
78
|--------------------------------------------------------------------------
89
|
910
| Here you may specify the fully qualified class name of the invokable class
10-
| used to determine whether a user can see and perform sorts to a given model
11-
| or not.
11+
| used to determine whether a user can see sortable actions or not.
1212
| If null, all users who have access to Nova will have the permission.
1313
|
1414
*/
1515

1616
'can_see_sortable_action' => null,
1717

18+
/*
19+
|--------------------------------------------------------------------------
20+
| Run sortable action permission
21+
|--------------------------------------------------------------------------
22+
|
23+
| Here you may specify the fully qualified class name of the invokable class
24+
| used to determine whether a user can sort a given model or not.
25+
| If null, all users who have access to Nova will have the permission.
26+
|
27+
*/
28+
29+
'can_run_sortable_action' => null,
30+
1831
];

src/Actions/CanRunSortableAction.php

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Maize\NovaEloquentSortable\Actions;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
use Laravel\Nova\Http\Requests\NovaRequest;
7+
8+
class CanRunSortableAction
9+
{
10+
public function __invoke(NovaRequest $request, Model $model): bool
11+
{
12+
return true;
13+
}
14+
}

src/Actions/CanSeeSortableAction.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
class CanSeeSortableAction
88
{
9-
public function __invoke(NovaRequest $request, $model = null, $resource = null): bool
9+
public function __invoke(NovaRequest $request): bool
1010
{
1111
return true;
1212
}

src/Actions/EloquentSortableAction.php

+9-13
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,28 @@
22

33
namespace Maize\NovaEloquentSortable\Actions;
44

5+
use Illuminate\Database\Eloquent\Model;
56
use Laravel\Nova\Actions\Action;
67
use Laravel\Nova\Http\Requests\NovaRequest;
7-
use Laravel\Nova\Resource;
88
use Maize\NovaEloquentSortable\Support\Config;
99

1010
abstract class EloquentSortableAction extends Action
1111
{
12-
public static function for(Resource $resource): self
12+
public function __construct()
1313
{
14-
return static::make()
15-
->withoutConfirmation()
14+
$this
1615
->onlyInline()
17-
->canSee(fn (NovaRequest $request) => static::canSeeSortable(
18-
$request,
19-
$resource->model(),
20-
$resource
21-
));
16+
->canSee(fn (NovaRequest $request) => static::canSeeSortable($request))
17+
->canRun(fn (NovaRequest $request, Model $model) => static::canRunSortable($request, $model));
2218
}
2319

24-
public static function canSeeSortable(NovaRequest $request, $model = null, $resource = null): bool
20+
public static function canSeeSortable(NovaRequest $request): bool
2521
{
26-
return Config::getCanSeeSortableAction()($request, $model, $resource);
22+
return Config::getCanSeeSortableAction()($request);
2723
}
2824

29-
public static function isUriKey(?string $uri): bool
25+
public static function canRunSortable(NovaRequest $request, Model $model): bool
3026
{
31-
return $uri === static::make()->uriKey();
27+
return Config::getCanRunSortableAction()($request, $model);
3228
}
3329
}

src/Actions/MoveOrderDownAction.php

+4-7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Maize\NovaEloquentSortable\Actions;
44

5+
use Illuminate\Database\Eloquent\Model;
56
use Illuminate\Support\Collection;
67
use Laravel\Nova\Actions\Action;
78
use Laravel\Nova\Fields\ActionFields;
@@ -14,17 +15,13 @@ public function name(): string
1415
return __('Move order down');
1516
}
1617

17-
public static function canSeeSortable(NovaRequest $request, $model = null, $resource = null): bool
18+
public static function canRunSortable(NovaRequest $request, Model $model): bool
1819
{
19-
if ($model?->isLastInOrder()) {
20+
if ($model->isLastInOrder()) {
2021
return false;
2122
}
2223

23-
if (static::isUriKey($request->action)) {
24-
return true;
25-
}
26-
27-
return parent::canSeeSortable($request, $model, $resource);
24+
return parent::canRunSortable($request, $model);
2825
}
2926

3027
public function handle(ActionFields $fields, Collection $models): mixed

src/Actions/MoveOrderUpAction.php

+4-7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Maize\NovaEloquentSortable\Actions;
44

5+
use Illuminate\Database\Eloquent\Model;
56
use Illuminate\Support\Collection;
67
use Laravel\Nova\Actions\Action;
78
use Laravel\Nova\Fields\ActionFields;
@@ -14,17 +15,13 @@ public function name(): string
1415
return __('Move order up');
1516
}
1617

17-
public static function canSeeSortable(NovaRequest $request, $model = null, $resource = null): bool
18+
public static function canRunSortable(NovaRequest $request, Model $model): bool
1819
{
19-
if ($model?->isFirstInOrder()) {
20+
if ($model->isFirstInOrder()) {
2021
return false;
2122
}
2223

23-
if (static::isUriKey($request->action)) {
24-
return true;
25-
}
26-
27-
return parent::canSeeSortable($request, $model, $resource);
24+
return parent::canRunSortable($request, $model);
2825
}
2926

3027
public function handle(ActionFields $fields, Collection $models): mixed

src/Actions/MoveToEndAction.php

+4-7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Maize\NovaEloquentSortable\Actions;
44

5+
use Illuminate\Database\Eloquent\Model;
56
use Illuminate\Support\Collection;
67
use Laravel\Nova\Actions\Action;
78
use Laravel\Nova\Fields\ActionFields;
@@ -14,17 +15,13 @@ public function name(): string
1415
return __('Move to end');
1516
}
1617

17-
public static function canSeeSortable(NovaRequest $request, $model = null, $resource = null): bool
18+
public static function canRunSortable(NovaRequest $request, Model $model): bool
1819
{
19-
if ($model?->isLastInOrder()) {
20+
if ($model->isLastInOrder()) {
2021
return false;
2122
}
2223

23-
if (static::isUriKey($request->action)) {
24-
return true;
25-
}
26-
27-
return parent::canSeeSortable($request, $model, $resource);
24+
return parent::canRunSortable($request, $model);
2825
}
2926

3027
public function handle(ActionFields $fields, Collection $models): mixed

src/Actions/MoveToStartAction.php

+4-7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Maize\NovaEloquentSortable\Actions;
44

5+
use Illuminate\Database\Eloquent\Model;
56
use Illuminate\Support\Collection;
67
use Laravel\Nova\Actions\Action;
78
use Laravel\Nova\Fields\ActionFields;
@@ -14,17 +15,13 @@ public function name(): string
1415
return __('Move to start');
1516
}
1617

17-
public static function canSeeSortable(NovaRequest $request, $model = null, $resource = null): bool
18+
public static function canRunSortable(NovaRequest $request, Model $model): bool
1819
{
19-
if ($model?->isFirstInOrder()) {
20+
if ($model->isFirstInOrder()) {
2021
return false;
2122
}
2223

23-
if (static::isUriKey($request->action)) {
24-
return true;
25-
}
26-
27-
return parent::canSeeSortable($request, $model, $resource);
24+
return parent::canRunSortable($request, $model);
2825
}
2926

3027
public function handle(ActionFields $fields, Collection $models): mixed

src/Support/Config.php

+9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Maize\NovaEloquentSortable\Support;
44

5+
use Maize\NovaEloquentSortable\Actions\CanRunSortableAction;
56
use Maize\NovaEloquentSortable\Actions\CanSeeSortableAction;
67

78
class Config
@@ -13,4 +14,12 @@ public static function getCanSeeSortableAction(): CanSeeSortableAction
1314

1415
return app($action);
1516
}
17+
18+
public static function getCanRunSortableAction(): CanRunSortableAction
19+
{
20+
$action = config('nova-eloquent-sortable.can_run_sortable_action')
21+
?? CanRunSortableAction::class;
22+
23+
return app($action);
24+
}
1625
}

0 commit comments

Comments
 (0)