Skip to content

Commit

Permalink
Refatorando e adicionando a versão do spatie/laravel-permission:^5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffersongoncalves committed Apr 3, 2022
1 parent 6dfbc44 commit 8d17693
Show file tree
Hide file tree
Showing 13 changed files with 109 additions and 67 deletions.
41 changes: 24 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
# A Laravel Nova tool for Spatie's laravel-permission library

[![Latest Version on Packagist](https://img.shields.io/packagist/v/vyuldashev/nova-permission.svg?style=flat-square)](https://packagist.org/packages/vyuldashev/nova-permission)
[![Total Downloads](https://img.shields.io/packagist/dt/vyuldashev/nova-permission.svg?style=flat-square)](https://packagist.org/packages/vyuldashev/nova-permission)
[![Latest Version on Packagist](https://img.shields.io/packagist/v/jeffersonsimaogoncalves/nova-permission.svg?style=flat-square)](https://packagist.org/packages/jeffersonsimaogoncalves/nova-permission)
[![Total Downloads](https://img.shields.io/packagist/dt/jeffersonsimaogoncalves/nova-permission.svg?style=flat-square)](https://packagist.org/packages/jeffersonsimaogoncalves/nova-permission)

![screenshot 1](https://raw.githubusercontent.com/vyuldashev/nova-permission/master/docs/user-resource.png)
![screenshot 1](https://raw.githubusercontent.com/jeffersonsimaogoncalves/nova-permission/master/docs/user-resource.png)

## Installation

You can install the package in to a Laravel app that uses [Nova](https://nova.laravel.com) via composer:

```bash
composer require vyuldashev/nova-permission
composer require jeffersonsimaogoncalves/nova-permission
```

Go through the [Installation](https://github.com/spatie/laravel-permission#installation) section in order to setup [laravel-permission](https://packagist.org/packages/spatie/laravel-permission).
Expand All @@ -26,7 +26,7 @@ public function tools()
{
return [
// ...
\Vyuldashev\NovaPermission\NovaPermissionTool::make(),
\JeffersonSimaoGoncalves\NovaPermission\NovaPermissionTool::make(),
];
}
```
Expand All @@ -37,7 +37,7 @@ Next, add middleware to `config/nova.php`
// in config/nova.php
'middleware' => [
// ...
\Vyuldashev\NovaPermission\ForgetCachedPermissions::class,
\JeffersonSimaoGoncalves\NovaPermission\ForgetCachedPermissions::class,
],
```

Expand All @@ -51,8 +51,8 @@ public function fields(Request $request)
{
return [
// ...
MorphToMany::make('Roles', 'roles', \Vyuldashev\NovaPermission\Role::class),
MorphToMany::make('Permissions', 'permissions', \Vyuldashev\NovaPermission\Permission::class),
MorphToMany::make('Roles', 'roles', \JeffersonSimaoGoncalves\NovaPermission\Nova\Role::class),
MorphToMany::make('Permissions', 'permissions', \JeffersonSimaoGoncalves\NovaPermission\Nova\Permission::class),
];
}
```
Expand All @@ -61,8 +61,8 @@ Or if you want to attach multiple permissions at once, use `RoleBooleanGroup` an

```php
// ...
use Vyuldashev\NovaPermission\PermissionBooleanGroup;
use Vyuldashev\NovaPermission\RoleBooleanGroup;
use JeffersonSimaoGoncalves\NovaPermission\Nova\Fields\PermissionBooleanGroup;
use JeffersonSimaoGoncalves\NovaPermission\Nova\Fields\RoleBooleanGroup;

public function fields(Request $request)
{
Expand All @@ -78,8 +78,7 @@ If your `User` could have a single role at any given time, you can use `RoleSele

```php
// ...
use Vyuldashev\NovaPermission\PermissionBooleanGroup;
use Vyuldashev\NovaPermission\RoleSelect;
use JeffersonSimaoGoncalves\NovaPermission\Nova\Fields\RoleSelect;

public function fields(Request $request)
{
Expand All @@ -103,7 +102,7 @@ public function tools()
{
return [
// ...
\Vyuldashev\NovaPermission\NovaPermissionTool::make()
\JeffersonSimaoGoncalves\NovaPermission\NovaPermissionTool::make()
->roleResource(CustomRole::class)
->permissionResource(CustomPermission::class),
];
Expand All @@ -115,8 +114,8 @@ If you want to show your roles and policies with a custom label, you can set `$l

```php
// ...
use Vyuldashev\NovaPermission\PermissionBooleanGroup;
use Vyuldashev\NovaPermission\RoleSelect;
use JeffersonSimaoGoncalves\NovaPermission\Nova\Fields\PermissionBooleanGroup;
use JeffersonSimaoGoncalves\NovaPermission\Nova\Fields\RoleSelect;

public function fields(Request $request)
{
Expand All @@ -130,7 +129,7 @@ public function fields(Request $request)
```


## Define Policies
## Define Policies

```php
// in app/Providers/NovaServiceProvider.php
Expand All @@ -141,7 +140,7 @@ public function tools()
{
return [
// ...
\Vyuldashev\NovaPermission\NovaPermissionTool::make()
\JeffersonSimaoGoncalves\NovaPermission\NovaPermissionTool::make()
->rolePolicy(RolePolicy::class)
->permissionPolicy(PermissionPolicy::class),
];
Expand All @@ -152,3 +151,11 @@ public function tools()
## Usage

A new menu item called "Permissions & Roles" will appear in your Nova app after installing this package.

## Reporting Issues

If you have a problem with this plugin or any bug, please open an issue on [GitHub](https://github.com/jeffersonsimaogoncalves/cakephp-rest-api/issues).

## Credits

This work is based on the [code by Vyuldashev](https://github.com/vyuldashev/nova-permission).
11 changes: 4 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "vyuldashev/nova-permission",
"name": "jeffersonsimaogoncalves/nova-permission",
"description": "A Laravel Nova tool for Spatie's Permission library.",
"keywords": [
"laravel",
Expand All @@ -9,20 +9,17 @@
"license": "MIT",
"require": {
"php": "^7.4|^8.0",
"spatie/laravel-permission": "^3.0|^4.0"
"spatie/laravel-permission": "^3.0|^4.0|^5.0"
},
"autoload": {
"psr-4": {
"Vyuldashev\\NovaPermission\\": "src/"
"JeffersonSimaoGoncalves\\NovaPermission\\": "src/"
}
},
"extra": {
"branch-alias": {
"dev-master": "2.0-dev"
},
"laravel": {
"providers": [
"Vyuldashev\\NovaPermission\\ToolServiceProvider"
"JeffersonSimaoGoncalves\\NovaPermission\\ToolServiceProvider"
]
}
},
Expand Down
4 changes: 2 additions & 2 deletions src/ForgetCachedPermissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

declare(strict_types=1);

namespace Vyuldashev\NovaPermission;
namespace JeffersonSimaoGoncalves\NovaPermission;

use Illuminate\Support\Str;
use Laravel\Nova\Nova;
use Spatie\Permission\PermissionRegistrar;
use Illuminate\Support\Str;

class ForgetCachedPermissions
{
Expand Down
3 changes: 2 additions & 1 deletion src/AttachToRole.php → src/Nova/Actions/AttachToRole.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<?php

namespace Vyuldashev\NovaPermission;
namespace JeffersonSimaoGoncalves\NovaPermission\Nova\Actions;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Collection;
use JeffersonSimaoGoncalves\NovaPermission\Nova\Role;
use Laravel\Nova\Actions\Action;
use Laravel\Nova\Fields\ActionFields;
use Laravel\Nova\Fields\Select;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
<?php

namespace Vyuldashev\NovaPermission;
namespace JeffersonSimaoGoncalves\NovaPermission\Nova\Fields;

use Illuminate\Support\Collection;
use Laravel\Nova\Fields\BooleanGroup;
use Laravel\Nova\Http\Requests\NovaRequest;
use Spatie\Permission\Models\Permission as PermissionModel;
use Spatie\Permission\PermissionRegistrar;
use Spatie\Permission\Traits\HasPermissions;
use function app;
use function collect;

class PermissionBooleanGroup extends BooleanGroup
{
Expand Down Expand Up @@ -38,7 +40,7 @@ public function __construct($name, $attribute = null, callable $resolveCallback
*/
protected function fillAttributeFromRequest(NovaRequest $request, $requestAttribute, $model, $attribute)
{
if (! $request->exists($requestAttribute)) {
if (!$request->exists($requestAttribute)) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
<?php

namespace Vyuldashev\NovaPermission;
namespace JeffersonSimaoGoncalves\NovaPermission\Nova\Fields;

use Illuminate\Support\Collection;
use Laravel\Nova\Fields\BooleanGroup;
use Laravel\Nova\Http\Requests\NovaRequest;
use Spatie\Permission\Models\Role as RoleModel;
use Spatie\Permission\PermissionRegistrar;
use Spatie\Permission\Traits\HasPermissions;
use function app;
use function collect;

class RoleBooleanGroup extends BooleanGroup
{
Expand Down Expand Up @@ -38,7 +40,7 @@ public function __construct($name, $attribute = null, callable $resolveCallback
*/
protected function fillAttributeFromRequest(NovaRequest $request, $requestAttribute, $model, $attribute)
{
if (! $request->exists($requestAttribute)) {
if (!$request->exists($requestAttribute)) {
return;
}

Expand Down
37 changes: 20 additions & 17 deletions src/RoleSelect.php → src/Nova/Fields/RoleSelect.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
<?php

namespace Vyuldashev\NovaPermission;
namespace JeffersonSimaoGoncalves\NovaPermission\Nova\Fields;

use Illuminate\Support\Collection;
use Laravel\Nova\Fields\Select;
use Laravel\Nova\Http\Requests\NovaRequest;
use Spatie\Permission\PermissionRegistrar;
use Spatie\Permission\Traits\HasPermissions;
use function app;
use function collect;
use function optional;

class RoleSelect extends Select
{
Expand All @@ -27,6 +30,20 @@ public function __construct($name, $attribute = null, callable $resolveCallback
$this->options($options);
}

/**
* Display values using their corresponding specified labels.
*
* @return $this
*/
public function displayUsingLabels(): RoleSelect
{
return $this->displayUsing(function ($value) {
return collect($this->meta['options'])
->where('value', optional($value->first())->name)
->first()['label'] ?? optional($value->first())->name;
});
}

/**
* @param NovaRequest $request
* @param string $requestAttribute
Expand All @@ -35,30 +52,16 @@ public function __construct($name, $attribute = null, callable $resolveCallback
*/
protected function fillAttributeFromRequest(NovaRequest $request, $requestAttribute, $model, $attribute)
{
if (! $request->exists($requestAttribute)) {
if (!$request->exists($requestAttribute)) {
return;
}

$model->roles()->detach();

if (! is_null($request[$requestAttribute])) {
if (!is_null($request[$requestAttribute])) {
$roleClass = app(PermissionRegistrar::class)->getRoleClass();
$role = $roleClass::where('name', $request[$requestAttribute])->first();
$model->assignRole($role);
}
}

/**
* Display values using their corresponding specified labels.
*
* @return $this
*/
public function displayUsingLabels(): RoleSelect
{
return $this->displayUsing(function ($value) {
return collect($this->meta['options'])
->where('value', optional($value->first())->name)
->first()['label'] ?? optional($value->first())->name;
});
}
}
28 changes: 21 additions & 7 deletions src/Permission.php → src/Nova/Permission.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
<?php

namespace Vyuldashev\NovaPermission;
namespace JeffersonSimaoGoncalves\NovaPermission\Nova;

use App\Nova\Resource;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;
use Illuminate\Validation\Rule;
use JeffersonSimaoGoncalves\NovaPermission\Nova\Actions\AttachToRole;
use JeffersonSimaoGoncalves\NovaPermission\Nova\Fields\RoleBooleanGroup;
use Laravel\Nova\Fields\DateTime;
use Laravel\Nova\Fields\ID;
use Laravel\Nova\Fields\MorphToMany;
use Laravel\Nova\Fields\Select;
use Laravel\Nova\Fields\Text;
use Laravel\Nova\Nova;
use Laravel\Nova\Resource;
use Spatie\Permission\PermissionRegistrar;
use function __;
use function app;
use function collect;
use function config;
use function getModelForGuard;

class Permission extends Resource
{
Expand Down Expand Up @@ -93,19 +100,26 @@ public function fields(Request $request): array
ID::make()->sortable(),

Text::make(__('nova-permission-tool::permissions.name'), 'name')
->rules(['required', 'string', 'max:255'])
->creationRules('unique:'.config('permission.table_names.permissions'))
->updateRules('unique:'.config('permission.table_names.permissions').',name,{{resourceId}}'),
->rules([
'required',
'string',
'max:255',
])
->creationRules('unique:' . config('permission.table_names.permissions'))
->updateRules('unique:' . config('permission.table_names.permissions') . ',name,{{resourceId}}'),

Text::make(__('nova-permission-tool::permissions.display_name'), function () {
return __('nova-permission-tool::permissions.display_names.'.$this->name);
return __('nova-permission-tool::permissions.display_names.' . $this->name);
})->canSee(function () {
return is_array(__('nova-permission-tool::permissions.display_names'));
}),

Select::make(__('nova-permission-tool::permissions.guard_name'), 'guard_name')
->options($guardOptions->toArray())
->rules(['required', Rule::in($guardOptions)]),
->rules([
'required',
Rule::in($guardOptions),
]),

DateTime::make(__('nova-permission-tool::permissions.created_at'), 'created_at')->exceptOnForms(),
DateTime::make(__('nova-permission-tool::permissions.updated_at'), 'updated_at')->exceptOnForms(),
Expand Down
Loading

0 comments on commit 8d17693

Please sign in to comment.