1. Go to this page and download the library: Download pktharindu/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/ */
pktharindu / nova-permissions example snippets
// in config/nova-permissions.php
return [
/*
|--------------------------------------------------------------------------
| User model class
|--------------------------------------------------------------------------
*/
'user_model' => 'App\User',
/*
|--------------------------------------------------------------------------
| Nova User resource tool class
|--------------------------------------------------------------------------
*/
'user_resource' => 'App\Nova\User',
/*
|--------------------------------------------------------------------------
| The group associated with the resource
|--------------------------------------------------------------------------
*/
'role_resource_group' => 'Other',
/*
|--------------------------------------------------------------------------
| Database table names
|--------------------------------------------------------------------------
| When using the "HasRoles" trait from this package, we need to know which
| table should be used to retrieve your roles. We have chosen a basic
| default value but you may easily change it to any table you like.
*/
'table_names' => [
'roles' => 'roles',
'role_permission' => 'role_permission',
'role_user' => 'role_user',
'users' => 'users',
],
/*
|--------------------------------------------------------------------------
| Application Permissions
|--------------------------------------------------------------------------
*/
'permissions' => [
'view users' => [
'display_name' => 'View users',
'description' => 'Can view users',
'group' => 'User',
],
'create users' => [
'display_name' => 'Create users',
'description' => 'Can create users',
'group' => 'User',
],
// ...
],
];
// in app/Providers/NovaServiceProvider.php
public function tools()
{
return [
// ...
new \Pktharindu\NovaPermissions\NovaPermissions(),
];
}
// in app/Providers/AuthServiceProvider.php
use Illuminate\Support\Facades\Gate;
use Pktharindu\NovaPermissions\Traits\ValidatesPermissions;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
class AuthServiceProvider extends ServiceProvider
{
use ValidatesPermissions;
protected $policies = [
\Pktharindu\NovaPermissions\Role::class => \App\Policies\RolePolicy::class,
];
public function boot()
{
$this->registerPolicies();
foreach (config('nova-permissions.permissions') as $key => $permissions) {
Gate::define($key, function (User $user) use ($key) {
if ($this->nobodyHasAccess($key)) {
return true;
}
return $user->hasPermissionTo($key);
});
}
}
}
// in app/User.php
use Illuminate\Notifications\Notifiable;
use Pktharindu\NovaPermissions\Traits\HasRoles;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use HasRoles,
Notifiable;
// ...
}
use Laravel\Nova\Fields\BelongsToMany;
public function fields(Request $request)
{
return [
// ...
BelongsToMany::make('Roles', 'roles', \Pktharindu\NovaPermissions\Nova\Role::class),
];
}
namespace App\Policies;
use App\Post;
use App\User;
use Illuminate\Auth\Access\HandlesAuthorization;
class PostPolicy
{
use HandlesAuthorization;
public function view(User $user, Post $post)
{
if ($user->hasPermissionTo('view own posts')) {
return $user->id === $post->user_id;
}
return $user->hasPermissionTo('view posts');
}
public function create(User $user)
{
return $user->hasAnyPermission(['manage posts', 'manage own posts']);
}
public function update(User $user, Post $post)
{
if ($user->hasPermissionTo('manage own posts')) {
return $user->id == $post->user_id;
}
return $user->hasPermissionTo('manage posts');
}
public function delete(User $user, Post $post)
{
if ($user->hasPermissionTo('manage own posts')) {
return $user->id === $post->user_id;
}
return $user->hasPermissionTo('manage posts');
}
}
// in app/Providers/NovaServiceProvider.php
// ...
use App\Nova\Role;
public function tools()
{
return [
// ...
\Pktharindu\NovaPermissions\NovaPermissions::make()
->roleResource(Role::class),
];
}
// in app/Nova/Role.php
use Pktharindu\NovaPermissions\Nova\Role as RoleResource;
class Role extends RoleResource
{
// ...
}