feat(projects): usuarios/empresas del proyecto como tablas Rappasoft + permiso assign companies
- Permiso 'assign companies' propio (antes empresas reutilizaba 'assign users'); concedido a los roles que ya tenían 'assign users'. - ProjectUsersTable y ProjectCompaniesTable (Rappasoft): búsqueda, filtro por rol, cambio de rol en línea y quitar; gateadas por assign users / assign companies. - ProjectUsers/ProjectCompanies quedan como contenedor (form de asignación) que embebe la tabla y refresca el desplegable vía eventos. - Unificadas confirmaciones (wire:confirm) y notificaciones (dispatch notify). Tests: ProjectAssignmentsTest (4). Suite 69 passing (solo 2 pre-existentes sqlite). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -2,15 +2,15 @@
|
||||
|
||||
namespace App\Livewire;
|
||||
|
||||
use Livewire\Component;
|
||||
use App\Models\Project;
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Livewire\Attributes\On;
|
||||
use Livewire\Component;
|
||||
|
||||
class ProjectUsers extends Component
|
||||
{
|
||||
public Project $project;
|
||||
public $assignedUsers = [];
|
||||
public $allUsers = [];
|
||||
public $selectedUserId = '';
|
||||
public $selectedRole = 'viewer';
|
||||
@@ -18,64 +18,46 @@ class ProjectUsers extends Component
|
||||
public function mount(Project $project)
|
||||
{
|
||||
$this->project = $project;
|
||||
$this->loadUsers();
|
||||
$this->loadAvailable();
|
||||
}
|
||||
|
||||
public function loadUsers()
|
||||
/** Users not yet assigned to the project (for the dropdown). */
|
||||
public function loadAvailable(): void
|
||||
{
|
||||
$this->assignedUsers = $this->project->users()->withPivot('role_in_project')->get();
|
||||
$assignedIds = $this->assignedUsers->pluck('id')->toArray();
|
||||
$assignedIds = $this->project->users()->pluck('users.id')->toArray();
|
||||
$this->allUsers = User::whereNotIn('id', $assignedIds)->orderBy('name')->get();
|
||||
}
|
||||
|
||||
/** Reload the dropdown when the embedded table changes assignments. */
|
||||
#[On('project-users-changed')]
|
||||
public function onUsersChanged(): void
|
||||
{
|
||||
$this->loadAvailable();
|
||||
}
|
||||
|
||||
public function assignUser()
|
||||
{
|
||||
$user = Auth::user();
|
||||
if (!$user->can('assign users')) {
|
||||
session()->flash('error', 'No tienes permisos para asignar usuarios.');
|
||||
return;
|
||||
}
|
||||
abort_unless(Auth::user()->can('assign users'), 403);
|
||||
|
||||
$this->validate([
|
||||
'selectedUserId' => 'required|exists:users,id',
|
||||
'selectedRole' => 'required|in:supervisor,consultant,client,viewer',
|
||||
'selectedRole' => 'required|in:' . implode(',', array_keys(ProjectUsersTable::ROLES)),
|
||||
]);
|
||||
|
||||
$this->project->users()->attach($this->selectedUserId, [
|
||||
'role_in_project' => $this->selectedRole
|
||||
'role_in_project' => $this->selectedRole,
|
||||
]);
|
||||
|
||||
$this->reset(['selectedUserId', 'selectedRole']);
|
||||
$this->loadUsers();
|
||||
$this->loadAvailable();
|
||||
$this->dispatch('project-users-changed');
|
||||
$this->dispatch('notify', 'Usuario asignado al proyecto.');
|
||||
}
|
||||
|
||||
public function removeUser($userId)
|
||||
{
|
||||
$user = Auth::user();
|
||||
if (!$user->can('assign users')) {
|
||||
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)
|
||||
{
|
||||
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');
|
||||
return view('livewire.project-users', [
|
||||
'roles' => ProjectUsersTable::ROLES,
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user