PHP code example of graphene-ict / nova-permissions
1. Go to this page and download the library: Download graphene-ict/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/ */
graphene-ict / nova-permissions example snippets
// in app/Providers/NovaServiceProvider.php
// ...
public function tools()
{
return [
// ...
new \GrapheneICT\NovaPermissions\NovaPermissions(),
];
}
// ...
use Laravel\Nova\Fields\MorphToMany;
public function fields(Request $request)
{
return [
// ...
MorphToMany::make('Roles', 'roles', \GrapheneICT\NovaPermissions\Nova\Role::class),
MorphToMany::make('Permissions', 'permissions', \GrapheneICT\NovaPermissions\Nova\Permission::class),
];
}
use Illuminate\Foundation\Auth\User as Authenticatable;
use Spatie\Permission\Traits\HasRoles;
class User extends Authenticatable
{
use HasRoles;
// ...
}
class RolesAndPermissionsSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
// Reset cached roles and permissions
app()[PermissionRegistrar::class]->forgetCachedPermissions();
$collection = collect([
User::class,
Role::class,
Permission::class,
// ... // List all your Models you want to have Permissions for.
]);
$adminEmail = env('NOVA_PERMISSION_ADMIN_EMAIL', null);
if (is_null($adminEmail)) {
throw new \InvalidArgumentException('Email parameter must be provided!');
}
// Create an Admin Role and assign all Permissions
$role = Role::create(['name' => 'admin']);
$role->givePermissionTo(Permission::all());
// Give User Admin Role
$user = User::whereEmail($adminEmail)->first(); // Change this to your email.
$user->assignRole('admin');
}
}
class UserPolicy extends Policy
{
/**
* The Permission key the Policy corresponds to.
*
* @var string
*/
public static $key = 'users';
}
class User extends ResourceForUser
{
//...
}
// in app/Providers/NovaServiceProvider.php
// ...
public function tools()
{
return [
// ...
\GrapheneICT\NovaPermissions\NovaPermissionTool::make()
->roleResource(Role::class)
->permissionResource(Permission::class),
];
}