Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Jan 6, 2024
1 parent 9741c82 commit 548fbd9
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 26 deletions.
30 changes: 30 additions & 0 deletions src/Resources/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,36 @@ public function getPolicy(): mixed
return Gate::getPolicyFor($this->getModel());
}

/**
* Map the resource abilities.
*/
public function mapAbilities(): array
{
return [
'viewAny' => function (Request $request): bool {
return is_null($this->getPolicy()) || Gate::allows('viewAny', $this->getModel());
},
'create' => function (Request $request): bool {
return is_null($this->getPolicy()) || Gate::allows('create', $this->getModel());
},
'view' => function (Request $request, Model $model): bool {
return is_null($this->getPolicy()) || Gate::allows('view', $model);
},
'update' => function (Request $request, Model $model): bool {
return is_null($this->getPolicy()) || Gate::allows('update', $model);
},
'delete' => function (Request $request, Model $model): bool {
return is_null($this->getPolicy()) || Gate::allows('delete', $model);
},
'forceDelete' => function (Request $request, Model $model): bool {
return is_null($this->getPolicy()) || Gate::allows('delete', $model);
},
'restore' => function (Request $request, Model $model): bool {
return is_null($this->getPolicy()) || Gate::allows('delete', $model);
},
];
}

/**
* Set the relations to eagerload.
*/
Expand Down
28 changes: 2 additions & 26 deletions src/Traits/MapsAbilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,15 @@

namespace Cone\Root\Traits;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;

trait MapsAbilities
{
/**
/**
* Map the resource abilities.
*/
public function mapAbilities(): array
{
return [
'viewAny' => function (Request $request): bool {
return is_null($this->getPolicy()) || Gate::allows('viewAny', $this->getModel());
},
'create' => function (Request $request): bool {
return is_null($this->getPolicy()) || Gate::allows('create', $this->getModel());
},
'view' => function (Request $request, Model $model): bool {
return is_null($this->getPolicy()) || Gate::allows('view', $model);
},
'update' => function (Request $request, Model $model): bool {
return is_null($this->getPolicy()) || Gate::allows('update', $model);
},
'delete' => function (Request $request, Model $model): bool {
return is_null($this->getPolicy()) || Gate::allows('delete', $model);
},
'forceDelete' => function (Request $request, Model $model): bool {
return is_null($this->getPolicy()) || Gate::allows('delete', $model);
},
'restore' => function (Request $request, Model $model): bool {
return is_null($this->getPolicy()) || Gate::allows('delete', $model);
},
//
];
}
}

0 comments on commit 548fbd9

Please sign in to comment.