Skip to content

Commit

Permalink
Fix permissions to access view of users, roles & permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
Fernando Pintabona committed May 13, 2024
1 parent 26100df commit 2276d5c
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 73 deletions.
29 changes: 23 additions & 6 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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-
Expand Down
6 changes: 2 additions & 4 deletions routes/adminpanel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});

});
Expand Down
13 changes: 13 additions & 0 deletions src/AdminPanel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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 = [
Expand All @@ -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 = [
Expand All @@ -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 = [
Expand Down
18 changes: 12 additions & 6 deletions src/Policies/RolePolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.' );
}

Expand All @@ -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.' );
}

Expand All @@ -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.' );
}

Expand All @@ -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.' );
}

Expand All @@ -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.' );
}

Expand All @@ -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.' );
}

Expand Down
96 changes: 51 additions & 45 deletions src/Policies/UserPolicy.php
Original file line number Diff line number Diff line change
@@ -1,47 +1,49 @@
<?php

namespace FefoP\AdminPanel\Policies;

use App\Models\User;
use Illuminate\Auth\Access\Response;
use Illuminate\Auth\Access\HandlesAuthorization;

class UserPolicy
{
use HandlesAuthorization;

/**
* Determine whether the user can administer the model.
*
* @param User $user
*
* @return Response|bool
*/
public function administer( User $user )
public function administer(User $user)
{
if ( $user->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.
*
* @param 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.
*
Expand All @@ -50,31 +52,33 @@ 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.
*
* @param User $user
*
* @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.
*
Expand All @@ -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.
*
Expand All @@ -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.
*
Expand All @@ -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.
*
Expand All @@ -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.');
}
}
Loading

0 comments on commit 2276d5c

Please sign in to comment.