-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement New Team-Based User Model (#127)
* activate teams and invitations * add missing team policy * add link to team settings * change forms to use team_id * fix initial team creation and add logout to missing team page * change naming * wip make dedicated create team page after signup * fix team creation * isntall telescope * change message for team creation page * fix tests * remove underscores from test * update policies * add tests to preview form * fix migrations for sqlite * change default team name
- Loading branch information
1 parent
5512055
commit f1e19f6
Showing
62 changed files
with
979 additions
and
159 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use Illuminate\Http\Request; | ||
|
||
class MissingTeamController extends Controller | ||
{ | ||
public function __invoke(Request $request) | ||
{ | ||
return inertia('Teams/MissingTeam'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
<?php | ||
|
||
namespace App\Http\Middleware; | ||
|
||
use App\Models\User; | ||
use Closure; | ||
use Illuminate\Http\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
|
||
class EnsureHasTeam | ||
{ | ||
/** | ||
* Handle an incoming request. | ||
* | ||
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next | ||
*/ | ||
public function handle(Request $request, Closure $next): Response | ||
{ | ||
if ($request->expectsJson()) { | ||
return $this->handleApi($request, $next); | ||
} else { | ||
return $this->handleWeb($request, $next); | ||
} | ||
} | ||
|
||
protected function handleApi(Request $request, Closure $next): Response | ||
{ | ||
// if user is not logged in, allow them to proceed | ||
if (! $request->user()) { | ||
return $next($request); | ||
} | ||
|
||
// check if user has a current team | ||
// if they do, allow them to proceed | ||
if ($request->user()->currentTeam) { | ||
return $next($request); | ||
} | ||
|
||
// otherwise, get all teams for the user | ||
$teams = $request->user()->allTeams(); | ||
|
||
// if user has no team at this point, return a 403 | ||
if ($teams->count() === 0) { | ||
return abort(403, 'You are not a member of any team.'); | ||
} | ||
|
||
// otherwise, switch to the first team | ||
$request->user()->switchTeam($teams->first()); | ||
|
||
return $next($request); | ||
} | ||
|
||
protected function handleWeb(Request $request, Closure $next): Response | ||
{ | ||
// if user is not logged in, allow them to proceed | ||
if (! $request->user()) { | ||
return $next($request); | ||
} | ||
|
||
// check if user has a current team | ||
// if they do, allow them to proceed | ||
if ($request->user()->currentTeam) { | ||
return $next($request); | ||
} | ||
|
||
$whitelistedRoutes = [ | ||
'teams.missing', | ||
'teams.create', | ||
'teams.store', | ||
'team-invitations.accept', | ||
'logout', | ||
]; | ||
|
||
// whitelist some routes | ||
if (in_array($request->route()->getName(), $whitelistedRoutes)) { | ||
return $next($request); | ||
} | ||
|
||
// otherwise, get all teams for the user | ||
$teams = $request->user()->allTeams(); | ||
|
||
// if the user is the first user, redirect them to the create team page | ||
if ($teams->count() === 0 && User::count() === 1) { | ||
return redirect()->route('teams.create'); | ||
} | ||
|
||
// if user has no team at this point, redirect them to the missing team page | ||
if ($teams->count() === 0) { | ||
return redirect()->route('teams.missing'); | ||
} | ||
|
||
// otherwise, switch to the first team | ||
$request->user()->switchTeam($teams->first()); | ||
|
||
return $next($request); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.