Skip to content

Instantly share code, notes, and snippets.

@mathieutu
Created November 22, 2018 08:07
Show Gist options
  • Save mathieutu/d742046ca00fad76c78fc3a6334ddadc to your computer and use it in GitHub Desktop.
Save mathieutu/d742046ca00fad76c78fc3a6334ddadc to your computer and use it in GitHub Desktop.
Nova policies
<?php
namespace App\Policies;
class AllowAllForNova
{
public function viewAny()
{
return $this->sameForAll();
}
public function sameForAll()
{
return true;
}
public function view()
{
return $this->sameForAll();
}
public function create()
{
return $this->sameForAll();
}
public function update()
{
return $this->sameForAll();
}
public function delete()
{
return $this->sameForAll();
}
public function restore()
{
return $this->sameForAll();
}
public function forceDelete()
{
return $this->sameForAll();
}
}
<?php
namespace App\Providers;
use App\Models\Order;
use App\Policies\AllowAllForNova;
use App\Policies\OrderPolicy;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Gate;
use Laravel\Nova\Nova;
class AuthServiceProvider extends ServiceProvider
{
protected $policies = [
Order::class => OrderPolicy::class,
];
protected $novaPolicies = [
];
/**
* Register any authentication / authorization services.
*
* @return void
*/
public function boot()
{
$this->registerPolicies();
Nova::serving(function() {
$this->registerNovaPolicies();
});
}
private function novaPolicies()
{
return collect($this->policies())->map(function () {
return AllowAllForNova::class;
})->merge($this->novaPolicies);
}
public function registerNovaPolicies()
{
foreach ($this->novaPolicies() as $key => $value) {
Gate::policy($key, $value);
}
}
}
@mathieutu
Copy link
Author

tip: in \App\Nova\Resources\Resource

   public static function authorizable()
    {
        return false;
    }
``` 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment