Skip to content
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
/public/js
/public/fonts
/storage/*.key
/storage/debugbar
/vendor
.env
.env.testing
Expand Down
13 changes: 9 additions & 4 deletions app-modules/authentication/src/Actions/AuthenticateAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public function withOAuth(string $tenantSlug, OAuthProviderEnum $oauthProvider,
$user = $clientProvider->getAuthenticatedUser($accessData);

$provider = Provider::query()
->where('tenant_id', $tenant->getKey())
->where('provider', $user->provider)
->where('provider_id', $user->providerId)
->first();
Expand All @@ -34,21 +35,25 @@ public function withOAuth(string $tenantSlug, OAuthProviderEnum $oauthProvider,
$provider = $this->registerNewUser($user, $tenant);
}

Auth::logout();
Auth::login($provider->user);
filament()->auth()->setUser($provider->user);
if (! auth()->check()) {
Auth::logout();
Auth::login($provider->user);
filament()->auth()->setUser($provider->user);
}
}

private function registerNewUser(OAuthUserDTO $userDTO, Tenant $tenant): Provider
{
$user = User::query()->firstOrCreate(['email' => $userDTO->email], [
$user = auth()->check() ? auth()->user() : User::query()->firstOrCreate(['email' => $userDTO->email], [
'id' => Uuid::uuid4()->toString(),
'username' => $userDTO->username,
'name' => $userDTO->name,
'password' => Hash::make(Date::now()->getTimestamp().'-vai-brasil'),
'is_donator' => false,
]);

$user->tenants()->attach($tenant);

/** @var Provider $provider */
$provider = $user->providers()->updateOrCreate([
'tenant_id' => $tenant->getKey(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

interface OAuthClientContract
{
public function redirectUrl(): string;
public function redirectUrl(?string $state = null): string;

public function auth(string $code): OAuthAccessDTO;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

class DiscordOAuthClient implements OAuthClientContract
{
public function redirectUrl(): string
public function redirectUrl(?string $state = null): string
{
return sprintf(
'https://discord.com/oauth2/authorize?client_id=%s&response_type=code&redirect_uri=%s&scope=%s',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,21 @@
use He4rt\Integrations\Twitch\OAuth\Contracts\TwitchOAuthService;
use He4rt\Integrations\Twitch\OAuth\DTO\TwitchOAuthAccessDTO;
use He4rt\Integrations\Twitch\OAuth\DTO\TwitchOAuthDTO;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Date;

final readonly class TwitchOAuthClient implements TwitchOAuthService
{
public function __construct(private Client $client) {}

public function redirectUrl(): string
public function redirectUrl(?string $state = null): string
{
return sprintf(
'https://id.twitch.tv/oauth2/authorize?client_id=%s&redirect_uri=%s&response_type=code&scope=%s',
'https://id.twitch.tv/oauth2/authorize?client_id=%s&redirect_uri=%s&response_type=code&scope=%s&state=%s',
config('services.twitch.client_id'),
config('services.twitch.redirect_uri'),
config('services.twitch.scopes')
config('services.twitch.scopes'),
Crypt::encryptString($state ?? Date::now()->getTimestamp())
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('tenant_users', function (Blueprint $table): void {
$table->foreignId('tenant_id')->constrained('tenants')->cascadeOnDelete();
$table->foreignUuid('user_id')->constrained('users')->cascadeOnDelete();
$table->timestamps();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('tenant_users');
}
};
45 changes: 45 additions & 0 deletions app-modules/tenant/src/Models/Concerns/InteractsWithTenants.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace He4rt\Tenant\Models\Concerns;

use Filament\Panel;
use He4rt\Tenant\Models\Tenant;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\Pivot;
use Illuminate\Support\Collection;

trait InteractsWithTenants
{
/**
* @return BelongsToMany<Tenant, $this, Pivot>
*/
public function tenants(): BelongsToMany
{
return $this->belongsToMany(Tenant::class, 'tenant_users');
}

public function canAccessTenant(Model $tenant): bool
{
return $this->tenants()->whereKey($tenant)->exists();
}

/**
* @return array<Tenant> | Collection
*/
public function getTenants(Panel $panel): array|Collection
{
return $this->tenants;
}

/**
* @return HasMany<Tenant, $this>
*/
public function ownedTenants(): HasMany
{
return $this->hasMany(Tenant::class, 'owner_id');
}
}
10 changes: 10 additions & 0 deletions app-modules/tenant/src/Models/Tenant.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Database\Eloquent\Relations\Pivot;
use Illuminate\Database\Eloquent\SoftDeletes;

class Tenant extends Model
Expand All @@ -34,6 +36,14 @@ public function owner(): BelongsTo
return $this->belongsTo(User::class, 'owner_id');
}

/**
* @return BelongsToMany<User, $this, Pivot>
*/
public function members(): BelongsToMany
{
return $this->belongsToMany(User::class, 'tenant_users');
}

/**
* @return MorphMany<Provider, $this>
*/
Expand Down
Loading