Skip to content

Commit

Permalink
Develop the package
Browse files Browse the repository at this point in the history
  • Loading branch information
antonioribeiro committed Feb 11, 2023
1 parent 7d9b386 commit 0791a72
Show file tree
Hide file tree
Showing 13 changed files with 230 additions and 195 deletions.
37 changes: 2 additions & 35 deletions src/Http/Controllers/TwillFirewallController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
use Illuminate\Support\Facades\URL;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Redirect;
use A17\Twill\Http\Controllers\Admin\ModuleController;
use A17\TwillFirewall\Models\TwillFirewall;
use A17\Twill\Http\Controllers\Admin\ModuleController;
use A17\TwillFirewall\Repositories\TwillFirewallRepository;
use A17\TwillFirewall\Support\Facades\TwillFirewall as TwillFirewallFacade;

Expand Down Expand Up @@ -57,7 +57,7 @@ class TwillFirewallController extends ModuleController
*/
public function index($parentModuleId = null)
{
$this->generateDomains();
app(TwillFirewallRepository::class)->generateDomains();

$this->setIndexOptions();

Expand All @@ -69,39 +69,6 @@ protected function getViewPrefix(): string|null
return 'twill-firewall::admin';
}

public function generateDomains(): void
{
if (DB::table('twill_firewall')->count() !== 0) {
return;
}

$appDomain = TwillFirewallFacade::getDomain(config('app.url'));

$currentDomain = TwillFirewallFacade::getDomain(URL::current());

/** @phpstan-ignore-next-line */
app(TwillFirewallRepository::class)->create([
'domain' => '*',
'published' => false,
]);

if (filled($currentDomain)) {
/** @phpstan-ignore-next-line */
app(TwillFirewallRepository::class)->create([
'domain' => $currentDomain,
'published' => false,
]);
}

if (filled($appDomain) && $appDomain !== $currentDomain) {
/** @phpstan-ignore-next-line */
app(TwillFirewallRepository::class)->create([
'domain' => $appDomain,
'published' => false,
]);
}
}

public function setIndexOptions(): void
{
$this->indexOptions = ['create' => !TwillFirewallFacade::allDomainsEnabled()];
Expand Down
42 changes: 0 additions & 42 deletions src/Models/Behaviors/Encrypt.php

This file was deleted.

19 changes: 15 additions & 4 deletions src/Models/TwillFirewall.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use A17\Twill\Models\Behaviors\HasRevisions;
use A17\TwillFirewall\Services\Helpers;
use Illuminate\Database\Eloquent\Relations\HasMany;
use A17\TwillFirewall\Models\Behaviors\Encrypt;
use A17\TwillFirewall\Support\Facades\TwillFirewall as TwillFirewallFacade;

/**
Expand All @@ -17,11 +16,10 @@
class TwillFirewall extends Model
{
use HasRevisions;
use Encrypt;

protected $table = 'twill_firewall';

protected $fillable = ['published', 'domain', 'allow', 'block', 'redirect_to', 'allow_laravel_login', 'allow_twill_login'];
protected $fillable = ['published', 'domain', 'allow', 'block', 'redirect_to', 'allow_laravel_login', 'allow_twill_login', 'strategy'];

protected $appends = ['domain_string', 'status', 'from_dot_env'];

Expand All @@ -32,7 +30,9 @@ public function revisions(): HasMany

public function getConfiguredAttribute(): bool
{
return filled($this->allow) || filled($this->block);
return TwillFirewallFacade::hasDotEnv() ||
$this->strategy === 'allow' && filled($this->allow) ||
$this->strategy === 'block' && filled($this->block);
}

public function getStatusAttribute(): string
Expand All @@ -52,4 +52,15 @@ public function getFromDotEnvAttribute(): string
{
return TwillFirewallFacade::hasDotEnv() ? 'yes' : 'no';
}

public function getDomainStringAttribute(): string|null
{
$domain = $this->domain;

if ($domain === '*') {
return '* (all domains)';
}

return $domain;
}
}
36 changes: 36 additions & 0 deletions src/Repositories/TwillFirewallRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
use A17\Twill\Repositories\ModuleRepository;
use A17\Twill\Repositories\Behaviors\HandleRevisions;
use A17\TwillFirewall\Models\TwillFirewall;
use A17\TwillFirewall\Support\Facades\TwillFirewall as TwillFirewallFacade;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\URL;

/**
* @method \Illuminate\Database\Eloquent\Builder published()
Expand All @@ -17,4 +20,37 @@ public function __construct(TwillFirewall $model)
{
$this->model = $model;
}

public function generateDomains(): void
{
if (DB::table('twill_firewall')->count() !== 0) {
return;
}

$appDomain = TwillFirewallFacade::getDomain(config('app.url'));

$currentDomain = TwillFirewallFacade::getDomain(URL::current());

/** @phpstan-ignore-next-line */
app(TwillFirewallRepository::class)->create([
'domain' => '*',
'published' => false,
]);

if (filled($currentDomain)) {
/** @phpstan-ignore-next-line */
app(TwillFirewallRepository::class)->create([
'domain' => $currentDomain,
'published' => false,
]);
}

if (filled($appDomain) && $appDomain !== $currentDomain) {
/** @phpstan-ignore-next-line */
app(TwillFirewallRepository::class)->create([
'domain' => $appDomain,
'published' => false,
]);
}
}
}
2 changes: 1 addition & 1 deletion src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use A17\Twill\TwillPackageServiceProvider;
use A17\TwillFirewall\Http\Middleware;
use A17\TwillFirewall\Services\Helpers;
use A17\TwillFirewall\Support\TwillFirewall;
use A17\TwillFirewall\Services\TwillFirewall;

class ServiceProvider extends TwillPackageServiceProvider
{
Expand Down
2 changes: 1 addition & 1 deletion src/Services/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace A17\TwillFirewall\Services;

use A17\TwillFirewall\Support\TwillFirewall;
use A17\TwillFirewall\Services\TwillFirewall;

class Helpers
{
Expand Down
77 changes: 77 additions & 0 deletions src/Services/Middleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace A17\TwillFirewall\Services;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\RateLimiter;
use A17\TwillFirewall\Support\Facades\TwillFirewall;

trait Middleware
{
public function middleware(Request $request): mixed
{
if (!$this->enabled()) {
return null;
}

$checkAuth = fn() => $this->checkAuth($request);

$rateLimitingKey = 'firewall:' . $this->readFromDatabase('allow');

$response = RateLimiter::attempt(
$rateLimitingKey,
$perMinute = $this->config('rate-limiting.attemps-per-minute', 5),
$checkAuth,
);

if (RateLimiter::tooManyAttempts($rateLimitingKey, $perMinute)) {
abort(429, 'Too many attempts. Please wait one minute and try again.');
}

if ($response === null) {
RateLimiter::clear($rateLimitingKey);
}

return $response === true ? null : $response;
}

public function checkAuth(Request $request): mixed
{
if ($this->loggedInWithAuthGuard()) {
return true;
}

return TwillFirewall::checkAuth($request, [
'allow' => $this->allow(),
'block' => $this->block(),
'guards' => $this->getAuthGuards(),
'routes' => $this->config('routes'),
]);
}

public function loggedInWithAuthGuard(): bool
{
foreach ($this->getAuthGuards() as $guard) {
if (auth($guard)->check()) {
return true;
}
}

return false;
}

public function getAuthGuards(): array
{
$guards = [];

foreach ($this->config('database-login', []) as $name => $guard) {
$enabled = $this->hasDotEnv() ? $guard['enabled'] ?? false : $this->readFromDatabase("allow_{$name}_login");

if ($enabled) {
$guards[] = $guard['guard'];
}
}

return $guards;
}
}
Loading

0 comments on commit 0791a72

Please sign in to comment.