Files
petriandClaude Sonnet 5.5 32c33e2a64 Give the machine console its own session with PIN sign-in, make counters cumulative
The console under console/ has its own session cookie and console guard
(UseConsoleSession), so signing out or changing user there leaves the
normal interface alone. Links open it in a new window through a one-time
handoff key; a signed-out console asks for a PIN code or password on its
own login page. Users set their 4-6 digit PIN on the profile's PIN page,
and the console's change user dialog takes it on a keypad; five wrong
PINs lock PIN sign-in for 15 minutes.

Machine counters are cumulative: users record how much a job used and it
is added to the latest reading. A maintenance record restarts the whole
counter interval from the reading at the record.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
2026-11-29 09:58:11 +02:00

104 lines
3.6 KiB
PHP

<?php
namespace App\Modules\Users\Infrastructure\Persistence\Repositories;
use App\Models\User;
use App\Modules\Users\Application\Contracts\Clock;
use App\Modules\Users\Application\Contracts\UserAccountQueries;
use App\Modules\Users\Application\DTOs\UserOption;
use App\Modules\Users\Application\DTOs\UserSummary;
use App\Modules\Users\Domain\Enums\Role;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Database\Eloquent\Builder;
class EloquentUserAccountQueries implements UserAccountQueries
{
public function __construct(
private Clock $clock,
) {}
public function findSummary(int $id): ?UserSummary
{
$model = User::query()->with(['supervisor:id,name', 'moneyPermissions'])->find($id);
return $model === null ? null : $this->toSummary($model);
}
public function paginate(?string $search, int $perPage = 25): LengthAwarePaginator
{
$search = trim((string) $search);
return User::query()
->with('supervisor:id,name')
->when($search !== '', function (Builder $query) use ($search): void {
$pattern = '%'.addcslashes($search, '%_\\').'%';
$query->where(fn (Builder $query) => $query
->where('name', 'like', $pattern)
->orWhere('email', 'like', $pattern));
})
->orderBy('name')
->paginate($perPage)
->through(fn (User $model): UserSummary => $this->toSummary($model));
}
public function supervisorOptions(?int $exceptUserId = null): array
{
return User::query()
->whereIn('role', array_filter(Role::cases(), fn (Role $role): bool => $role->canSupervise()))
->where('is_active', true)
->when($exceptUserId !== null, fn (Builder $query) => $query->whereKeyNot($exceptUserId))
->orderBy('name')
->get(['id', 'name'])
->map(fn (User $model): UserOption => new UserOption($model->id, $model->name))
->values()
->all();
}
public function activeUserOptions(): array
{
return User::query()
->where('is_active', true)
->orderBy('name')
->get(['id', 'name', 'role'])
->map(fn (User $model): UserOption => new UserOption($model->id, $model->name, $model->role))
->values()
->all();
}
public function pinLoginOptions(): array
{
$today = $this->clock->today()->format('Y-m-d');
return User::query()
->where('is_active', true)
->whereNotNull('pin_hash')
->where(fn (Builder $query) => $query->whereNull('employment_ends_on')->orWhereDate('employment_ends_on', '>=', $today))
->orderBy('name')
->get(['id', 'name', 'role'])
->map(fn (User $model): UserOption => new UserOption($model->id, $model->name, $model->role))
->values()
->all();
}
public function namesByIds(array $ids): array
{
return $ids === [] ? [] : User::query()->whereIn('id', $ids)->pluck('name', 'id')->all();
}
private function toSummary(User $model): UserSummary
{
return new UserSummary(
id: $model->id,
name: $model->name,
email: $model->email,
role: $model->role,
active: $model->is_active,
employmentEndsOn: $model->employment_ends_on?->toDateString(),
supervisorId: $model->supervisor_id,
supervisorName: $model->supervisor?->name,
moneyPermissions: $model->moneyPermissions?->permissions() ?? [],
);
}
}