Avances con varias cosas~

This commit is contained in:
2025-01-17 08:31:59 -03:00
parent ee19e479f4
commit 5fc8e2bda1
21 changed files with 463 additions and 121 deletions

View File

@@ -1,50 +0,0 @@
FROM dunglas/frankenphp:php8.4
ARG USER=app
ARG UID=1000
ARG GID=1000
ENV XDG_CONFIG_HOME=/home/${USER}/.config
RUN apt update && apt install -y nginx git curl zip unzip supervisor libpq-dev libzip-dev libicu-dev; \
curl -fsSL https://deb.nodesource.com/setup_23.x -o nodesource_setup.sh; \
bash nodesource_setup.sh; \
apt install nodejs; \
pecl install xdebug; \
install-php-extensions pdo_pgsql; \
install-php-extensions zip; \
install-php-extensions bcmath; \
install-php-extensions intl; \
install-php-extensions xdebug; \
install-php-extensions pcntl; \
apt clean; \
rm -rf /var/lib/{apt,dpkg,cache,log}/; \
apt clean autoclean; \
apt autoremove --yes
WORKDIR /app
COPY --from=composer:lts /usr/bin/composer /usr/local/bin/composer
COPY composer.json composer.lock ./
RUN composer install --no-dev --prefer-dist --no-autoloader --optimize-autoloader && rm -rf /root/.composer/cache/*
COPY ./ ./
RUN groupadd -g ${GID} ${USER}; \
useradd -m -u ${UID} -g ${GID} ${USER}; \
setcap CAP_NET_BIND_SERVICE=+eip /usr/local/bin/frankenphp; \
mkdir -p /home/${USER}/.config; \
chown -R ${UID}:${GID} /data/caddy; \
chown -R ${UID}:${GID} /config/caddy; \
chown -R ${UID}:${GID} /app; \
chown -R ${UID}:${GID} /home/${USER};
USER ${USER}
RUN composer dump-autoload; \
npm install; \
npm run build; \
npm cache clean --force; \
rm -rf node_modules; \
rm -rf /tmp/* /var/tmp/*
EXPOSE 80
ENTRYPOINT ["php", "artisan", "octane:frankenphp", "--port=80"]

View File

@@ -239,6 +239,27 @@ namespace App\Models{
class Ingreso extends \Eloquent {}
}
namespace App\Models{
/**
*
*
* @property int $id
* @property string $name
* @property string|null $title
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @method static \Illuminate\Database\Eloquent\Builder<static>|Role newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder<static>|Role newQuery()
* @method static \Illuminate\Database\Eloquent\Builder<static>|Role query()
* @method static \Illuminate\Database\Eloquent\Builder<static>|Role whereCreatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder<static>|Role whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder<static>|Role whereName($value)
* @method static \Illuminate\Database\Eloquent\Builder<static>|Role whereTitle($value)
* @method static \Illuminate\Database\Eloquent\Builder<static>|Role whereUpdatedAt($value)
*/
class Role extends \Eloquent {}
}
namespace App\Models{
/**
*
@@ -289,6 +310,8 @@ namespace App\Models{
* @property \Illuminate\Support\Carbon|null $updated_at
* @property-read \Illuminate\Notifications\DatabaseNotificationCollection<int, \Illuminate\Notifications\DatabaseNotification> $notifications
* @property-read int|null $notifications_count
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Role> $roles
* @property-read int|null $roles_count
* @method static \Database\Factories\UserFactory factory($count = null, $state = [])
* @method static \Illuminate\Database\Eloquent\Builder<static>|User newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder<static>|User newQuery()

10
app/Enums/RoleName.php Normal file
View File

@@ -0,0 +1,10 @@
<?php
namespace App\Enums;
enum RoleName: string
{
case SuperAdmin = 'superadmin';
case Admin = 'admin';
case User = 'user';
}

View File

@@ -0,0 +1,118 @@
<?php
namespace App\Livewire\Usuarios;
use App\Enums\RoleName;
use App\Models\Role;
use App\Models\User;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Session;
use Illuminate\Validation\Rule;
use Livewire\Attributes\Computed;
use Livewire\Attributes\Locked;
use Livewire\Attributes\Validate;
use Livewire\Component;
use TallStackUi\Traits\Interactions;
class Edit extends Component
{
use Interactions;
#[Locked]
public ?User $user = null;
public $name = null;
public $email = null;
public $password = null;
public $password_confirmation = null;
public $roles = [];
public $change_password = false;
public function mount(): void
{
if ($this->user) {
$this->name = $this->user->name;
$this->email = $this->user->email;
$this->roles = $this->user->roles()->pluck('id')->toArray();
}
}
public function render(): View
{
return view('livewire.usuarios.edit');
}
public function rules(): array
{
return [
'name' => 'required|string',
'email' => ['required', 'email', Rule::unique('users', 'email')->ignore($this->user->id)],
'password' => $this->change_password ? 'required|string|min:8|confirmed' : 'nullable|string|min:8|confirmed',
'password_confirmation' => $this->change_password ? 'required' : 'nullable',
'roles' => 'nullable',
];
}
public function save(): void
{
$this->validate();
if ($this->user) {
$this->update();
} else {
$this->store();
}
$this->redirectRoute('usuarios.index', navigate: true);
}
public function update(): void
{
$this->authorize('update', $this->user);
$this->user->name = $this->name;
$this->user->email = $this->email;
if ($this->change_password) {
$this->user->password = Hash::make($this->password);
}
$this->user->save();
$isSuperAdmin = $this->user->roles()->where('name', RoleName::SuperAdmin)->exists();
$this->user->roles()->sync($this->roles);
if ($isSuperAdmin) {
$this->user->roles()->attach(Role::where('name', RoleName::SuperAdmin)->first()->id);
}
Session::flash('toast', ['type' => 'success', 'message' => 'Usuario modificado correctamente']);
}
public function store(): void
{
$this->authorize('create', User::class);
$user = User::create([
'name' => $this->name,
'email' => $this->email,
'password' => Hash::make($this->password),
]);
$user->roles()->sync($this->roles);
Session::flash('toast', ['type' => 'success', 'message' => 'Usuario registrado correctamente']);
}
#[Computed]
public function availableRoles(): Collection
{
return Role::whereNot('name', RoleName::SuperAdmin)->get();
}
}

View File

@@ -0,0 +1,66 @@
<?php
namespace App\Livewire\Usuarios;
use App\Enums\RoleName;
use App\Models\Turno;
use App\Models\User;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Facades\Gate;
use Livewire\Attributes\Computed;
use Livewire\Component;
use TallStackUi\Traits\Interactions;
class Index extends Component
{
use Interactions;
public function render(): View
{
return view('livewire.usuarios.index');
}
public function confirmDelete($id): void
{
$user = User::find($id);
$this->authorize('delete', $user);
if ($user->roles()->where('name', RoleName::SuperAdmin)->exists()) {
$this->toast()->error('Error', 'No se puede eliminar un super administrador')->send();
return;
}
$this->dialog()->question('¿Esta seguro de eliminar este usuario?',
'No podra ser recuperado')
->confirm('Eliminar Usuario', method: 'doDelete', params: $id)
->cancel()
->send();
}
public function doDelete($id): void
{
$user = User::find($id);
$this->authorize('delete', $user);
$user->delete();
$this->toast()->success('Usuario eliminado correctamente')->send();
}
#[Computed]
public function headers(): array
{
return [
['index' => 'name', 'label' => 'Nombre'],
['index' => 'email', 'label' => 'Email'],
['index' => 'action', 'label' => 'Acciones'],
];
}
#[Computed]
public function rows(): LengthAwarePaginator
{
return User::paginate();
}
}

22
app/Models/Role.php Normal file
View File

@@ -0,0 +1,22 @@
<?php
namespace App\Models;
use App\Enums\RoleName;
use Illuminate\Database\Eloquent\Concerns\HasUlids;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
class Role extends Model
{
use HasUlids;
protected $table = 'roles';
protected $fillable = ['name', 'title'];
protected $casts = ['name' => RoleName::class];
public function users(): BelongsToMany
{
return $this->belongsToMany(User::class, 'user_roles', 'role_id', 'user_id');
}
}

View File

@@ -4,6 +4,7 @@ namespace App\Models;
use Illuminate\Database\Eloquent\Concerns\HasUlids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
@@ -24,6 +25,11 @@ class User extends Authenticatable
'remember_token',
];
public function roles(): BelongsToMany
{
return $this->belongsToMany(Role::class, 'user_roles', 'user_id', 'role_id');
}
protected function casts(): array
{
return [

View File

@@ -0,0 +1,77 @@
<?php
namespace App\Policies;
use App\Enums\RoleName;
use App\Models\User;
use Log;
class UserPolicy
{
public function before(User $user, string $ability): bool|null
{
$isAdmin = $user->roles()->whereIn('name', [RoleName::SuperAdmin, RoleName::Admin])->exists();
if (!$isAdmin) {
return false;
}
return null;
}
/**
* Determine whether the user can view any models.
*/
public function viewAny(User $user): bool
{
return true;
}
/**
* Determine whether the user can view the model.
*/
public function view(User $user, User $model): bool
{
return true;
}
/**
* Determine whether the user can create models.
*/
public function create(User $user): bool
{
return true;
}
/**
* Determine whether the user can update the model.
*/
public function update(User $user, User $model): bool
{
return true;
}
/**
* Determine whether the user can delete the model.
*/
public function delete(User $user, User $model): bool
{
return true;
}
/**
* Determine whether the user can restore the model.
*/
public function restore(User $user, User $model): bool
{
return true;
}
/**
* Determine whether the user can permanently delete the model.
*/
public function forceDelete(User $user, User $model): bool
{
return true;
}
}

View File

@@ -1,28 +0,0 @@
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Livewire\Volt\Volt;
class VoltServiceProvider extends ServiceProvider
{
/**
* Register services.
*/
public function register(): void
{
//
}
/**
* Bootstrap services.
*/
public function boot(): void
{
Volt::mount([
config('livewire.view_path', resource_path('views/livewire')),
resource_path('views/pages'),
]);
}
}

View File

@@ -1,17 +0,0 @@
<?php
namespace App\View\Components;
use Illuminate\View\Component;
use Illuminate\View\View;
class AppLayout extends Component
{
/**
* Get the view / contents that represents the component.
*/
public function render(): View
{
return view('layouts.app');
}
}

View File

@@ -1,17 +0,0 @@
<?php
namespace App\View\Components;
use Illuminate\View\Component;
use Illuminate\View\View;
class GuestLayout extends Component
{
/**
* Get the view / contents that represents the component.
*/
public function render(): View
{
return view('layouts.guest');
}
}

View File

@@ -0,0 +1,29 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration {
public function up(): void
{
Schema::create('roles', function (Blueprint $table) {
$table->ulid('id')->primary();
$table->text('name');
$table->text('title')->nullable();
$table->timestamps();
});
Schema::create('user_roles', function (Blueprint $table) {
$table->foreignUlid('role_id')->constrained('roles')->cascadeOnDelete();
$table->foreignUlid('user_id')->constrained('users')->cascadeOnDelete();
});
}
public function down(): void
{
Schema::dropIfExists('user_roles');
Schema::dropIfExists('roles');
}
};

View File

@@ -6,5 +6,10 @@ use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
public function run(): void {}
public function run(): void
{
$this->call([
RolesSeeder::class
]);
}
}

View File

@@ -0,0 +1,30 @@
<?php
namespace Database\Seeders;
use App\Models\Role;
use Illuminate\Database\Seeder;
class RolesSeeder extends Seeder
{
public function run(): void
{
Role::firstOrCreate([
'name' => 'superadmin'
], [
'title' => 'Super Administrador'
]);
Role::firstOrCreate([
'name' => 'admin',
], [
'title' => 'Administrador'
]);
Role::firstOrCreate([
'name' => 'user',
], [
'title' => 'Usuario'
]);
}
}

View File

@@ -11,7 +11,7 @@
<img class="h-8 rounded" src="https://api.dicebear.com/9.x/thumbs/svg" alt="avatar"/>
<div>
<p class="text-sm">{{auth()->user()->name}}</p>
<p class="text-xs text-secondary">Administrador</p>
<p class="text-xs text-secondary">{{auth()->user()->email}}</p>
</div>
</button>
</x-slot:action>

View File

@@ -1,3 +1 @@
<div>
<h1 class="text-4xl mb-8 underline decoration-primary decoration-3">{{$slot}}</h1>
</div>
<h1 class="text-4xl underline decoration-primary decoration-3">{{$slot}}</h1>

View File

@@ -1,6 +1,6 @@
@use(Illuminate\Support\Number)
<div>
<div class="flex gap-2 justify-between items-baseline">
<div class="flex gap-2 justify-between items-baseline mb-4">
<x-title>
{{$this->turno ? "Editar caja" : "Registrar Caja"}}
</x-title>

View File

@@ -1,12 +1,15 @@
@use(Illuminate\Support\Number)
<div>
<x-title>Cajas</x-title>
<div class="flex gap-2 justify-between items-end">
<x-date label="Fecha" wire:model.live="searchFecha" helpers/>
<div class="flex flex-col sm:flex-row gap-2 justify-between items-baseline mb-4">
<x-title>Cajas</x-title>
<x-button wire:click="createTurno" icon="plus">Registrar Caja</x-button>
</div>
<div class="flex">
<x-date label="Fecha" wire:model.live="searchFecha" helpers/>
</div>
<div class="mt-4">
<x-table striped :headers="$this->headers" :rows="$this->rows" paginate>
@interact('column_fecha', $row)

View File

@@ -0,0 +1,38 @@
<div class="max-w-screen-sm mx-auto">
<div class="mb-4">
@if($this->user)
<x-title>Editar Usuario</x-title>
@else
<x-title>Crear Usuario</x-title>
@endif
</div>
<form wire:submit.prevent="save">
<x-card class="space-y-4">
<x-input label="Nombre" wire:model="name"/>
<x-input label="Correo Electrónico" wire:model="email"/>
<x-select.styled label="Roles" :options="$this->availableRoles" select="label:title|value:id" multiple
wire:model="roles"/>
@if($this->user)
<x-checkbox label="Cambiar contraseña" wire:model.live="change_password"/>
@endif
@if(!$this->user || $change_password)
<x-password label="Contraseña" wire:model="password"/>
<x-password label="Confirmación de la contraseña" wire:model="password_confirmation"/>
@endif
<x-slot:footer>
<x-button wire:navigate :href="route('usuarios.index')" icon="arrow-left" color="secondary">
Volver
</x-button>
<x-button type="submit" icon="plus">
Guardar
</x-button>
</x-slot:footer>
</x-card>
</form>
</div>

View File

@@ -0,0 +1,22 @@
@php use App\Models\User; @endphp
<div>
<div class="flex flex-col sm:flex-row gap-2 justify-between items-baseline mb-4">
<x-title>Usuarios</x-title>
@can('create', User::class)
<x-button :href="route('usuarios.create')" wire:navigate icon="plus">Registrar Usuario</x-button>
@endcan
</div>
<div class="mt-4">
<x-table striped :headers="$this->headers" :rows="$this->rows" paginate>
@interact('column_action', $row)
@can('update', $row)
<x-button.circle icon="edit" :href="route('usuarios.edit', $row->id)" wire:navigate :key="uniqid()"/>
@endcan
@can('delete', $row)
<x-button.circle icon="trash" color="red" wire:click="confirmDelete('{{$row->id}}')" :key="uniqid()"/>
@endcan
@endinteract
</x-table>
</div>
</div>

View File

@@ -1,6 +1,7 @@
<?php
use App\Http\Controllers\Auth\LogoutController;
use App\Models\User;
use Illuminate\Support\Facades\Route;
Route::middleware('guest')->group(function () {
@@ -21,4 +22,10 @@ Route::middleware('auth')->group(function () {
Route::get('/', App\Livewire\Cajas\Index::class)->name('index');
Route::get('/{turno}/edit', App\Livewire\Cajas\Edit::class)->name('edit');
});
Route::name('usuarios.')->prefix('usuarios')->group(function () {
Route::get('/', App\Livewire\Usuarios\Index::class)->can('view-any', User::class)->name('index');
Route::get('/create', App\Livewire\Usuarios\Edit::class)->can('create', User::class)->name('create');
Route::get('/edit/{user}', App\Livewire\Usuarios\Edit::class)->can('update', 'user')->name('edit');
});
});