Implement EnsureUserHasWorkspace Trait and Integrate into Controllers… (#741)
* Implement EnsureUserHasWorkspace Trait and Integrate into Controllers and Jobs - Introduced the EnsureUserHasWorkspace trait to ensure users have at least one workspace when they are detached from a workspace. - Integrated the trait into WorkspaceUserController to enforce workspace checks during user detachment. - Updated RemoveWorkspaceGuests job to utilize the new trait for ensuring users have a workspace after detachment. - Modified UserWorkspace model to call the ensureUserHasWorkspace method upon deletion, maintaining workspace integrity. These changes enhance user management by ensuring that users always have a workspace, improving overall application stability. * Add test --------- Co-authored-by: Julien Nahum <julien@nahum.net>
This commit is contained in:
@@ -4,11 +4,13 @@ namespace App\Jobs\Billing;
|
||||
|
||||
use App\Models\User;
|
||||
use App\Models\Workspace;
|
||||
use App\Traits\EnsureUserHasWorkspace;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class RemoveWorkspaceGuests implements ShouldQueue
|
||||
{
|
||||
@@ -16,6 +18,7 @@ class RemoveWorkspaceGuests implements ShouldQueue
|
||||
use InteractsWithQueue;
|
||||
use Queueable;
|
||||
use SerializesModels;
|
||||
use EnsureUserHasWorkspace;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
@@ -50,13 +53,14 @@ class RemoveWorkspaceGuests implements ShouldQueue
|
||||
|
||||
// Detach all users from the workspace (except the owner)
|
||||
foreach ($workspace->users()->where('users.id', '!=', $this->user->id)->get() as $user) {
|
||||
\Log::info('Detaching user from workspace', [
|
||||
Log::info('Detaching user from workspace', [
|
||||
'workspace_id' => $workspace->id,
|
||||
'workspace_name' => $workspace->name,
|
||||
'user_id' => $user->id,
|
||||
'user_email' => $user->email,
|
||||
]);
|
||||
$workspace->users()->detach($user);
|
||||
$this->ensureUserHasWorkspace($user);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user