Skip to content

Commit

Permalink
Merge pull request vyuldashev#118 from Reflow1319/master
Browse files Browse the repository at this point in the history
Set Custom Policies for Roles and Permissions
  • Loading branch information
vyuldashev authored Mar 29, 2020
2 parents 27396fd + 4db8a14 commit e005b52
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,27 @@ public function tools()
->permissionResource(CustomPermission::class),
];
}

```


## Define Policies

```php
// in app/Providers/NovaServiceProvider.php

// ...

public function tools()
{
return [
// ...
\Vyuldashev\NovaPermission\NovaPermissionTool::make()
->rolePolicy(RolePolicy::class)
->permissionPolicy(PermissionPolicy::class),
];
}

```

## Usage
Expand Down
22 changes: 22 additions & 0 deletions src/NovaPermissionTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@

namespace Vyuldashev\NovaPermission;

use Gate;
use Laravel\Nova\Nova;
use Laravel\Nova\Tool;

class NovaPermissionTool extends Tool
{
public $roleResource = Role::class;
public $permissionResource = Permission::class;

public $rolePolicy = RolePolicy::class;
public $permissionPolicy = PermissionPolicy::class;

/**
* Perform any tasks that need to happen when the tool is booted.
Expand All @@ -21,6 +25,9 @@ public function boot()
$this->roleResource,
$this->permissionResource,
]);

Gate::policy(config('permission.models.permission'), $this->permissionPolicy);
Gate::policy(config('permission.models.role'), $this->rolePolicy);
}

public function roleResource(string $roleResource)
Expand All @@ -36,4 +43,19 @@ public function permissionResource(string $permissionResource)

return $this;
}

public function rolePolicy(string $rolePolicy)
{
$this->rolePolicy = $rolePolicy;

return $this;
}

public function permissionPolicy(string $permissionPolicy)
{

$this->permissionPolicy = $permissionPolicy;

return $this;
}
}
4 changes: 0 additions & 4 deletions src/ToolServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Vyuldashev\NovaPermission;

use Gate;
use Illuminate\Support\ServiceProvider;
use Laravel\Nova\Events\ServingNova;
use Laravel\Nova\Nova;
Expand All @@ -26,9 +25,6 @@ public function boot()
$this->routes();
});

Gate::policy(config('permission.models.permission'), PermissionPolicy::class);
Gate::policy(config('permission.models.role'), RolePolicy::class);

Nova::serving(function (ServingNova $event) {
//
});
Expand Down

0 comments on commit e005b52

Please sign in to comment.