Skip to content

Commit

Permalink
Added enabled function, to enable / disable auth routes programmatically
Browse files Browse the repository at this point in the history
  • Loading branch information
nahime0 committed Apr 2, 2023
1 parent 5c0fe4b commit 34b27c8
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 8 deletions.
26 changes: 18 additions & 8 deletions src/Authenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
* It will be available during the lifecycle of the request.
*
* Internal params:
* @property bool $enabled Whether the auth is enabled
* @property bool $registration_enabled Whether registration is enabled
* @property bool $forgot_password_enabled Whether forgot password is enabled
* @property bool $email_verification_enabled Whether email verification is enabled
* @property bool $user_profile_enabled Whether the user profile is enabled
* @property string $dashboard The name of the dashboard route
* @property string $registration_enabled Whether registration is enabled
* @property string $forgot_password_enabled Whether forgot password is enabled
* @property string $email_verification_enabled Whether email verification is enabled
* @property string $user_profile_enabled Whether the user profile is enabled
* @property string $template_confirm_password The name of the confirm password template
* @property string $template_forgot_password The name of the forgot password template
* @property string $template_login The name of the login template
Expand Down Expand Up @@ -65,11 +66,12 @@ class Authenticator
public function __construct(private readonly string $name)
{
$this->parameters = Collection::make([
'dashboard' => null,
'enabled' => true,
'registration_enabled' => true,
'forgot_password_enabled' => true,
'email_verification_enabled' => true,
'user_profile_enabled' => true,
'dashboard' => null,
'template_confirm_password' => 'inside_auth::auth.confirm-password',
'template_forgot_password' => 'inside_auth::auth.forgot-password',
'template_login' => 'inside_auth::auth.login',
Expand Down Expand Up @@ -129,11 +131,11 @@ private function set(string $key, mixed $value): static
###############################################

/**
* Set the dashboard route
* Enable/Disable the auth
*/
public function withDashboard(string $dashboard): static
public function enabled(bool $enabled = true): static
{
return $this->set('dashboard', $dashboard);
return $this->set('enabled', $enabled);
}

/**
Expand Down Expand Up @@ -176,6 +178,14 @@ public function withoutUserProfile(bool $without = true): static
return $this->set('user_profile_enabled', !$without);
}

/**
* Set the dashboard route
*/
public function withDashboard(string $dashboard): static
{
return $this->set('dashboard', $dashboard);
}

/**
* Set the confirm password template
*/
Expand Down
21 changes: 21 additions & 0 deletions src/Http/Middleware/EnsureAuthIsEnabled.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Illegal\InsideAuth\Http\Middleware;

use Closure;
use Illegal\InsideAuth\InsideAuth;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

class EnsureAuthIsEnabled
{
public function handle(Request $request, Closure $next): Response
{
if(!InsideAuth::current()->enabled) {
abort(404);
}

return $next($request);
}

}
7 changes: 7 additions & 0 deletions src/Registrators/MiddlewareRegistrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illegal\InsideAuth\Authenticator;
use Illegal\InsideAuth\Contracts\RegistratorInterface;
use Illegal\InsideAuth\Http\Middleware\Authenticate;
use Illegal\InsideAuth\Http\Middleware\EnsureAuthIsEnabled;
use Illegal\InsideAuth\Http\Middleware\EnsureEmailIsVerified;
use Illegal\InsideAuth\Http\Middleware\InjectIntoApplication;
use Illegal\InsideAuth\Http\Middleware\RedirectIfAuthenticated;
Expand All @@ -24,6 +25,7 @@
* @property string $web
* @property string $authenticated
* @property string $ensure_verified
* @property string $ensure_enabled
* @property string $inject
* @property string $redirect_authenticated
*/
Expand All @@ -46,13 +48,15 @@ public function __construct(private readonly Authenticator $authenticator, strin
'web' => $this->authenticator->name() . '-web',
'authenticated' => $this->authenticator->name() . '-authenticated',
'ensure_verified' => $this->authenticator->name() . '-ensure-email-is-verified',
'ensure_enabled' => $this->authenticator->name() . '-ensure-auth-is-enabled',
'inject' => $this->authenticator->name() . '-inject',
'redirect_authenticated' => $this->authenticator->name() . '-redirect-if-authenticated',
]);

$this->authenticator->merge($this->parameters->except([
'authenticated',
'ensure_verified',
'ensure_enabled',
'inject',
'redirect_authenticated'
])->mapWithKeys(fn($value, $key) => [$prefix . '_' . $key => $value]));
Expand Down Expand Up @@ -80,6 +84,7 @@ public function boot(): void
*/
Route::aliasMiddleware($this->authenticated, Authenticate::class);
Route::aliasMiddleware($this->ensure_verified, EnsureEmailIsVerified::class);
Route::aliasMiddleware($this->ensure_enabled, EnsureAuthIsEnabled::class);
Route::aliasMiddleware($this->redirect_authenticated, RedirectIfAuthenticated::class);
Route::aliasMiddleware($this->inject, InjectIntoApplication::class);

Expand All @@ -100,6 +105,7 @@ public function boot(): void
*/
Route::middlewareGroup($this->guest, [
$this->inject . ':' . $this->authenticator->name(),
$this->ensure_enabled,
$this->redirect_authenticated
]);

Expand All @@ -109,6 +115,7 @@ public function boot(): void
*/
Route::middlewareGroup($this->logged_in, [
$this->inject . ':' . $this->authenticator->name(),
$this->ensure_enabled,
$this->authenticated . ':' . $this->authenticator->route_login . ',' . $this->authenticator->security_guard
]);

Expand Down

0 comments on commit 34b27c8

Please sign in to comment.