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,6 +4,7 @@ namespace App\Http\Controllers;
|
||||
|
||||
use App\Jobs\Billing\WorkspaceUsersUpdated;
|
||||
use App\Models\UserInvite;
|
||||
use App\Traits\EnsureUserHasWorkspace;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Models\Workspace;
|
||||
use App\Models\User;
|
||||
@@ -11,6 +12,8 @@ use App\Service\WorkspaceHelper;
|
||||
|
||||
class WorkspaceUserController extends Controller
|
||||
{
|
||||
use EnsureUserHasWorkspace;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->middleware('auth');
|
||||
@@ -106,7 +109,9 @@ class WorkspaceUserController extends Controller
|
||||
$workspace = Workspace::findOrFail($workspaceId);
|
||||
$this->authorize('adminAction', $workspace);
|
||||
|
||||
$user = User::findOrFail($userId);
|
||||
$workspace->users()->detach($userId);
|
||||
$this->ensureUserHasWorkspace($user);
|
||||
WorkspaceUsersUpdated::dispatch($workspace);
|
||||
|
||||
return $this->success([
|
||||
@@ -119,7 +124,9 @@ class WorkspaceUserController extends Controller
|
||||
$workspace = Workspace::findOrFail($workspaceId);
|
||||
$this->authorize('view', $workspace);
|
||||
|
||||
$workspace->users()->detach($request->user()->id);
|
||||
$user = $request->user();
|
||||
$workspace->users()->detach($user->id);
|
||||
$this->ensureUserHasWorkspace($user);
|
||||
|
||||
return $this->success([
|
||||
'message' => 'You have left the workspace successfully.'
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -2,12 +2,15 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Traits\EnsureUserHasWorkspace;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class UserWorkspace extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
use EnsureUserHasWorkspace;
|
||||
|
||||
protected $table = 'user_workspace';
|
||||
|
||||
protected $fillable = [
|
||||
@@ -25,4 +28,13 @@ class UserWorkspace extends Model
|
||||
{
|
||||
return $this->belongsTo(Workspace::class);
|
||||
}
|
||||
|
||||
protected static function boot()
|
||||
{
|
||||
parent::boot();
|
||||
|
||||
static::deleted(function (UserWorkspace $userWorkspace) {
|
||||
$userWorkspace->ensureUserHasWorkspace($userWorkspace->user);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
34
api/app/Traits/EnsureUserHasWorkspace.php
Normal file
34
api/app/Traits/EnsureUserHasWorkspace.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace App\Traits;
|
||||
|
||||
use App\Models\User;
|
||||
use App\Models\Workspace;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
trait EnsureUserHasWorkspace
|
||||
{
|
||||
/**
|
||||
* Ensure a user has at least one workspace.
|
||||
* If they don't, create a new workspace for them.
|
||||
*/
|
||||
protected function ensureUserHasWorkspace(User $user): void
|
||||
{
|
||||
if ($user->workspaces()->count() === 0) {
|
||||
Log::info('Creating new workspace for user with no workspaces', [
|
||||
'user_id' => $user->id,
|
||||
'user_email' => $user->email,
|
||||
]);
|
||||
|
||||
$newWorkspace = Workspace::create([
|
||||
'name' => 'My Workspace',
|
||||
]);
|
||||
|
||||
$user->workspaces()->sync([
|
||||
$newWorkspace->id => [
|
||||
'role' => 'admin',
|
||||
],
|
||||
], false);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user