Skip to content

Commit

Permalink
feat: allow wildcard route exclusion
Browse files Browse the repository at this point in the history
  • Loading branch information
SychO9 committed Feb 4, 2024
1 parent a6d628c commit 0f7b202
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 56 deletions.
3 changes: 0 additions & 3 deletions extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,6 @@
(new Extend\View())
->namespace('sycho-private-facade', __DIR__.'/views'),

(new Extend\ServiceProvider())
->register(RouteExclusionProvider::class),

(new Extend\Settings())
->default('sycho-private-facade.header_layout', 'show_only_logo')
->default('sycho-private-facade.primary_color_bg', true)
Expand Down
10 changes: 3 additions & 7 deletions src/Extend/FacadeExclusions.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Flarum\Extend\ExtenderInterface;
use Flarum\Extension\Extension;
use Illuminate\Contracts\Container\Container;
use SychO\PrivateFacade\PrivateFacadeMiddleware;

class FacadeExclusions implements ExtenderInterface
{
Expand All @@ -31,12 +32,7 @@ public function extend(Container $container, Extension $extension = null)
return;
}

$container->extend('sycho-private-facade.backend-route-exclusions', function ($excludedRoutes) {
return array_merge($excludedRoutes, $this->excludedBackendRoutes);
});

$container->extend('sycho-private-facade.frontend-route-exclusions', function ($excludedRoutes) {
return array_merge($excludedRoutes, $this->excludedFrontendRoutes);
});
PrivateFacadeMiddleware::$backendRouteExclusions = array_merge(PrivateFacadeMiddleware::$backendRouteExclusions, $this->excludedBackendRoutes);
PrivateFacadeMiddleware::$frontendRouteExclusions = array_merge(PrivateFacadeMiddleware::$frontendRouteExclusions, $this->excludedFrontendRoutes);
}
}
28 changes: 28 additions & 0 deletions src/Helper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace SychO\PrivateFacade;

class Helper
{
public static function itemsInclude(array $items, string $value): bool
{
$included = false;

foreach ($items as $item) {
// allow for wildcards
if (strpos($item, '*') !== false) {
$item = str_replace('*', '.*', preg_quote($item, '/'));

if (preg_match('/' . $item . '/', $value)) {
$included = true;
break;
}
} elseif ($value === $item) {
$included = true;
break;
}
}

return $included;
}
}
36 changes: 17 additions & 19 deletions src/PrivateFacadeMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,19 @@

class PrivateFacadeMiddleware implements MiddlewareInterface
{
public static $backendRouteExclusions = [
'login', 'register', 'sycho-private-facade.login', 'sycho-private-facade.signup',
'resetPassword', 'confirmEmail', 'savePassword', 'confirmEmail.submit',
// FoF-OAuth
'auth.twitter', 'fof-oauth',
// PWA
'askvortsov-pwa.*',
];

public static $frontendRouteExclusions = [
'sycho-private-facade.login', 'sycho-private-facade.signup',
];

/**
* @var SettingsRepositoryInterface
*/
Expand All @@ -39,25 +52,10 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
$extensionExcludedRoutes = self::getBackendRouteExclusions($userExcludedRoutes);
$extensionExcludedUrls = explode(',', str_replace(' ', '', $userExcludedUrls));

$excludedRoute = in_array($request->getAttribute('routeName'), $extensionExcludedRoutes, true);
$excludedRoute = Helper::itemsInclude($extensionExcludedRoutes, $request->getAttribute('routeName'));

if (! $excludedRoute) {
$currentUri = $request->getUri()->getPath();

foreach ($extensionExcludedUrls as $url) {
// allow for wildcards
if (strpos($url, '*') !== false) {
$url = str_replace('*', '.*', preg_quote($url, '/'));

if (preg_match('/' . $url . '/', $currentUri)) {
$excludedRoute = true;
break;
}
} elseif ($currentUri === $url) {
$excludedRoute = true;
break;
}
}
$excludedRoute = Helper::itemsInclude($extensionExcludedUrls, $request->getUri()->getPath());
}

$isPrivateFacade = in_array(
Expand Down Expand Up @@ -89,12 +87,12 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface

public static function getBackendRouteExclusions(?string $userExcludedRoutes): array
{
return self::getRouteExclusions($userExcludedRoutes, resolve('sycho-private-facade.backend-route-exclusions'));
return self::getRouteExclusions($userExcludedRoutes, self::$backendRouteExclusions);
}

public static function getFrontendRouteExclusions(?string $userExcludedRoutes): array
{
return self::getRouteExclusions($userExcludedRoutes, resolve('sycho-private-facade.frontend-route-exclusions'));
return self::getRouteExclusions($userExcludedRoutes, self::$frontendRouteExclusions);
}

protected static function getRouteExclusions(?string $userExcludedRoutes, array $extensionExcludedRoutes): array
Expand Down
27 changes: 0 additions & 27 deletions src/Providers/RouteExclusionProvider.php

This file was deleted.

0 comments on commit 0f7b202

Please sign in to comment.