From ad38d105380cc797c987f44a80ee122a63fac0f9 Mon Sep 17 00:00:00 2001 From: nicko170 Date: Wed, 5 May 2021 01:02:34 +1000 Subject: [PATCH] No personal Teams --- .idea/vcs.xml | 6 ++++ app/Actions/Fortify/CreateNewUser.php | 2 +- app/Http/Kernel.php | 1 + app/Http/Middleware/EnsureHasTeam.php | 27 +++++++++++++++ app/Models/User.php | 8 +++-- app/Traits/HasNoPersonalTeam.php | 42 +++++++++++++++++++++++ resources/views/navigation-menu.blade.php | 4 +-- routes/web.php | 2 +- 8 files changed, 86 insertions(+), 6 deletions(-) create mode 100644 .idea/vcs.xml create mode 100644 app/Http/Middleware/EnsureHasTeam.php create mode 100644 app/Traits/HasNoPersonalTeam.php diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/app/Actions/Fortify/CreateNewUser.php b/app/Actions/Fortify/CreateNewUser.php index 5c2a969..65f9d08 100644 --- a/app/Actions/Fortify/CreateNewUser.php +++ b/app/Actions/Fortify/CreateNewUser.php @@ -35,7 +35,7 @@ public function create(array $input) 'email' => $input['email'], 'password' => Hash::make($input['password']), ]), function (User $user) { - $this->createTeam($user); + // $this->createTeam($user); }); }); } diff --git a/app/Http/Kernel.php b/app/Http/Kernel.php index ce15205..1930359 100644 --- a/app/Http/Kernel.php +++ b/app/Http/Kernel.php @@ -62,5 +62,6 @@ class Kernel extends HttpKernel 'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class, 'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class, 'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class, + 'team' => \App\Http\Middleware\EnsureHasTeam::class, ]; } diff --git a/app/Http/Middleware/EnsureHasTeam.php b/app/Http/Middleware/EnsureHasTeam.php new file mode 100644 index 0000000..634ee07 --- /dev/null +++ b/app/Http/Middleware/EnsureHasTeam.php @@ -0,0 +1,27 @@ +user()->isMemberOfATeam()) { + return redirect()->route('teams.create'); + } + $this->ensureUserHasCurrentTeamSet(); + return $next($request); + } + + protected function ensureUserHasCurrentTeamSet(): void + { + if (is_null(auth()->user()->current_team_id)) { + $user = auth()->user(); + $user->current_team_id = $user->allTeams()->first()->id; + $user->save(); + } + } +} diff --git a/app/Models/User.php b/app/Models/User.php index 3481e75..37ff28b 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -2,7 +2,7 @@ namespace App\Models; -use Illuminate\Contracts\Auth\MustVerifyEmail; + use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; @@ -10,13 +10,17 @@ use Laravel\Jetstream\HasProfilePhoto; use Laravel\Jetstream\HasTeams; use Laravel\Sanctum\HasApiTokens; +use App\Traits\HasNoPersonalTeam; class User extends Authenticatable { use HasApiTokens; use HasFactory; use HasProfilePhoto; - use HasTeams; + use HasNoPersonalTeam, HasTeams { + HasNoPersonalTeam::ownsTeam insteadof HasTeams; + HasNoPersonalTeam::isCurrentTeam insteadof HasTeams; + } use Notifiable; use TwoFactorAuthenticatable; diff --git a/app/Traits/HasNoPersonalTeam.php b/app/Traits/HasNoPersonalTeam.php new file mode 100644 index 0000000..35f7199 --- /dev/null +++ b/app/Traits/HasNoPersonalTeam.php @@ -0,0 +1,42 @@ +id == $team->user_id; + return $this->id == optional($team)->user_id; + } + + /** + * Determine if the given team is the current team. + * + * @param mixed $team + * @return bool + */ + public function isCurrentTeam($team) + { + return optional($team)->id === $this->currentTeam->id; + } + + /** + * Determine if the user is apart of any team. + * + * @param mixed $team + * @return bool + */ + public function isMemberOfATeam(): bool + { + return (bool) ($this->teams()->count() || $this->ownedTeams()->count()); + } + +} diff --git a/resources/views/navigation-menu.blade.php b/resources/views/navigation-menu.blade.php index c066aa5..f64e057 100644 --- a/resources/views/navigation-menu.blade.php +++ b/resources/views/navigation-menu.blade.php @@ -20,7 +20,7 @@