Skip to content

Commit 2811a0f

Browse files
committed
refactor: moved login API to controller
1 parent 7cb8c2c commit 2811a0f

File tree

9 files changed

+136
-41
lines changed

9 files changed

+136
-41
lines changed

phpmyfaq/.htaccess

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,6 @@ RewriteRule ^(.*)$ index.php?action=ask [L,QSA]
6060
RewriteCond %{REQUEST_URI} /open-questions\.html$ [NC]
6161
RewriteRule ^(.*)$ index.php?action=open-questions [L,QSA]
6262

63-
# the help page
64-
RewriteCond %{REQUEST_URI} /help\.html$ [NC]
65-
RewriteRule ^(.*)$ index.php?action=help [L,QSA]
66-
6763
# the contact page
6864
RewriteCond %{REQUEST_URI} /contact\.html$ [NC]
6965
RewriteRule ^(.*)$ index.php?action=contact [L,QSA]
@@ -155,7 +151,7 @@ RewriteRule api/v2.0/open-questions api/index.php
155151
RewriteRule api/v2.0/comments/([0-9]+) api/index.php
156152
RewriteRule api/v2.0/attachments/([0-9]+) api/index.php
157153
RewriteRule api/v2.0/news api/index.php
158-
RewriteRule api/v2.0/login api.php?action=login [L,QSA]
154+
RewriteRule api/v2.0/login api/index.php
159155
RewriteRule api/v2.0/faqs/([0-9]+) api.php?action=faqs&categoryId=$1 [L,QSA]
160156
RewriteRule api/v2.0/faqs/popular api.php?action=faqs&filter=popular [L,QSA]
161157
RewriteRule api/v2.0/faqs/latest api.php?action=faqs&filter=latest [L,QSA]
@@ -176,7 +172,7 @@ RewriteRule api/v2.1/open-questions api/index.php
176172
RewriteRule api/v2.1/comments/([0-9]+) api/index.php
177173
RewriteRule api/v2.1/attachments/([0-9]+) api/index.php
178174
RewriteRule api/v2.1/news api/index.php
179-
RewriteRule api/v2.1/login api.php?action=login [L,QSA]
175+
RewriteRule api/v2.1/login api/index.php
180176
RewriteRule api/v2.1/faqs/([0-9]+) api.php?action=faqs&categoryId=$1 [L,QSA]
181177
RewriteRule api/v2.1/faqs/popular api.php?action=faqs&filter=popular [L,QSA]
182178
RewriteRule api/v2.1/faqs/latest api.php?action=faqs&filter=latest [L,QSA]
@@ -203,7 +199,7 @@ RewriteRule api/v2.2/open-questions api/index.php
203199
RewriteRule api/v2.2/comments/([0-9]+) api/index.php
204200
RewriteRule api/v2.2/attachments/([0-9]+) api/index.php
205201
RewriteRule api/v2.2/news api/index.php
206-
RewriteRule api/v2.2/login api.php?action=login [L,QSA]
202+
RewriteRule api/v2.2/login api/index.php
207203
RewriteRule api/v2.2/faqs/([0-9]+) api.php?action=faqs&categoryId=$1 [L,QSA]
208204
RewriteRule api/v2.2/faqs/popular api.php?action=faqs&filter=popular [L,QSA]
209205
RewriteRule api/v2.2/faqs/latest api.php?action=faqs&filter=latest [L,QSA]
@@ -221,6 +217,8 @@ RewriteRule api/v2.3/categories api/index.php
221217
RewriteRule api/v2.3/comments/([0-9]+) api/index.php
222218
RewriteRule api/v2.3/groups api/index.php
223219
RewriteRule api/v2.3/language api/index.php
220+
RewriteRule api/v2.3/login api/index.php
221+
RewriteRule api/v2.3/news api/index.php
224222
RewriteRule api/v2.3/open-questions api/index.php
225223
RewriteRule api/v2.3/search api/index.php
226224
RewriteRule api/v2.3/searches/popular api/index.php

phpmyfaq/api.php

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -221,15 +221,6 @@
221221
$response->setData($result);
222222
break;
223223

224-
case 'news':
225-
$news = new News($faqConfig);
226-
$result = $news->getLatestData(false, true, true);
227-
if ((is_countable($result) ? count($result) : 0) === 0) {
228-
$response->setStatusCode(Response::HTTP_NOT_FOUND);
229-
}
230-
$response->setData($result);
231-
break;
232-
233224
case 'faqs':
234225
$filter = Filter::filterInput(INPUT_GET, 'filter', FILTER_SANITIZE_SPECIAL_CHARS);
235226
$faq = new Faq($faqConfig);
@@ -401,30 +392,6 @@
401392
$response->setData($result);
402393
break;
403394

404-
case 'login':
405-
$postData = json_decode(file_get_contents('php://input'), true, 512, JSON_THROW_ON_ERROR);
406-
$faqUsername = Filter::filterVar($postData['username'], FILTER_SANITIZE_SPECIAL_CHARS);
407-
$faqPassword = Filter::filterVar($postData['password'], FILTER_SANITIZE_SPECIAL_CHARS);
408-
409-
$user = new CurrentUser($faqConfig);
410-
$userAuth = new UserAuthentication($faqConfig, $user);
411-
try {
412-
$user = $userAuth->authenticate($faqUsername, $faqPassword);
413-
$response->setStatusCode(Response::HTTP_OK);
414-
$result = [
415-
'loggedin' => true
416-
];
417-
} catch (Exception $e) {
418-
$faqConfig->getLogger()->error('Failed login: ' . $e->getMessage());
419-
$response->setStatusCode(Response::HTTP_BAD_REQUEST);
420-
$result = [
421-
'loggedin' => false,
422-
'error' => Translation::get('ad_auth_fail')
423-
];
424-
}
425-
$response->setData($result);
426-
break;
427-
428395
case 'register':
429396
if ($faqConfig->get('api.apiClientToken') !== $request->headers->get('x-pmf-token')) {
430397
$response->setStatusCode(Response::HTTP_UNAUTHORIZED);

phpmyfaq/src/api-routes.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use phpMyFAQ\Api\Controller\CommentController;
2121
use phpMyFAQ\Api\Controller\GroupController;
2222
use phpMyFAQ\Api\Controller\LanguageController;
23+
use phpMyFAQ\Api\Controller\LoginController;
2324
use phpMyFAQ\Api\Controller\NewsController;
2425
use phpMyFAQ\Api\Controller\OpenQuestionController;
2526
use phpMyFAQ\Api\Controller\SearchController;
@@ -56,6 +57,10 @@
5657
'api.language',
5758
new Route("v{$apiVersion}/language", ['_controller' => [LanguageController::class, 'index']])
5859
);
60+
$routes->add(
61+
'api.login',
62+
new Route("v{$apiVersion}/login", ['_controller' => [LoginController::class, 'login'], '_methods' => 'POST'])
63+
);
5964
$routes->add(
6065
'api.news',
6166
new Route("v{$apiVersion}/news", ['_controller' => [NewsController::class, 'list']])

phpmyfaq/src/phpMyFAQ/Api/Controller/AttachmentController.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
<?php
22

3+
/**
4+
* The Attachment Controller for the REST API
5+
*
6+
* This Source Code Form is subject to the terms of the Mozilla Public License,
7+
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
8+
* obtain one at https://mozilla.org/MPL/2.0/.
9+
*
10+
* @package phpMyFAQ
11+
* @author Thorsten Rinne <[email protected]>
12+
* @copyright 2023 phpMyFAQ Team
13+
* @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
14+
* @link https://www.phpmyfaq.de
15+
* @since 2023-07-30
16+
*/
17+
318
namespace phpMyFAQ\Api\Controller;
419

520
use phpMyFAQ\Attachment\AttachmentException;

phpmyfaq/src/phpMyFAQ/Api/Controller/CommentController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

33
/**
4-
* The Comemnt Controller for the REST API
4+
* The Comment Controller for the REST API
55
*
66
* This Source Code Form is subject to the terms of the Mozilla Public License,
77
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
/**
4+
* The Login Controller for the REST API
5+
*
6+
* This Source Code Form is subject to the terms of the Mozilla Public License,
7+
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
8+
* obtain one at https://mozilla.org/MPL/2.0/.
9+
*
10+
* @package phpMyFAQ
11+
* @author Thorsten Rinne <[email protected]>
12+
* @copyright 2023 phpMyFAQ Team
13+
* @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
14+
* @link https://www.phpmyfaq.de
15+
* @since 2023-07-30
16+
*/
17+
18+
namespace phpMyFAQ\Api\Controller;
19+
20+
use phpMyFAQ\Configuration;
21+
use phpMyFAQ\Core\Exception;
22+
use phpMyFAQ\Filter;
23+
use phpMyFAQ\Translation;
24+
use phpMyFAQ\User\CurrentUser;
25+
use phpMyFAQ\User\UserAuthentication;
26+
use Symfony\Component\HttpFoundation\JsonResponse;
27+
use Symfony\Component\HttpFoundation\Request;
28+
use Symfony\Component\HttpFoundation\Response;
29+
30+
class LoginController
31+
{
32+
/**
33+
* @throws \JsonException
34+
*/
35+
public function login(Request $request): JsonResponse
36+
{
37+
$response = new JsonResponse();
38+
$faqConfig = Configuration::getConfigurationInstance();
39+
40+
$postBody = json_decode($request->getContent(), false, 512, JSON_THROW_ON_ERROR);
41+
42+
$faqUsername = Filter::filterVar($postBody->username, FILTER_SANITIZE_SPECIAL_CHARS);
43+
$faqPassword = Filter::filterVar($postBody->password, FILTER_SANITIZE_SPECIAL_CHARS);
44+
45+
$user = new CurrentUser($faqConfig);
46+
$userAuth = new UserAuthentication($faqConfig, $user);
47+
try {
48+
$user = $userAuth->authenticate($faqUsername, $faqPassword);
49+
$response->setStatusCode(Response::HTTP_OK);
50+
$result = [
51+
'loggedin' => true
52+
];
53+
} catch (Exception $e) {
54+
$faqConfig->getLogger()->error('Failed login: ' . $e->getMessage());
55+
$response->setStatusCode(Response::HTTP_BAD_REQUEST);
56+
$result = [
57+
'loggedin' => false,
58+
'error' => Translation::get('ad_auth_fail')
59+
];
60+
}
61+
$response->setData($result);
62+
63+
return $response;
64+
}
65+
}

phpmyfaq/src/phpMyFAQ/Api/Controller/NewsController.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
<?php
22

3+
/**
4+
* The News Controller for the REST API
5+
*
6+
* This Source Code Form is subject to the terms of the Mozilla Public License,
7+
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
8+
* obtain one at https://mozilla.org/MPL/2.0/.
9+
*
10+
* @package phpMyFAQ
11+
* @author Thorsten Rinne <[email protected]>
12+
* @copyright 2023 phpMyFAQ Team
13+
* @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
14+
* @link https://www.phpmyfaq.de
15+
* @since 2023-07-30
16+
*/
17+
318
namespace phpMyFAQ\Api\Controller;
419

520
use phpMyFAQ\Configuration;

phpmyfaq/src/phpMyFAQ/Api/Controller/OpenQuestionController.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
<?php
22

3+
/**
4+
* The Open Questions Controller for the REST API
5+
*
6+
* This Source Code Form is subject to the terms of the Mozilla Public License,
7+
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
8+
* obtain one at https://mozilla.org/MPL/2.0/.
9+
*
10+
* @package phpMyFAQ
11+
* @author Thorsten Rinne <[email protected]>
12+
* @copyright 2023 phpMyFAQ Team
13+
* @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
14+
* @link https://www.phpmyfaq.de
15+
* @since 2023-07-29
16+
*/
17+
318
namespace phpMyFAQ\Api\Controller;
419

520
use phpMyFAQ\Configuration;

phpmyfaq/src/phpMyFAQ/Api/Controller/TagController.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
<?php
22

3+
/**
4+
* The Tags Controller for the REST API
5+
*
6+
* This Source Code Form is subject to the terms of the Mozilla Public License,
7+
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
8+
* obtain one at https://mozilla.org/MPL/2.0/.
9+
*
10+
* @package phpMyFAQ
11+
* @author Thorsten Rinne <[email protected]>
12+
* @copyright 2023 phpMyFAQ Team
13+
* @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
14+
* @link https://www.phpmyfaq.de
15+
* @since 2023-07-29
16+
*/
17+
318
namespace phpMyFAQ\Api\Controller;
419

520
use phpMyFAQ\Configuration;

0 commit comments

Comments
 (0)