Skip to content

Commit

Permalink
fix team middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshKisb committed Sep 6, 2024
1 parent d1d7573 commit dbe5a84
Showing 1 changed file with 3 additions and 24 deletions.
27 changes: 3 additions & 24 deletions app/Http/Middleware/TeamsPermission.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,16 @@
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Spatie\Permission\PermissionRegistrar;

class TeamsPermission
{
public function handle(Request $request, Closure $next)
{
$user = Auth::user();

if (!$user) {
return redirect()->route('login')->with('error', 'You must be logged in to access this area.');
}

// Allow staff and admin users to access without team restrictions
if ($user->hasRole(['staff', 'admin'])) {
return $next($request);
}

if (!$user->currentTeam) {
// Redirect to a default route or show an error
return redirect()->route('home')->with('error', 'You must be part of a team to access this area.');
}

// Check if the requested team matches the user's current team
$requestedTeamId = $request->route('tenant');
if ($requestedTeamId && $requestedTeamId != $user->currentTeam->id) {
return redirect()->route('staff.dashboard', ['tenant' => $user->currentTeam->id])
->with('error', 'You do not have permission to access this team.');
if (!empty($user = auth()->user()) && !empty($user->current_team_id)) {
app(PermissionRegistrar::class)->setPermissionsTeamId($user->current_team_id);
}

// Check if the user has permission to access the current route
// You can implement your team-based permission logic here

return $next($request);
}
}

0 comments on commit dbe5a84

Please sign in to comment.