From b3c6265003cff1208a6f0d6a02a517c1be359daa Mon Sep 17 00:00:00 2001 From: Ian Morland Date: Sun, 6 Nov 2022 17:38:56 +0000 Subject: [PATCH 1/4] Allow additional login params, dispatch 'LoggingIn' event --- .../js/src/forum/components/LogInModal.tsx | 17 ++++++++++------ .../src/Forum/Controller/LogInController.php | 5 +++++ framework/core/src/User/Event/LoggingIn.php | 20 +++++++++++++++++++ 3 files changed, 36 insertions(+), 6 deletions(-) create mode 100644 framework/core/src/User/Event/LoggingIn.php diff --git a/framework/core/js/src/forum/components/LogInModal.tsx b/framework/core/js/src/forum/components/LogInModal.tsx index 556f78be9d..2d790cb12c 100644 --- a/framework/core/js/src/forum/components/LogInModal.tsx +++ b/framework/core/js/src/forum/components/LogInModal.tsx @@ -9,6 +9,7 @@ import ItemList from '../../common/utils/ItemList'; import Stream from '../../common/utils/Stream'; import type Mithril from 'mithril'; import RequestError from '../../common/utils/RequestError'; +import { LoginParams } from '../../common/Session'; export interface ILoginModalAttrs extends IInternalModalAttrs { identification?: string; @@ -172,13 +173,17 @@ export default class LogInModal window.location.reload(), this.loaded.bind(this)); + } + + loginParams(): LoginParams { + const data = { + identification: this.identification(), + password: this.password(), + remember: this.remember(), + }; - app.session - .login({ identification, password, remember }, { errorHandler: this.onerror.bind(this) }) - .then(() => window.location.reload(), this.loaded.bind(this)); + return data; } onerror(error: RequestError) { diff --git a/framework/core/src/Forum/Controller/LogInController.php b/framework/core/src/Forum/Controller/LogInController.php index 30212d3b87..f41924985c 100644 --- a/framework/core/src/Forum/Controller/LogInController.php +++ b/framework/core/src/Forum/Controller/LogInController.php @@ -15,6 +15,7 @@ use Flarum\Http\Rememberer; use Flarum\Http\SessionAuthenticator; use Flarum\User\Event\LoggedIn; +use Flarum\User\Event\LoggingIn; use Flarum\User\UserRepository; use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Support\Arr; @@ -72,6 +73,10 @@ public function handle(Request $request): ResponseInterface $body = $request->getParsedBody(); $params = Arr::only($body, ['identification', 'password', 'remember']); + // Dispatch an event with all the provided params, except the password provided. + // This allows extensions to add custom validation rules, etc and check for their existence in the payload. + $this->events->dispatch(new LoggingIn(Arr::except($body, 'password'))); + $response = $this->apiClient->withParentRequest($request)->withBody($params)->post('/token'); if ($response->getStatusCode() === 200) { diff --git a/framework/core/src/User/Event/LoggingIn.php b/framework/core/src/User/Event/LoggingIn.php new file mode 100644 index 0000000000..4fb252840a --- /dev/null +++ b/framework/core/src/User/Event/LoggingIn.php @@ -0,0 +1,20 @@ +params = $params; + } +} From cff1b6ee02d0f7caa683164eb801cabe83438980 Mon Sep 17 00:00:00 2001 From: Ian Morland <16573496+imorland@users.noreply.github.com> Date: Sun, 6 Nov 2022 18:09:18 +0000 Subject: [PATCH 2/4] Update framework/core/js/src/forum/components/LogInModal.tsx Co-authored-by: David Wheatley --- framework/core/js/src/forum/components/LogInModal.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/framework/core/js/src/forum/components/LogInModal.tsx b/framework/core/js/src/forum/components/LogInModal.tsx index 2d790cb12c..6305f37dae 100644 --- a/framework/core/js/src/forum/components/LogInModal.tsx +++ b/framework/core/js/src/forum/components/LogInModal.tsx @@ -9,7 +9,7 @@ import ItemList from '../../common/utils/ItemList'; import Stream from '../../common/utils/Stream'; import type Mithril from 'mithril'; import RequestError from '../../common/utils/RequestError'; -import { LoginParams } from '../../common/Session'; +import type { LoginParams } from '../../common/Session'; export interface ILoginModalAttrs extends IInternalModalAttrs { identification?: string; From 33f2ddf0e3e6c7b4026ecb3095e4432233e23b1f Mon Sep 17 00:00:00 2001 From: Ian Morland Date: Sun, 6 Nov 2022 19:56:10 +0000 Subject: [PATCH 3/4] Introduce 'LogInValidator' --- .../src/Forum/Controller/LogInController.php | 14 +++++++++---- framework/core/src/Forum/LogInValidator.php | 13 ++++++++++++ framework/core/src/User/Event/LoggingIn.php | 20 ------------------- 3 files changed, 23 insertions(+), 24 deletions(-) create mode 100644 framework/core/src/Forum/LogInValidator.php delete mode 100644 framework/core/src/User/Event/LoggingIn.php diff --git a/framework/core/src/Forum/Controller/LogInController.php b/framework/core/src/Forum/Controller/LogInController.php index f41924985c..b14c66d0b1 100644 --- a/framework/core/src/Forum/Controller/LogInController.php +++ b/framework/core/src/Forum/Controller/LogInController.php @@ -10,6 +10,7 @@ namespace Flarum\Forum\Controller; use Flarum\Api\Client; +use Flarum\Forum\LogInValidator; use Flarum\Http\AccessToken; use Flarum\Http\RememberAccessToken; use Flarum\Http\Rememberer; @@ -50,19 +51,26 @@ class LogInController implements RequestHandlerInterface */ protected $rememberer; + /** + * @var LogInValidator + */ + protected $validator; + /** * @param \Flarum\User\UserRepository $users * @param Client $apiClient * @param SessionAuthenticator $authenticator * @param Rememberer $rememberer + * @param LogInValidator $validator */ - public function __construct(UserRepository $users, Client $apiClient, SessionAuthenticator $authenticator, Dispatcher $events, Rememberer $rememberer) + public function __construct(UserRepository $users, Client $apiClient, SessionAuthenticator $authenticator, Dispatcher $events, Rememberer $rememberer, LogInValidator $validator) { $this->users = $users; $this->apiClient = $apiClient; $this->authenticator = $authenticator; $this->events = $events; $this->rememberer = $rememberer; + $this->validator = $validator; } /** @@ -73,9 +81,7 @@ public function handle(Request $request): ResponseInterface $body = $request->getParsedBody(); $params = Arr::only($body, ['identification', 'password', 'remember']); - // Dispatch an event with all the provided params, except the password provided. - // This allows extensions to add custom validation rules, etc and check for their existence in the payload. - $this->events->dispatch(new LoggingIn(Arr::except($body, 'password'))); + $this->validator->assertValid($body); $response = $this->apiClient->withParentRequest($request)->withBody($params)->post('/token'); diff --git a/framework/core/src/Forum/LogInValidator.php b/framework/core/src/Forum/LogInValidator.php new file mode 100644 index 0000000000..cdbc2a3f3f --- /dev/null +++ b/framework/core/src/Forum/LogInValidator.php @@ -0,0 +1,13 @@ +params = $params; - } -} From 461a8d3a721827fbc0063a2372d8d76615f84b96 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Sun, 6 Nov 2022 19:56:31 +0000 Subject: [PATCH 4/4] Apply fixes from StyleCI --- framework/core/src/Forum/Controller/LogInController.php | 1 - framework/core/src/Forum/LogInValidator.php | 7 +++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/framework/core/src/Forum/Controller/LogInController.php b/framework/core/src/Forum/Controller/LogInController.php index b14c66d0b1..86de9ceeb5 100644 --- a/framework/core/src/Forum/Controller/LogInController.php +++ b/framework/core/src/Forum/Controller/LogInController.php @@ -16,7 +16,6 @@ use Flarum\Http\Rememberer; use Flarum\Http\SessionAuthenticator; use Flarum\User\Event\LoggedIn; -use Flarum\User\Event\LoggingIn; use Flarum\User\UserRepository; use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Support\Arr; diff --git a/framework/core/src/Forum/LogInValidator.php b/framework/core/src/Forum/LogInValidator.php index cdbc2a3f3f..33035ae6a4 100644 --- a/framework/core/src/Forum/LogInValidator.php +++ b/framework/core/src/Forum/LogInValidator.php @@ -1,5 +1,12 @@