Files
construprogress/app/Livewire/ProjectUsers.php
T
javier 941dbd5997 restore: bring back f8a1310 (security review) state
Restores all files to the f8a1310 security-review snapshot as requested,
plus the 2 boot-critical fixes from a24c8a2 (config/session.php env()
instead of app()->environment(), and removal of the duplicate $activeTab
in ProjectMap.php) so the application actually boots.

Forward commit, no history rewrite. The 7d854ff state remains in history.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-17 10:36:44 +02:00

90 lines
2.6 KiB
PHP

<?php
namespace App\Livewire;
use Livewire\Component;
use App\Models\Project;
use App\Models\User;
use Illuminate\Support\Facades\Auth;
class ProjectUsers extends Component
{
public Project $project;
public $assignedUsers = [];
public $allUsers = [];
public $selectedUserId = '';
public $selectedRole = 'viewer';
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->loadUsers();
}
public function loadUsers()
{
$this->assignedUsers = $this->project->users()->withPivot('role_in_project')->get();
$assignedIds = $this->assignedUsers->pluck('id')->toArray();
$this->allUsers = User::whereNotIn('id', $assignedIds)->orderBy('name')->get();
}
public function assignUser()
{
$user = Auth::user();
if (!$user->can('assign users') && !$user->hasRole('Admin')) {
session()->flash('error', 'No tienes permisos para asignar usuarios.');
return;
}
$this->validate([
'selectedUserId' => 'required|exists:users,id',
'selectedRole' => 'required|in:supervisor,consultant,client,viewer',
]);
$this->project->users()->attach($this->selectedUserId, [
'role_in_project' => $this->selectedRole
]);
$this->reset(['selectedUserId', 'selectedRole']);
$this->loadUsers();
$this->dispatch('notify', 'Usuario asignado al proyecto.');
}
public function removeUser($userId)
{
$user = Auth::user();
if (!$user->can('assign users') && !$user->hasRole('Admin')) {
session()->flash('error', 'Sin permisos.');
return;
}
$this->project->users()->detach($userId);
$this->loadUsers();
$this->dispatch('notify', 'Usuario eliminado del proyecto.');
}
public function changeRole($userId, $role)
{
$user = Auth::user();
if (!$user->can('assign users') && !$user->hasRole('Admin')) {
session()->flash('error', 'Sin permisos.');
return;
}
if (!in_array($role, ['supervisor', 'consultant', 'client', 'viewer'])) return;
$this->project->users()->updateExistingPivot($userId, [
'role_in_project' => $role
]);
$this->loadUsers();
$this->dispatch('notify', 'Rol actualizado.');
}
public function render()
{
return view('livewire.project-users');
}
}