Skip to content

Make personal teams optional #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion laravel/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 laravel/app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,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 laravel/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();
}
}
}
6 changes: 5 additions & 1 deletion laravel/app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@
use Laravel\Jetstream\HasTeams;
use Laravel\Sanctum\HasApiTokens;
use Spatie\Permission\Traits\HasRoles;
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;
use HasRoles;
Expand Down
41 changes: 41 additions & 0 deletions laravel/app/Traits/HasNoPersonalTeam.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?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());
}
}
2 changes: 1 addition & 1 deletion laravel/database/seeders/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function run()
{
$this->call([
PermissionTableSeeder::class,
TeamTableSeeder::class,
// TeamTableSeeder::class,
// Add user seeder
UserTableSeeder::class,

Expand Down
4 changes: 2 additions & 2 deletions laravel/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 @@ -180,7 +180,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
7 changes: 2 additions & 5 deletions laravel/routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,8 @@
Route::get('/phpinfo', function () {
return phpinfo();
});
Route::middleware([
'auth:sanctum',
config('jetstream.auth_session'),
'verified'
])->group(function () {

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