Skip to content

Commit

Permalink
No personal Teams
Browse files Browse the repository at this point in the history
  • Loading branch information
nicko170 committed May 4, 2021
1 parent b8d858d commit ad38d10
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 6 deletions.
6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion app/Actions/Fortify/CreateNewUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
}
Expand Down
1 change: 1 addition & 0 deletions app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
];
}
27 changes: 27 additions & 0 deletions app/Http/Middleware/EnsureHasTeam.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;

class EnsureHasTeam
{
public function handle(Request $request, Closure $next)
{
if (!auth()->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();
}
}
}
8 changes: 6 additions & 2 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,25 @@

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;
use Laravel\Fortify\TwoFactorAuthenticatable;
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;

Expand Down
42 changes: 42 additions & 0 deletions app/Traits/HasNoPersonalTeam.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace App\Traits;

trait HasNoPersonalTeam
{

/**
* Determine if the user owns the given team.
*
* @param mixed $team
* @return bool
*/
public function ownsTeam($team)
{
// return $this->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());
}

}
4 changes: 2 additions & 2 deletions resources/views/navigation-menu.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

<div class="hidden sm:flex sm:items-center sm:ml-6">
<!-- Teams Dropdown -->
@if (Laravel\Jetstream\Jetstream::hasTeamFeatures())
@if (Laravel\Jetstream\Jetstream::hasTeamFeatures() && Auth::user()->isMemberOfATeam())
<div class="ml-3 relative">
<x-jet-dropdown align="right" width="60">
<x-slot name="trigger">
Expand Down Expand Up @@ -182,7 +182,7 @@
</form>

<!-- Team Management -->
@if (Laravel\Jetstream\Jetstream::hasTeamFeatures())
@if (Laravel\Jetstream\Jetstream::hasTeamFeatures() && Auth::user()->isMemberOfATeam())
<div class="border-t border-gray-200"></div>

<div class="block px-4 py-2 text-xs text-gray-400">
Expand Down
2 changes: 1 addition & 1 deletion routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@
return view('welcome');
});

Route::middleware(['auth:sanctum', 'verified'])->get('/dashboard', function () {
Route::middleware(['auth:sanctum', 'verified', 'team'])->get('/dashboard', function () {
return view('dashboard');
})->name('dashboard');

0 comments on commit ad38d10

Please sign in to comment.