Skip to content

ADD canRun action #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 3, 2023
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
58 changes: 49 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,33 @@ This is the contents of the published config file:

```php
return [

/*
|--------------------------------------------------------------------------
| Sortable permission action
| See sortable action permission
|--------------------------------------------------------------------------
|
| Here you may specify the fully qualified class name of the invokable class
| used to determine whether a user can see and perform sorts to a given model
| or not.
| used to determine whether a user can see sortable actions or not.
| If null, all users who have access to Nova will have the permission.
|
*/

'can_see_sortable_action' => null,

/*
|--------------------------------------------------------------------------
| Run sortable action permission
|--------------------------------------------------------------------------
|
| Here you may specify the fully qualified class name of the invokable class
| used to determine whether a user can sort a given model or not.
| If null, all users who have access to Nova will have the permission.
|
*/

'can_run_sortable_action' => null,

];
```

Expand Down Expand Up @@ -71,10 +84,10 @@ use Maize\NovaEloquentSortable\Actions\MoveToStartAction;
public function actions(NovaRequest $request)
{
return [
MoveOrderDownAction::for($this),
MoveToEndAction::for($this),
MoveOrderUpAction::for($this),
MoveToStartAction::for($this),
MoveOrderDownAction::make(),
MoveToEndAction::make(),
MoveOrderUpAction::make(),
MoveToStartAction::make(),
];
}
```
Expand Down Expand Up @@ -127,7 +140,7 @@ The action is automatically hidden when the model is already in the first positi

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

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

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

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

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

## Define a custom run permission

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

If you want to restrict the permission for some users, you can define a custom `CanRunSortableAction` invokable class.

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

```php
use Illuminate\Database\Eloquent\Model;
use Laravel\Nova\Http\Requests\NovaRequest;

class CanRunSortableAction
{
public function __invoke(NovaRequest $request, Model $model): bool
{
return $request->user()->can('sort_model', $model);
}
}
```

Once done, all you have to do is reference your custom class in `can_run_sortable_action` attribute under `config/nova-eloquent-sortable.php`:

``` php
'can_run_sortable_action' => \Path\To\CanRunSortableAction::class,
```

## Testing

```bash
Expand Down
19 changes: 16 additions & 3 deletions config/nova-eloquent-sortable.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,31 @@
<?php

return [

/*
|--------------------------------------------------------------------------
| Sortable permission action
| See sortable action permission
|--------------------------------------------------------------------------
|
| Here you may specify the fully qualified class name of the invokable class
| used to determine whether a user can see and perform sorts to a given model
| or not.
| used to determine whether a user can see sortable actions or not.
| If null, all users who have access to Nova will have the permission.
|
*/

'can_see_sortable_action' => null,

/*
|--------------------------------------------------------------------------
| Run sortable action permission
|--------------------------------------------------------------------------
|
| Here you may specify the fully qualified class name of the invokable class
| used to determine whether a user can sort a given model or not.
| If null, all users who have access to Nova will have the permission.
|
*/

'can_run_sortable_action' => null,

];
14 changes: 14 additions & 0 deletions src/Actions/CanRunSortableAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Maize\NovaEloquentSortable\Actions;

use Illuminate\Database\Eloquent\Model;
use Laravel\Nova\Http\Requests\NovaRequest;

class CanRunSortableAction
{
public function __invoke(NovaRequest $request, Model $model): bool
{
return true;
}
}
2 changes: 1 addition & 1 deletion src/Actions/CanSeeSortableAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class CanSeeSortableAction
{
public function __invoke(NovaRequest $request, $model = null, $resource = null): bool
public function __invoke(NovaRequest $request): bool
{
return true;
}
Expand Down
22 changes: 9 additions & 13 deletions src/Actions/EloquentSortableAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,28 @@

namespace Maize\NovaEloquentSortable\Actions;

use Illuminate\Database\Eloquent\Model;
use Laravel\Nova\Actions\Action;
use Laravel\Nova\Http\Requests\NovaRequest;
use Laravel\Nova\Resource;
use Maize\NovaEloquentSortable\Support\Config;

abstract class EloquentSortableAction extends Action
{
public static function for(Resource $resource): self
public function __construct()
{
return static::make()
->withoutConfirmation()
$this
->onlyInline()
->canSee(fn (NovaRequest $request) => static::canSeeSortable(
$request,
$resource->model(),
$resource
));
->canSee(fn (NovaRequest $request) => static::canSeeSortable($request))
->canRun(fn (NovaRequest $request, Model $model) => static::canRunSortable($request, $model));
}

public static function canSeeSortable(NovaRequest $request, $model = null, $resource = null): bool
public static function canSeeSortable(NovaRequest $request): bool
{
return Config::getCanSeeSortableAction()($request, $model, $resource);
return Config::getCanSeeSortableAction()($request);
}

public static function isUriKey(?string $uri): bool
public static function canRunSortable(NovaRequest $request, Model $model): bool
{
return $uri === static::make()->uriKey();
return Config::getCanRunSortableAction()($request, $model);
}
}
11 changes: 4 additions & 7 deletions src/Actions/MoveOrderDownAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Maize\NovaEloquentSortable\Actions;

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

public static function canSeeSortable(NovaRequest $request, $model = null, $resource = null): bool
public static function canRunSortable(NovaRequest $request, Model $model): bool
{
if ($model?->isLastInOrder()) {
if ($model->isLastInOrder()) {
return false;
}

if (static::isUriKey($request->action)) {
return true;
}

return parent::canSeeSortable($request, $model, $resource);
return parent::canRunSortable($request, $model);
}

public function handle(ActionFields $fields, Collection $models): mixed
Expand Down
11 changes: 4 additions & 7 deletions src/Actions/MoveOrderUpAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Maize\NovaEloquentSortable\Actions;

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

public static function canSeeSortable(NovaRequest $request, $model = null, $resource = null): bool
public static function canRunSortable(NovaRequest $request, Model $model): bool
{
if ($model?->isFirstInOrder()) {
if ($model->isFirstInOrder()) {
return false;
}

if (static::isUriKey($request->action)) {
return true;
}

return parent::canSeeSortable($request, $model, $resource);
return parent::canRunSortable($request, $model);
}

public function handle(ActionFields $fields, Collection $models): mixed
Expand Down
11 changes: 4 additions & 7 deletions src/Actions/MoveToEndAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Maize\NovaEloquentSortable\Actions;

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

public static function canSeeSortable(NovaRequest $request, $model = null, $resource = null): bool
public static function canRunSortable(NovaRequest $request, Model $model): bool
{
if ($model?->isLastInOrder()) {
if ($model->isLastInOrder()) {
return false;
}

if (static::isUriKey($request->action)) {
return true;
}

return parent::canSeeSortable($request, $model, $resource);
return parent::canRunSortable($request, $model);
}

public function handle(ActionFields $fields, Collection $models): mixed
Expand Down
11 changes: 4 additions & 7 deletions src/Actions/MoveToStartAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Maize\NovaEloquentSortable\Actions;

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

public static function canSeeSortable(NovaRequest $request, $model = null, $resource = null): bool
public static function canRunSortable(NovaRequest $request, Model $model): bool
{
if ($model?->isFirstInOrder()) {
if ($model->isFirstInOrder()) {
return false;
}

if (static::isUriKey($request->action)) {
return true;
}

return parent::canSeeSortable($request, $model, $resource);
return parent::canRunSortable($request, $model);
}

public function handle(ActionFields $fields, Collection $models): mixed
Expand Down
9 changes: 9 additions & 0 deletions src/Support/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Maize\NovaEloquentSortable\Support;

use Maize\NovaEloquentSortable\Actions\CanRunSortableAction;
use Maize\NovaEloquentSortable\Actions\CanSeeSortableAction;

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

return app($action);
}

public static function getCanRunSortableAction(): CanRunSortableAction
{
$action = config('nova-eloquent-sortable.can_run_sortable_action')
?? CanRunSortableAction::class;

return app($action);
}
}
Loading