1. Go to this page and download the library: Download sereny/nova-permissions library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
sereny / nova-permissions example snippets
// in app/Providers/NovaServiceProvider.php
// ...
public function tools()
{
return [
// ...
new \Sereny\NovaPermissions\NovaPermissions(),
];
}
// in app/Providers/NovaServiceProvider.php
// ...
public function tools()
{
return [
// ...
(new \Sereny\NovaPermissions\NovaPermissions())->canSee(function ($request) {
return $request->user()->isSuperAdmin();
}),
];
}
// ...
use Laravel\Nova\Fields\MorphToMany;
public function fields(Request $request)
{
return [
// ...
MorphToMany::make('Roles', 'roles', \Sereny\NovaPermissions\Nova\Role::class),
MorphToMany::make('Permissions', 'permissions', \Sereny\NovaPermissions\Nova\Permission::class),
];
}
use Illuminate\Foundation\Auth\User as Authenticatable;
use Spatie\Permission\Traits\HasRoles;
class User extends Authenticatable
{
use HasRoles;
// ...
}
use Illuminate\Database\Seeder;
use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;
class RolesAndPermissionsSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
// Reset cached roles and permissions
app()[\Spatie\Permission\PermissionRegistrar::class]->forgetCachedPermissions();
$collection = collect([
'Invoice',
'Client',
'Contact',
'Payment',
'Team',
'User',
'Role',
'Permission'
// ... your own models/permission you want to crate
]);
$collection->each(function ($item, $key) {
// create permissions for each collection item
Permission::create(['group' => $item, 'name' => 'viewAny' . $item]);
Permission::create(['group' => $item, 'name' => 'view' . $item]);
Permission::create(['group' => $item, 'name' => 'update' . $item]);
Permission::create(['group' => $item, 'name' => 'create' . $item]);
Permission::create(['group' => $item, 'name' => 'delete' . $item]);
Permission::create(['group' => $item, 'name' => 'destroy' . $item]);
});
// Create a Super-Admin Role and assign all permissions to it
$role = Role::create(['name' => 'super-admin']);
$role->givePermissionTo(Permission::all());
// Give User Super-Admin Role
$user = \App\Models\User::where('email', '[email protected]')->first(); // enter your email here
$user->assignRole('super-admin');
}
}
namespace App\Policies;
use Sereny\NovaPermissions\Policies\BasePolicy;
class ContactPolicy extends BasePolicy
{
/**
* The Permission key the Policy corresponds to.
*
* @var string
*/
public $key = 'contact';
}
namespace App;
class User {
/**
* Determines if the User is a Super admin
* @return null
*/
public function isSuperAdmin()
{
return $this->hasRole('super-admin');
}
}
// in app/Providers/NovaServiceProvider.php
use App\Nova\Permission;
use App\Nova\Role;
use App\Policies\PermissionPolicy;
use App\Policies\RolePolicy;
// ...
public function tools()
{
return [
// ...
\Sereny\NovaPermissions\NovaPermissions::make()
->roleResource(Role::class)
->permissionResource(Permission::class)
->rolePolicy(RolePolicy::class)
->permissionPolicy(PermissionPolicy::class)
->disablePermissions()
->disableMenu();
->hideFieldsFromRole([
'id',
'guard_name'
])
->hideFieldsFromPermission([
'id',
'guard_name',
'users',
'roles'
])
->resolveGuardsUsing(function($request) {
return [ 'web' ];
})
->resolveModelForGuardUsing(function($request) {
/** @var App\Auth\CustomGuard $guard */
$guard = auth()->guard();
return $guard->getProvider()->getModel();
})
];
}
// Role using UUID primary key
namespace App\Models;
use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Support\Str;
use Sereny\NovaPermissions\Traits\SupportsRole;
use Spatie\Permission\Models\Role as BaseRole;
class Role extends BaseRole
{
use HasUuids,
SupportsRole; // REQUIRED TRAIT
}