diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 41e6099..35e4cb2 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -4,9 +4,17 @@ - @@ -580,7 +596,8 @@ - diff --git a/README.md b/README.md index c319ac9..f7c5e32 100644 --- a/README.md +++ b/README.md @@ -25,19 +25,18 @@ These are all required dependencies that will be installed if needed. To get started, add a local repository to Composer: -```js +```json { - [...] "repositories": { "admin-panel": { "type": "path", "url": "/path-to-downloaded-file/fefo-p/admin-panel", "options": { - "symlink": true + "symlink": true + } } - }, - [...] - } + } +} ``` ### -production- diff --git a/routes/adminpanel.php b/routes/adminpanel.php index f51a7d6..185287e 100644 --- a/routes/adminpanel.php +++ b/routes/adminpanel.php @@ -24,10 +24,8 @@ Route::get('/', 'index')->name('adminpanel.dashboard'); Route::get('/about', 'about')->name('adminpanel.about'); Route::get('/users', 'users')->name('adminpanel.users'); - Route::get('/roles', 'roles')->name('adminpanel.roles') - ->can('administer', Role::class); - Route::get('/permissions', 'permissions')->name('adminpanel.permissions') - ->can('administer', Permission::class); + Route::get('/roles', 'roles')->name('adminpanel.roles'); + Route::get('/permissions', 'permissions')->name('adminpanel.permissions'); }); }); diff --git a/src/AdminPanel.php b/src/AdminPanel.php index 22a60ed..45d63ca 100644 --- a/src/AdminPanel.php +++ b/src/AdminPanel.php @@ -9,6 +9,7 @@ use Illuminate\Support\Facades\Auth; use FefoP\AdminPanel\Models\Permission; use Illuminate\Support\Facades\Artisan; + use Illuminate\Auth\Access\AuthorizationException; class AdminPanel extends Controller { @@ -45,6 +46,8 @@ public function about() public function users(Request $request) { + $this->authorize('viewAny', App\Models\User::class); + $title = 'Listado de Usuarios'; $description = 'Listado de usuarios definidos en el sistema'; $action = [ @@ -68,6 +71,11 @@ public function users(Request $request) public function roles() { + $this->authorize('viewAny', FefoP\AdminPanel\Models\Role::class); + /*if (Auth::user()->cannot('adminpanel.rol.ver')) { + throw new AuthorizationException('No tienes permisos para acceder a este panel.'); + }*/ + $title = 'Listado de Roles'; $description = 'Roles definidos en el sistema'; $action = [ @@ -86,6 +94,11 @@ public function roles() public function permissions() { + $this->authorize('viewAny', FefoP\AdminPanel\Models\Permission::class); + /*if (Auth::user()->cannot('adminpanel.permiso.ver')) { + throw new AuthorizationException('No tienes permisos para acceder a este panel.'); + }*/ + $title = 'Listado de Permisos'; $description = 'Permisos definidos en el sistema'; $action = [ diff --git a/src/Policies/RolePolicy.php b/src/Policies/RolePolicy.php index 7abbd72..beffce8 100644 --- a/src/Policies/RolePolicy.php +++ b/src/Policies/RolePolicy.php @@ -20,7 +20,8 @@ class RolePolicy */ public function administer( User $user ) { - if ( $user->getAllPermissions()->pluck( 'name' )->contains( 'adminpanel.rol.editar' ) ) { + //if ( $user->getAllPermissions()->pluck( 'name' )->contains( 'adminpanel.rol.editar' ) ) { + if ($user->can('adminpanel.rol.editar')) { return Response::allow( 'You can administer roles.' ); } @@ -36,7 +37,8 @@ public function administer( User $user ) */ public function viewAny( User $user ) { - if ( $user->getAllPermissions()->pluck( 'name' )->contains( 'adminpanel.rol.ver' ) ) { + //if ( $user->getAllPermissions()->pluck( 'name' )->contains( 'adminpanel.rol.ver' ) ) { + if ($user->can('adminpanel.rol.ver')) { return Response::allow( 'You can see the role list.' ); } @@ -53,7 +55,8 @@ public function viewAny( User $user ) */ public function view( User $user, Role $role ) { - if ( $user->getAllPermissions()->pluck( 'name' )->contains( 'adminpanel.rol.ver' ) ) { + //if ( $user->getAllPermissions()->pluck( 'name' )->contains( 'adminpanel.rol.ver' ) ) { + if ($user->can('adminpanel.rol.ver')) { return Response::allow( 'You can see this role.' ); } @@ -69,7 +72,8 @@ public function view( User $user, Role $role ) */ public function create( User $user ) { - if ( $user->getAllPermissions()->pluck( 'name' )->contains( 'adminpanel.rol.crear' ) ) { + //if ( $user->getAllPermissions()->pluck( 'name' )->contains( 'adminpanel.rol.crear' ) ) { + if ($user->can('adminpanel.rol.crear')) { return Response::allow( 'You can create a role.' ); } @@ -86,7 +90,8 @@ public function create( User $user ) */ public function update( User $user, Role $role ) { - if ( $user->getAllPermissions()->pluck( 'name' )->contains( 'adminpanel.rol.editar' ) ) { + //if ( $user->getAllPermissions()->pluck( 'name' )->contains( 'adminpanel.rol.editar' ) ) { + if ($user->can('adminpanel.rol.editar')) { return Response::allow( 'You can edit this role.' ); } @@ -103,7 +108,8 @@ public function update( User $user, Role $role ) */ public function delete( User $user, Role $role ) { - if ( $user->getAllPermissions()->pluck( 'name' )->contains( 'adminpanel.rol.borrar' ) ) { + //if ( $user->getAllPermissions()->pluck( 'name' )->contains( 'adminpanel.rol.borrar' ) ) { + if ($user->can('adminpanel.rol.borrar')) { return Response::allow( 'You can delete this role.' ); } diff --git a/src/Policies/UserPolicy.php b/src/Policies/UserPolicy.php index 8102ecf..8b19be9 100644 --- a/src/Policies/UserPolicy.php +++ b/src/Policies/UserPolicy.php @@ -1,15 +1,15 @@ getAllPermissions()->pluck( 'name' )->contains( 'adminpanel.usuario.editar' ) ) { - return Response::allow( 'You can administer users.' ); + //if ( $user->getAllPermissions()->pluck('name')->contains('adminpanel.usuario.editar') ) { + if ($user->can('adminpanel.usuario.editar')) { + return Response::allow('You can administer users.'); } - - Response::deny( 'You cannot administer users.' ); + + Response::deny('You cannot administer users.'); } - + /** * Determine whether the user can view any models. * @@ -33,15 +34,16 @@ public function administer( User $user ) * * @return Response|bool */ - public function viewAny( User $user ) + public function viewAny(User $user) { - if ( $user->getAllPermissions()->pluck( 'name' )->contains( 'adminpanel.usuario.ver' ) ) { - return Response::allow( 'You can see the user list.' ); + //if ( $user->getAllPermissions()->pluck('name')->contains('adminpanel.usuario.ver') ) { + if ($user->can('adminpanel.usuario.ver')) { + return Response::allow('You can see the user list.'); } - - Response::deny( 'You cannot see the user list.' ); + + Response::deny('You cannot see the user list.'); } - + /** * Determine whether the user can view the model. * @@ -50,15 +52,16 @@ public function viewAny( User $user ) * * @return Response|bool */ - public function view( User $user, User $model ) + public function view(User $user, User $model) { - if ( $user->getAllPermissions()->pluck( 'name' )->contains( 'adminpanel.usuario.ver' ) ) { - return Response::allow( 'You can see this user.' ); + //if ( $user->getAllPermissions()->pluck('name')->contains('adminpanel.usuario.ver') ) { + if ($user->can('adminpanel.usuario.ver')) { + return Response::allow('You can see this user.'); } - - Response::deny( 'You cannot see this user.' ); + + Response::deny('You cannot see this user.'); } - + /** * Determine whether the user can create models. * @@ -66,15 +69,16 @@ public function view( User $user, User $model ) * * @return Response|bool */ - public function create( User $user ) + public function create(User $user) { - if ( $user->getAllPermissions()->pluck( 'name' )->contains( 'adminpanel.usuario.crear' ) ) { - return Response::allow( 'You can create a user.' ); + //if ( $user->getAllPermissions()->pluck( 'name' )->contains( 'adminpanel.usuario.crear' ) ) { + if ( $user->can('adminpanel.usuario.crear') ) { + return Response::allow('You can create a user.'); } - - Response::deny( 'You cannot create a user.' ); + + Response::deny('You cannot create a user.'); } - + /** * Determine whether the user can update the model. * @@ -83,15 +87,16 @@ public function create( User $user ) * * @return Response|bool */ - public function update( User $user, User $model ) + public function update(User $user, User $model) { - if ( $user->getAllPermissions()->pluck( 'name' )->contains( 'adminpanel.usuario.editar' ) ) { - return Response::allow( 'You can edit this user.' ); + //if ( $user->getAllPermissions()->pluck( 'name' )->contains( 'adminpanel.usuario.editar' ) ) { + if ( $user->can('adminpanel.usuario.editar') ) { + return Response::allow('You can edit this user.'); } - - Response::deny( 'You cannot edit this user.' ); + + Response::deny('You cannot edit this user.'); } - + /** * Determine whether the user can delete the model. * @@ -100,15 +105,16 @@ public function update( User $user, User $model ) * * @return Response|bool */ - public function delete( User $user, User $model ) + public function delete(User $user, User $model) { - if ( $user->getAllPermissions()->pluck( 'name' )->contains( 'adminpanel.usuario.borrar' ) ) { - return Response::allow( 'You can delete this user.' ); + //if ( $user->getAllPermissions()->pluck('name')->contains('adminpanel.usuario.borrar') ) { + if ($user->can('adminpanel.usuario.borrar')) { + return Response::allow('You can delete this user.'); } - - Response::deny( 'You cannot delete this user.' ); + + Response::deny('You cannot delete this user.'); } - + /** * Determine whether the user can restore the model. * @@ -117,11 +123,11 @@ public function delete( User $user, User $model ) * * @return Response|bool */ - public function restore( User $user, User $model ) + public function restore(User $user, User $model) { - Response::deny( 'You cannot restore this user.' ); + Response::deny('You cannot restore this user.'); } - + /** * Determine whether the user can permanently delete the model. * @@ -130,8 +136,8 @@ public function restore( User $user, User $model ) * * @return Response|bool */ - public function forceDelete( User $user, User $model ) + public function forceDelete(User $user, User $model) { - Response::deny( 'You cannot force delete this user.' ); + Response::deny('You cannot force delete this user.'); } } \ No newline at end of file diff --git a/src/Roles/Livewire/RoleCreate.php b/src/Roles/Livewire/RoleCreate.php index 9a0e56e..d78cd29 100644 --- a/src/Roles/Livewire/RoleCreate.php +++ b/src/Roles/Livewire/RoleCreate.php @@ -7,15 +7,20 @@ use LivewireUI\Modal\ModalComponent; use Illuminate\Support\Facades\Auth; use FefoP\AdminPanel\Models\Activity; + use Illuminate\Foundation\Auth\Access\AuthorizesRequests; + class RoleCreate extends ModalComponent { + use AuthorizesRequests; + public $name; public string $guard_name; public function mount() { - Auth::user()->can('adminpanel.rol.crear'); + $this->authorize('create', FefoP\AdminPanel\Models\Role::class); + //Auth::user()->can('adminpanel.rol.crear'); $this->guard_name = config('adminpanel.guard'); $this->separator = config( 'adminpanel.log.separator' ); diff --git a/src/Roles/Livewire/RoleDelete.php b/src/Roles/Livewire/RoleDelete.php index f99095b..68a650c 100644 --- a/src/Roles/Livewire/RoleDelete.php +++ b/src/Roles/Livewire/RoleDelete.php @@ -16,9 +16,10 @@ class RoleDelete extends ModalComponent public function mount(int $role_id, string $action) { - Auth::user()->can('adminpanel.rol.borrar'); + $this->role = Role::find($role_id); + $this->authorize('delete', $this->role); + //Auth::user()->can('adminpanel.rol.borrar'); - $this->role = Role::find($role_id); $this->separator = config('adminpanel.log.separator'); } diff --git a/src/Users/Livewire/UserDelete.php b/src/Users/Livewire/UserDelete.php index fd8be80..dde0026 100644 --- a/src/Users/Livewire/UserDelete.php +++ b/src/Users/Livewire/UserDelete.php @@ -17,9 +17,10 @@ class UserDelete extends ModalComponent public function mount(int $user_id, string $action) { - Auth::user()->can('adminpanel.usuario.borrar'); - $this->user = User::withTrashed()->find($user_id); + $this->authorize('delete', $this->user); + //Auth::user()->can('adminpanel.usuario.borrar'); + $this->action = $action; $this->separator = config('adminpanel.log.separator'); } diff --git a/src/Users/Livewire/UserEdit.php b/src/Users/Livewire/UserEdit.php index 3b5782d..824d13f 100644 --- a/src/Users/Livewire/UserEdit.php +++ b/src/Users/Livewire/UserEdit.php @@ -28,11 +28,11 @@ class UserEdit extends ModalComponent public function mount(int $user_id): void { - $this->authorize('administer', App\Models\User::class); $this->user = User::withTrashed() ->with([ "roles", "permissions" ]) ->find($user_id); + $this->authorize('update', $this->user); $this->name = $this->user->name; $this->email = $this->user->email; $this->cuil = $this->user->cuil;