-
-
Notifications
You must be signed in to change notification settings - Fork 313
Add middleware system #3686
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
Open
tadhgboyle
wants to merge
7
commits into
develop
Choose a base branch
from
tb/middleware
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add middleware system #3686
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| <?php | ||
|
|
||
| abstract class AbstractMiddleware | ||
| { | ||
| public MiddlewareType $type = MiddlewareType::Global; | ||
|
|
||
| public array $exemptRoutes = []; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| <?php | ||
|
|
||
| use DI\Container; | ||
| use Symfony\Component\HttpFoundation\Request; | ||
|
|
||
| class MiddlewareHandler extends Instanceable | ||
| { | ||
| /** | ||
| * @var class-string<AbstractMiddleware>[] | ||
| */ | ||
| private array $middleware = []; | ||
|
|
||
| /** | ||
| * Register a middleware class. | ||
| * | ||
| * @param class-string<AbstractMiddleware> $class | ||
| */ | ||
| public function register(string $class): void | ||
| { | ||
| $this->middleware[] = $class; | ||
| } | ||
|
|
||
| /** | ||
| * Get all registered middleware classes. | ||
| * | ||
| * @return class-string<AbstractMiddleware>[] | ||
| */ | ||
| public function getMiddleware(): array | ||
| { | ||
| return $this->middleware; | ||
| } | ||
|
|
||
| public function call(MiddlewareType $type, Container $container) | ||
| { | ||
| $middlewareClasses = $this->getMiddleware(); | ||
|
|
||
| foreach ($middlewareClasses as $class) { | ||
| $middleware = $container->get($class); | ||
| $request = $container->get(Request::class); | ||
|
|
||
| foreach ($middleware->exemptRoutes as $exemptedRoute) { | ||
| if (str_starts_with($request->get('route'), $exemptedRoute)) { | ||
| continue 2; // Skip this middleware if the route is exempted | ||
| } | ||
| } | ||
|
|
||
| if ($middleware->type === $type) { | ||
| $container->call([$middleware, 'handle']); | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| <?php | ||
|
|
||
| enum MiddlewareType | ||
| { | ||
| case Global; | ||
| case Frontend; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
modules/Core/classes/Middleware/Frontend/EnsureUserIntegrationsLinkedMiddleware.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| <?php | ||
|
|
||
| class EnsureUserIntegrationsLinkedMiddleware extends AbstractMiddleware | ||
| { | ||
| public MiddlewareType $type = MiddlewareType::Frontend; | ||
|
|
||
| public array $exemptRoutes = [ | ||
| '/user/connections', | ||
| '/oauth', | ||
| '/user/settings', | ||
| ]; | ||
|
|
||
| public function handle(User $user, Language $language): void | ||
| { | ||
| if (!$user->isLoggedIn()) { | ||
| return; | ||
| } | ||
|
|
||
| // Check if any integrations is required before user can continue | ||
| foreach (Integrations::getInstance()->getEnabledIntegrations() as $integration) { | ||
| if ($integration->data()->required && $integration->allowLinking()) { | ||
| $integrationUser = $user->getIntegration($integration->getName()); | ||
| if ($integrationUser === null || !$integrationUser->isVerified()) { | ||
| Session::flash('connections_error', $language->get('user', 'integration_required_to_continue')); | ||
| Redirect::to(URL::build('/user/connections')); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
20 changes: 20 additions & 0 deletions
20
modules/Core/classes/Middleware/Frontend/GlobalWarningsMiddleware.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| <?php | ||
|
|
||
| class GlobalWarningsMiddleware extends AbstractMiddleware | ||
| { | ||
| public MiddlewareType $type = MiddlewareType::Frontend; | ||
|
|
||
| public function handle(User $user, Language $language, TemplateBase $template): void | ||
| { | ||
| $warnings = DB::getInstance()->query('SELECT * FROM nl2_infractions WHERE punished = ? AND revoked = 0 AND acknowledged = 0', [$user->data()->id])->results(); | ||
| foreach ($warnings as $warning) { | ||
| $template->getEngine()->addVariables([ | ||
| 'GLOBAL_WARNING_TITLE' => $language->get('user', 'you_have_received_a_warning'), | ||
| 'GLOBAL_WARNING_REASON' => Output::getClean($warning->reason), | ||
| 'GLOBAL_WARNING_ACKNOWLEDGE' => $language->get('user', 'acknowledge'), | ||
| 'GLOBAL_WARNING_ACKNOWLEDGE_LINK' => URL::build('/user/acknowledge/' . urlencode($warning->id)), | ||
| ]); | ||
| break; | ||
| } | ||
| } | ||
| } |
23 changes: 23 additions & 0 deletions
23
modules/Core/classes/Middleware/Global/BannedUserMiddleware.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * Banned User middleware hook. | ||
| * Handles user bans and IP bans enforcement. | ||
| * | ||
| * @package NamelessMC\Hooks | ||
| * @author Aberdeener | ||
| * @version 2.3.0 | ||
| * @license MIT | ||
| */ | ||
| class BannedUserMiddleware extends AbstractMiddleware | ||
| { | ||
| public function handle(User $user, Language $language): void | ||
| { | ||
| if (($user->isLoggedIn() && $user->data()->isbanned) || DB::getInstance()->get('ip_bans', ['ip', HttpUtils::getRemoteAddress()])->exists()) { | ||
| $user->logout(); | ||
|
|
||
| Session::flash('home_error', $language->get('user', 'you_have_been_banned')); | ||
| Redirect::to(URL::build('/')); | ||
| } | ||
| } | ||
| } |
40 changes: 40 additions & 0 deletions
40
modules/Core/classes/Middleware/Global/MaintenanceModeMiddleware.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * Maintenance Mode middleware hook. | ||
| * Redirects non-admin users when maintenance mode is enabled. | ||
| * | ||
| * @package NamelessMC\Hooks | ||
| * @author Aberdeener | ||
| * @version 2.3.0 | ||
| * @license MIT | ||
| */ | ||
| class MaintenanceModeMiddleware extends AbstractMiddleware | ||
| { | ||
| public array $exemptRoutes = [ | ||
| '/maintenance', | ||
| '/login', | ||
| '/forgot_password', | ||
| '/api', | ||
| '/queries', | ||
| '/oauth', | ||
| '/store/listener', | ||
| ]; | ||
|
|
||
| public function handle(User $user): void | ||
| { | ||
| // Check if maintenance mode is enabled | ||
| if (!Settings::get('maintenance')) { | ||
| return; | ||
| } | ||
|
|
||
| // Allow admin users to bypass maintenance mode | ||
| if ($user->isLoggedIn() && $user->canViewStaffCP()) { | ||
| // Display notice to admin stating maintenance mode is enabled | ||
| define('BYPASS_MAINTENANCE', true); | ||
| return; | ||
| } | ||
|
|
||
| Redirect::to(URL::build('/maintenance')); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should not send users to maintenance page thats just annoying, And alot of users like just to refresh site to see if maintenance is complete Also makes uses lose any details they might have in the URL |
||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
User should only be able to view /user/settings IF they are viewing enable_tfa