Skip to content

Commit

Permalink
revisando roles 1
Browse files Browse the repository at this point in the history
  • Loading branch information
SgcWeb2 committed Nov 15, 2021
1 parent ccf2110 commit 21a49ae
Show file tree
Hide file tree
Showing 9 changed files with 195 additions and 45 deletions.
23 changes: 18 additions & 5 deletions app/Http/Controllers/CtrInicio.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,28 @@ class CtrInicio extends Controller
{
public function __invoke()
{
if (Auth::user()->administrador) {
$usuario = Auth::user();

if (Condominio::first()) {
$comunicados = Comunicado::orderBy('created_at', 'desc')->take(5)->get();
if ($usuario->administrador) {

return view('welcome', compact('comunicados'));
if ($usuario->hasRole('Portero')) {
// Si el usuario es un portero

return redirect()->route('visita.index');
} else if ($usuario->hasRole('Propietario')) {
// Si el usaurio es un propietario

if (Condominio::first()) {
// Si los datos de condominio están registrados
return redirect()->route('admin.home');
} else {

return redirect()->route('admin.condominio');
}
} else {
$comunicados = Comunicado::orderBy('created_at', 'desc')->take(5)->get();

return redirect()->route('admin.condominio');
return view('welcome', compact('comunicados'));
}
} else {
$comunicados = Comunicado::orderBy('created_at', 'desc')->take(5)->get();
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Livewire/TablaUsuario.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class TablaUsuario extends Component
public $openDestroy = false;

protected $rules = [
'roles' => 'required|min:1',
'roles' => 'array',
];

protected $listeners = ['render'];
Expand Down Expand Up @@ -78,7 +78,7 @@ public function orden($orden)
public function edit(User $usuario)
{
$this->usuario = $usuario;
$this->roles = $this->usuario->roles()->allRelatedIds();
$this->roles = $this->usuario->roles()->allRelatedIds()->map(fn ($id) => (string)$id);

$this->openEdit = true;
}
Expand Down
5 changes: 5 additions & 0 deletions app/Providers/RouteServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ class RouteServiceProvider extends ServiceProvider
*/
public function boot()
{
Route::resourceVerbs([
'create' => 'crear',
'edit' => 'modificar',
]);

$this->configureRateLimiting();

$this->routes(function () {
Expand Down
19 changes: 13 additions & 6 deletions database/seeders/RoleSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public function run()
$role4 = Role::create(['name' => 'Condominio']);


Permission::create(['name' => 'home'])->assignRole($role2);

Permission::create(['name' => 'asamblea.index'])->assignRole($role4);
Permission::create(['name' => 'asamblea.create'])->assignRole($role4);
Permission::create(['name' => 'asamblea.show'])->assignRole($role4);
Expand Down Expand Up @@ -56,9 +58,14 @@ public function run()
Permission::create(['name' => 'gasto.create'])->assignRole($role4);
Permission::create(['name' => 'gasto.show'])->assignRole($role4);

Permission::create(['name' => 'pago.index'])->assignRole($role4);
Permission::create(['name' => 'pago.create'])->assignRole($role4);
Permission::create(['name' => 'pago.show'])->assignRole($role4);
Permission::create(['name' => 'pago-condominio.index'])->assignRole($role4);
Permission::create(['name' => 'pago-condominio.create'])->assignRole($role4);
Permission::create(['name' => 'pago-condominio.show'])->assignRole($role4);

Permission::create(['name' => 'pago-propietario.index'])->assignRole($role2);
Permission::create(['name' => 'pago-propietario.create'])->assignRole($role2);
Permission::create(['name' => 'pago-propietario.show'])->assignRole($role2);
Permission::create(['name' => 'pago-propietario.confirmar'])->assignRole($role4);

Permission::create(['name' => 'proveedor.index'])->assignRole($role4);
Permission::create(['name' => 'proveedor.create'])->assignRole($role4);
Expand All @@ -82,9 +89,11 @@ public function run()
Permission::create(['name' => 'visita.create'])->assignRole($role3);
Permission::create(['name' => 'visita.show'])->assignRole($role3);

Permission::create(['name' => 'visita.lista'])->assignRole($role4);


// Permisos exclusivos de administrador
Permission::create(['name' => 'admin']);
Permission::create(['name' => 'admin.home'])->assignRole($role4);

Permission::create(['name' => 'admin.administrador.index']);
Permission::create(['name' => 'admin.administrador.create']);
Expand All @@ -103,8 +112,6 @@ public function run()
Permission::create(['name' => 'admin.usuario.edit']);
Permission::create(['name' => 'admin.usuario.delete']);
Permission::create(['name' => 'admin.usuario.show']);

Permission::create(['name' => 'admin.visita.lista'])->assignRole($role4);


$role1->syncPermissions(Permission::all());
Expand Down
73 changes: 73 additions & 0 deletions database/seeders/UserSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public function run()
$integrante->unidad()->associate($unidad)->save();


// Usuario con rol de Administrador
$integrante = Integrante::factory([
'letra' => 'V',
'documento' => '00000000',
Expand Down Expand Up @@ -108,5 +109,77 @@ public function run()
$unidad = Unidad::factory()->create();
$unidad->propietario()->associate($propietario)->save();
$integrante->unidad()->associate($unidad)->save();


// Usuario con rol de Propietario
$integrante = Integrante::factory([
'letra' => 'V',
'documento' => '00000001',
'nombre' => 'Pro',
'apellido' => 'Pietario',
'fecha_nacimiento' => '01-01-2000',
'email' => 'propietario@gmail.com',
])->create();

$usuario = User::factory([
'name' => 'ProPietario',
'email' => 'propietario@gmail.com',
'password' => bcrypt('1234'),
])->create()->assignRole('Propietario');

$propietario = Propietario::factory()->create([
'integrante_id' => $integrante->id,
'user_id' => $usuario->id,
]);

$unidad = Unidad::factory()->create();
$unidad->propietario()->associate($propietario)->save();
$integrante->unidad()->associate($unidad)->save();


// Usuario con rol de Condominio
$integrante = Integrante::factory([
'letra' => 'V',
'documento' => '00000002',
'nombre' => 'Condo',
'apellido' => 'Minio',
'fecha_nacimiento' => '01-01-2000',
'email' => 'condominio@gmail.com',
])->create();

$usuario = User::factory([
'name' => 'CondoMinio',
'email' => 'condominio@gmail.com',
'password' => bcrypt('1234'),
])->create()->assignRole('Condominio');

Administrador::create([
'integrante_id' => $integrante->id,
'rol' => 'Administrador',
'user_id' => $usuario->id,
]);


// Usuario con rol de Portero
$integrante = Integrante::factory([
'letra' => 'V',
'documento' => '00000003',
'nombre' => 'Por',
'apellido' => 'Tero',
'fecha_nacimiento' => '01-01-2000',
'email' => 'portero@gmail.com',
])->create();

$usuario = User::factory([
'name' => 'PorTero',
'email' => 'portero@gmail.com',
'password' => bcrypt('1234'),
])->create()->assignRole('Portero');

Administrador::create([
'integrante_id' => $integrante->id,
'rol' => 'Administrador',
'user_id' => $usuario->id,
]);
}
}
7 changes: 5 additions & 2 deletions resources/views/livewire/tabla-usuario.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,15 @@ class="btn btn-blue">
class="mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm sm:text-sm border-gray-300 rounded-md">
</div>

{{-- {{var_dump($listaRoles)}} --}}
{{ var_dump($roles) }}
@foreach ($listaRoles as $key => $item)
<div class="col-span-6">
<div class="flex items-start">
<div class="flex items-center h-5">
<input wire:model="roles" id="role_{{ $key }}"
name="role_{{ $key }}" type="checkbox"
<input wire:model="roles" type="checkbox"
id="role_{{ $key }}"
name="role_{{ $key }}"
value="{{ $item->id }}"
class="focus:ring-indigo-500 h-4 w-4 text-indigo-600 border-gray-300 rounded">
</div>
Expand Down
102 changes: 76 additions & 26 deletions resources/views/navigation-menu.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,36 @@
'can' => 'visita.index',
],
];
$linksDropdown = [
[
'name' => 'Pagos del condominio',
'route' => route('pago.create'),
'active' => request()->routeIs('pago.create'),
'can' => 'pago-condominio.create',
],
[
'name' => 'Notificar pago',
'route' => route('pago-propietario.create'),
'active' => request()->routeIs('pago-propietario.create'),
'can' => 'pago-propietario.create',
],
[
'name' => 'Confirmar pagos',
'route' => route('pago.confirmar'),
'active' => request()->routeIs('pago.confirmar'),
'can' => 'pago-propietario.confirmar',
],
];
$dropCount = 0;
foreach ($linksDropdown as $item) {
if (Auth::user()->can($item['can'])) {
$dropCount++;
}
}
@endphp

<nav x-data="{ open: false }" class="bg-white border-b border-gray-100">
Expand Down Expand Up @@ -66,32 +96,44 @@
<x-jet-nav-link href="{{ route('home') }}" :active="request()->routeIs('home')">
Inicio
</x-jet-nav-link>
<x-jet-dropdown>
<x-slot name="trigger">
<x-jet-nav-link class="h-full cursor-pointer">
Pagos
</x-jet-nav-link>
</x-slot>
<x-slot name="content">
@can('pago.create')
<x-jet-dropdown-link href="{{ route('pago.create') }}"
:active="request()->routeIs('pago.create')">
Pagos del condominio
</x-jet-dropdown-link>
@endcan
<x-jet-dropdown-link href="{{ route('pago-propietario.create') }}"
:active="request()->routeIs('pago-propietario.create')">
Notificar pago
</x-jet-dropdown-link>
</x-slot>
</x-jet-dropdown>

@if ($dropCount)
<x-jet-dropdown>

<x-slot name="trigger">
<x-jet-nav-link class="h-full cursor-pointer">
Pagos
</x-jet-nav-link>
</x-slot>

<x-slot name="content">

@foreach ($linksDropdown as $item)

@can($item['can'])
<x-jet-dropdown-link href="{{ $item['route'] }}" :active="$item['active']">
{{ $item['name'] }}
</x-jet-dropdown-link>
@endcan

@endforeach
</x-slot>

</x-jet-dropdown>
@endif

@foreach ($nav_links as $item)

@can($item['can'])

<x-jet-nav-link href="{{ $item['route'] }}" :active="$item['active']">
{{ $item['name'] }}
</x-jet-nav-link>

@endcan

@endforeach

</div>
@endauth
</div>
Expand Down Expand Up @@ -183,9 +225,14 @@ class="inline-flex items-center px-3 py-2 border border-transparent text-sm lead

<x-slot name="content">

@can('admin')
@can('home')
<x-jet-dropdown-link href="{{ route('home') }}">
{{ __('Panel de propietario') }}
</x-jet-dropdown-link>
@endcan

@can('admin.home')
<x-jet-dropdown-link href="{{ route('admin.home') }}">
{{-- <i class="fa fas-settings"></i> --}}
{{ __('Administración del condominio') }}
</x-jet-dropdown-link>
@endcan
Expand All @@ -211,8 +258,9 @@ class="inline-flex items-center px-3 py-2 border border-transparent text-sm lead
<form method="POST" action="{{ route('logout') }}">
@csrf

<x-jet-dropdown-link href="{{ route('logout') }}" onclick="event.preventDefault();
this.closest('form').submit();">
<x-jet-dropdown-link href="{{ route('logout') }}"
onclick="event.preventDefault();
this.closest('form').submit();">
{{ __('Cerrar sesión') }}
</x-jet-dropdown-link>
</form>
Expand Down Expand Up @@ -274,7 +322,8 @@ class="inline-flex items-center justify-center p-2 rounded-md text-gray-400 hove
<div class="mt-3 space-y-1">

@can('admin')
<x-jet-responsive-nav-link href="{{ route('admin.home') }}" :active="request()->routeIs('admin.home')">
<x-jet-responsive-nav-link href="{{ route('admin.home') }}"
:active="request()->routeIs('admin.home')">
{{ __('Configuración') }}
</x-jet-responsive-nav-link>
@endcan
Expand All @@ -296,8 +345,9 @@ class="inline-flex items-center justify-center p-2 rounded-md text-gray-400 hove
<form method="POST" action="{{ route('logout') }}">
@csrf

<x-jet-responsive-nav-link href="{{ route('logout') }}" onclick="event.preventDefault();
this.closest('form').submit();">
<x-jet-responsive-nav-link href="{{ route('logout') }}"
onclick="event.preventDefault();
this.closest('form').submit();">
{{ __('Cerrar sesión') }}
</x-jet-responsive-nav-link>
</form>
Expand Down
5 changes: 1 addition & 4 deletions routes/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@
use App\Http\Controllers\Admin\CtrSancion;
use App\Http\Controllers\Admin\CtrUnidad;
use App\Http\Controllers\Admin\CtrUser;
use App\Http\Controllers\CtrVisita;
use Illuminate\Support\Facades\Route;

Route::get('/', CtrInicio::class)->middleware('can:admin')->name('home');
Route::get('/', CtrInicio::class)->middleware('can:admin.home')->name('home');

Route::resource('administrador', CtrAdministrador::class)->only(['index', 'show'])->names('administrador');

Expand All @@ -30,5 +29,3 @@
Route::get('usuario', [CtrUser::class, 'index'])->middleware('can:admin.usuario.index')->name('usuario.index');

Route::get('usuario/{usuario}', [CtrUser::class, 'show'])->middleware('can:admin.usuario.show')->name('usuario.show');

Route::get('visita/lista', [CtrVisita::class, 'lista'])->middleware('can:admin.visita.lista')->name('visita.lista');
2 changes: 2 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@

Route::get('visita/{visita}', [CtrVisita::class, 'show'])->name('visita.show');

Route::get('visita/lista', [CtrVisita::class, 'lista'])->middleware('can:visita.lista')->name('visita.lista');

Route::get('unidad', [CtrUnidad::class, 'index'])->name('unidad.index');

Route::get('unidad/{unidad}', [CtrUnidad::class, 'show'])->name('unidad.show');
Expand Down

0 comments on commit 21a49ae

Please sign in to comment.