authorize('manageCollaborators', $project); $data = $request->validate([ 'email' => ['required', 'email', 'exists:users,email'], 'role' => ['nullable', 'in:editor,viewer'], ]); $user = User::where('email', $data['email'])->where('is_active', true)->firstOrFail(); // Owner tidak boleh jadi collaborator if ($project->isOwnedBy($user)) { return back()->withErrors(['email' => 'Pengguna ini adalah pemilik projek.']); } // Elak duplicate $existing = ProjectCollaborator::where('project_id', $project->id) ->where('user_id', $user->id) ->exists(); if ($existing) { return back()->withErrors(['email' => 'Pengguna ini sudah menjadi collaborator.']); } ProjectCollaborator::create([ 'project_id' => $project->id, 'user_id' => $user->id, 'role' => $data['role'] ?? 'editor', 'added_by' => auth()->id(), ]); $audit->log('collaborator_added', [ 'project_id' => $project->id, 'target_user_id' => $user->id, 'new_values' => ['email' => $user->email, 'role' => $data['role'] ?? 'editor'], ]); return back()->with('success', "{$user->name} berjaya ditambah sebagai collaborator."); } public function destroy(TranscriptionProject $project, User $user, AuditLogService $audit): RedirectResponse { $this->authorize('manageCollaborators', $project); ProjectCollaborator::where('project_id', $project->id) ->where('user_id', $user->id) ->delete(); $audit->log('collaborator_removed', [ 'project_id' => $project->id, 'target_user_id' => $user->id, 'old_values' => ['email' => $user->email], ]); return back()->with('success', "{$user->name} telah dibuang daripada collaborator."); } }