PHP code example of baleethai / nova-permissions

1. Go to this page and download the library: Download baleethai/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/ */

    

baleethai / nova-permissions example snippets


// in app/Providers/NovaServiceProvider.php

// ...

public function tools()
{
    return [
        // ...
        \Baleethai\NovaPermissions\NovaPermissions(),
    ];
}

// ...
use Laravel\Nova\Fields\MorphToMany;

public function fields(Request $request)
{
    return [
        // ...
        MorphToMany::make('Roles', 'roles', \Baleethai\NovaPermissions\Nova\Role::class),
        MorphToMany::make('Permissions', 'permissions', \Baleethai\NovaPermissions\Nova\Permission::class),
    ];
}



use App\Role;
use App\Permission;
use Illuminate\Database\Seeder;

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([
            'invoices',
            'clients',
            'contacts',
            'payments',
            'teams',
            'users',
            'roles',
            // ... your own models/permission you want to crate
        ]);

        $collection->each(function ($item, $key) {
            // create permissions for each collection item
            Permission::create(['group' => $item, 'name' => 'view ' . $item]);
            Permission::create(['group' => $item, 'name' => 'view own ' . $item]);
            Permission::create(['group' => $item, 'name' => 'manage ' . $item]);
            Permission::create(['group' => $item, 'name' => 'manage own ' . $item]);
            Permission::create(['group' => $item, 'name' => 'restore ' . $item]);
            Permission::create(['group' => $item, 'name' => 'forceDelete ' . $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\User::whereEmail('[email protected]')->first(); // enter your email here 
        $user->assignRole('super-admin');
    }
}


namespace App\Policies;

use Baleethai\NovaPermissions\Policies\Policy;

class ContactPolicy extends Policy
{
    /**
     * The Permission key the Policy corresponds to.
     *
     * @var string
     */
    public static $key = 'contacts';
}


namespace App;

class User {
    /**
     * Determines if the User is a Super admin
     * @return null
    */
    public function isSuperAdmin()
    {
        return $this->hasRole('super-admin');
    }
}


namespace App\Nova;

use Baleethai\NovaPermissions\Nova\ResourceForUser;

class Contact extends ResourceForUser 
{
    //...
}

// in app/Providers/NovaServiceProvider.php

// ...

use App\Nova\Role;
use App\Nova\Permission;

public function tools()
{
    return [
        // ...
        \Baleethai\NovaPermissions\NovaPermissionTool::make()
            ->roleResource(Role::class)
            ->permissionResource(Permission::class),
    ];
}
bash
php artisan vendor:publish --provider="Baleethai\NovaPermissions\ToolServiceProvider" --tag="migrations"
bash
php artisan migrate

php artisan vendor:publish --provider="Baleethai\NovaPermissions\ToolServiceProvider" --tag="seeds"