first
This commit is contained in:
72
app/Http/Controllers/User/CollaboratorController.php
Normal file
72
app/Http/Controllers/User/CollaboratorController.php
Normal file
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\User;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\ProjectCollaborator;
|
||||
use App\Models\TranscriptionProject;
|
||||
use App\Models\User;
|
||||
use App\Services\AuditLogService;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class CollaboratorController extends Controller
|
||||
{
|
||||
public function store(Request $request, TranscriptionProject $project, AuditLogService $audit): RedirectResponse
|
||||
{
|
||||
$this->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.");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user