Files
construprogress/app/Livewire/TemplateManager.php
T
javier f8a1310c0f security: fix 27 vulnerabilities + UI integration (Issues tab, project nav, validation)
Security fixes (27 vulnerabilities across 20 files):
CRITICAL:
- MediaManager: whitelist mediable types prevents RCE via class instantiation
- MediaManager/OfflineSyncController: IDOR fixes, remove Auth::id()??1 fallback
- ClientProjects: verify project ownership on all mutations (IDOR)
- CompanyManagement: Admin role check on mount() and mutations (auth bypass)
- ProjectMap: scope feature/template lookups to current project (IDOR x5)
- PhaseList/TemplateManager/LayerManager: scope mutations to owned resources (IDOR)
- ProjectEditTabs: Gate::authorize on mount() and updateProject()
- routes/web.php: reports routes moved inside can:manage all middleware (auth bypass)

MEDIUM:
- layer-manager: escapeHtml() on Leaflet popup interpolations (XSS)
- MediaManager: server-side MIME validation + 50MB limit
- ProjectList/ProjectUsers/ProjectCompanies/PhaseProgress: auth checks added
- AdminUsers/ReportsDashboard/ExportController: role/permission checks added

LOW:
- config/session.php: secure cookie tied to production env
- OfflineSyncController: sanitize storage path (path traversal)

UI integration:
- project-map: Issues tab (4th) with open-count badge
- project-map: project navigation bar (Dashboard/Map/Gantt/Report/Issues)
- project-dashboard: action buttons for Map/Gantt/Report/Issues
- project-form: validation error summary + per-field @error spans
- template-manager: validation error display

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-16 18:25:36 +02:00

161 lines
4.4 KiB
PHP

<?php
namespace App\Livewire;
use Livewire\Component;
use App\Models\InspectionTemplate;
use App\Models\Project;
use App\Models\Phase;
use Illuminate\Support\Facades\Auth;
class TemplateManager extends Component
{
public $project;
public $templates;
public $phases;
public $editingTemplate = null;
public $showForm = false; // Controla si mostrar el formulario
public $form = [
'name' => '',
'description' => '',
'phase_id' => null,
'fields' => [],
];
public $fieldTypes = [
'text' => 'Texto corto',
'textarea' => 'Texto largo',
'integer' => 'Número entero',
'decimal' => 'Número decimal',
'percentage' => 'Porcentaje (0-100)',
'boolean' => 'Sí/No (checkbox)',
'date' => 'Fecha',
'select' => 'Lista desplegable',
];
protected $listeners = ['showTemplateForm' => 'newTemplate'];
public function mount(Project $project)
{
$user = Auth::user();
if (!$user->hasRole('Admin') && !$project->users()->where('user_id', $user->id)->exists()) {
abort(403);
}
$this->project = $project;
$this->loadPhases();
$this->loadTemplates();
}
public function loadPhases()
{
$this->phases = $this->project->phases()->orderBy('name')->get();
}
public function loadTemplates()
{
$this->templates = InspectionTemplate::where('project_id', $this->project->id)->get();
}
public function newTemplate()
{
$this->resetForm();
$this->editingTemplate = null;
$this->showForm = true;
}
public function editTemplate($id)
{
$template = InspectionTemplate::where('id', $id)
->where('project_id', $this->project->id)
->firstOrFail();
$this->form = $template->only(['name', 'description', 'phase_id', 'fields']);
$this->editingTemplate = $id;
$this->showForm = true;
}
public function cancelForm()
{
$this->showForm = false;
$this->resetForm();
}
public function resetForm()
{
$this->form = [
'name' => '',
'description' => '',
'phase_id' => null,
'fields' => [],
];
$this->editingTemplate = null;
}
public function addField()
{
$this->form['fields'][] = [
'name' => '',
'label' => '',
'type' => 'text',
'options' => [],
'required' => false,
'min' => null,
'max' => null,
'step' => null,
];
}
public function removeField($index)
{
unset($this->form['fields'][$index]);
$this->form['fields'] = array_values($this->form['fields']);
}
public function saveTemplate()
{
$this->validate([
'form.name' => 'required|string|max:255',
'form.phase_id' => 'nullable|exists:phases,id',
'form.fields' => 'array',
]);
if ($this->editingTemplate) {
$template = InspectionTemplate::where('id', $this->editingTemplate)
->where('project_id', $this->project->id)
->firstOrFail();
$template->update([
'name' => $this->form['name'],
'description' => $this->form['description'],
'phase_id' => $this->form['phase_id'],
'fields' => $this->form['fields'],
]);
session()->flash('message', 'Template actualizado');
} else {
InspectionTemplate::create([
'name' => $this->form['name'],
'description' => $this->form['description'],
'project_id' => $this->project->id,
'phase_id' => $this->form['phase_id'],
'fields' => $this->form['fields'],
]);
session()->flash('message', 'Template creado');
}
$this->cancelForm();
$this->loadTemplates();
}
public function deleteTemplate($id)
{
InspectionTemplate::where('id', $id)
->where('project_id', $this->project->id)
->firstOrFail()
->delete();
$this->loadTemplates();
session()->flash('message', 'Template eliminado');
}
public function render()
{
return view('livewire.template-manager');
}
}