Skip to content

Commit

Permalink
refactor: moved login API to controller
Browse files Browse the repository at this point in the history
  • Loading branch information
thorsten committed Jul 30, 2023
1 parent 7cb8c2c commit 2811a0f
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 41 deletions.
12 changes: 5 additions & 7 deletions phpmyfaq/.htaccess
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,6 @@ RewriteRule ^(.*)$ index.php?action=ask [L,QSA]
RewriteCond %{REQUEST_URI} /open-questions\.html$ [NC]
RewriteRule ^(.*)$ index.php?action=open-questions [L,QSA]

# the help page
RewriteCond %{REQUEST_URI} /help\.html$ [NC]
RewriteRule ^(.*)$ index.php?action=help [L,QSA]

# the contact page
RewriteCond %{REQUEST_URI} /contact\.html$ [NC]
RewriteRule ^(.*)$ index.php?action=contact [L,QSA]
Expand Down Expand Up @@ -155,7 +151,7 @@ RewriteRule api/v2.0/open-questions api/index.php
RewriteRule api/v2.0/comments/([0-9]+) api/index.php
RewriteRule api/v2.0/attachments/([0-9]+) api/index.php
RewriteRule api/v2.0/news api/index.php
RewriteRule api/v2.0/login api.php?action=login [L,QSA]
RewriteRule api/v2.0/login api/index.php
RewriteRule api/v2.0/faqs/([0-9]+) api.php?action=faqs&categoryId=$1 [L,QSA]
RewriteRule api/v2.0/faqs/popular api.php?action=faqs&filter=popular [L,QSA]
RewriteRule api/v2.0/faqs/latest api.php?action=faqs&filter=latest [L,QSA]
Expand All @@ -176,7 +172,7 @@ RewriteRule api/v2.1/open-questions api/index.php
RewriteRule api/v2.1/comments/([0-9]+) api/index.php
RewriteRule api/v2.1/attachments/([0-9]+) api/index.php
RewriteRule api/v2.1/news api/index.php
RewriteRule api/v2.1/login api.php?action=login [L,QSA]
RewriteRule api/v2.1/login api/index.php
RewriteRule api/v2.1/faqs/([0-9]+) api.php?action=faqs&categoryId=$1 [L,QSA]
RewriteRule api/v2.1/faqs/popular api.php?action=faqs&filter=popular [L,QSA]
RewriteRule api/v2.1/faqs/latest api.php?action=faqs&filter=latest [L,QSA]
Expand All @@ -203,7 +199,7 @@ RewriteRule api/v2.2/open-questions api/index.php
RewriteRule api/v2.2/comments/([0-9]+) api/index.php
RewriteRule api/v2.2/attachments/([0-9]+) api/index.php
RewriteRule api/v2.2/news api/index.php
RewriteRule api/v2.2/login api.php?action=login [L,QSA]
RewriteRule api/v2.2/login api/index.php
RewriteRule api/v2.2/faqs/([0-9]+) api.php?action=faqs&categoryId=$1 [L,QSA]
RewriteRule api/v2.2/faqs/popular api.php?action=faqs&filter=popular [L,QSA]
RewriteRule api/v2.2/faqs/latest api.php?action=faqs&filter=latest [L,QSA]
Expand All @@ -221,6 +217,8 @@ RewriteRule api/v2.3/categories api/index.php
RewriteRule api/v2.3/comments/([0-9]+) api/index.php
RewriteRule api/v2.3/groups api/index.php
RewriteRule api/v2.3/language api/index.php
RewriteRule api/v2.3/login api/index.php
RewriteRule api/v2.3/news api/index.php
RewriteRule api/v2.3/open-questions api/index.php
RewriteRule api/v2.3/search api/index.php
RewriteRule api/v2.3/searches/popular api/index.php
Expand Down
33 changes: 0 additions & 33 deletions phpmyfaq/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,15 +221,6 @@
$response->setData($result);
break;

case 'news':
$news = new News($faqConfig);
$result = $news->getLatestData(false, true, true);
if ((is_countable($result) ? count($result) : 0) === 0) {
$response->setStatusCode(Response::HTTP_NOT_FOUND);
}
$response->setData($result);
break;

case 'faqs':
$filter = Filter::filterInput(INPUT_GET, 'filter', FILTER_SANITIZE_SPECIAL_CHARS);
$faq = new Faq($faqConfig);
Expand Down Expand Up @@ -401,30 +392,6 @@
$response->setData($result);
break;

case 'login':
$postData = json_decode(file_get_contents('php://input'), true, 512, JSON_THROW_ON_ERROR);
$faqUsername = Filter::filterVar($postData['username'], FILTER_SANITIZE_SPECIAL_CHARS);
$faqPassword = Filter::filterVar($postData['password'], FILTER_SANITIZE_SPECIAL_CHARS);

$user = new CurrentUser($faqConfig);
$userAuth = new UserAuthentication($faqConfig, $user);
try {
$user = $userAuth->authenticate($faqUsername, $faqPassword);
$response->setStatusCode(Response::HTTP_OK);
$result = [
'loggedin' => true
];
} catch (Exception $e) {
$faqConfig->getLogger()->error('Failed login: ' . $e->getMessage());
$response->setStatusCode(Response::HTTP_BAD_REQUEST);
$result = [
'loggedin' => false,
'error' => Translation::get('ad_auth_fail')
];
}
$response->setData($result);
break;

case 'register':
if ($faqConfig->get('api.apiClientToken') !== $request->headers->get('x-pmf-token')) {
$response->setStatusCode(Response::HTTP_UNAUTHORIZED);
Expand Down
5 changes: 5 additions & 0 deletions phpmyfaq/src/api-routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use phpMyFAQ\Api\Controller\CommentController;
use phpMyFAQ\Api\Controller\GroupController;
use phpMyFAQ\Api\Controller\LanguageController;
use phpMyFAQ\Api\Controller\LoginController;
use phpMyFAQ\Api\Controller\NewsController;
use phpMyFAQ\Api\Controller\OpenQuestionController;
use phpMyFAQ\Api\Controller\SearchController;
Expand Down Expand Up @@ -56,6 +57,10 @@
'api.language',
new Route("v{$apiVersion}/language", ['_controller' => [LanguageController::class, 'index']])
);
$routes->add(
'api.login',
new Route("v{$apiVersion}/login", ['_controller' => [LoginController::class, 'login'], '_methods' => 'POST'])
);
$routes->add(
'api.news',
new Route("v{$apiVersion}/news", ['_controller' => [NewsController::class, 'list']])
Expand Down
15 changes: 15 additions & 0 deletions phpmyfaq/src/phpMyFAQ/Api/Controller/AttachmentController.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
<?php

/**
* The Attachment Controller for the REST API
*
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at https://mozilla.org/MPL/2.0/.
*
* @package phpMyFAQ
* @author Thorsten Rinne <[email protected]>
* @copyright 2023 phpMyFAQ Team
* @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
* @link https://www.phpmyfaq.de
* @since 2023-07-30
*/

namespace phpMyFAQ\Api\Controller;

use phpMyFAQ\Attachment\AttachmentException;
Expand Down
2 changes: 1 addition & 1 deletion phpmyfaq/src/phpMyFAQ/Api/Controller/CommentController.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/**
* The Comemnt Controller for the REST API
* The Comment Controller for the REST API
*
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
Expand Down
65 changes: 65 additions & 0 deletions phpmyfaq/src/phpMyFAQ/Api/Controller/LoginController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

/**
* The Login Controller for the REST API
*
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at https://mozilla.org/MPL/2.0/.
*
* @package phpMyFAQ
* @author Thorsten Rinne <[email protected]>
* @copyright 2023 phpMyFAQ Team
* @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
* @link https://www.phpmyfaq.de
* @since 2023-07-30
*/

namespace phpMyFAQ\Api\Controller;

use phpMyFAQ\Configuration;
use phpMyFAQ\Core\Exception;
use phpMyFAQ\Filter;
use phpMyFAQ\Translation;
use phpMyFAQ\User\CurrentUser;
use phpMyFAQ\User\UserAuthentication;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class LoginController
{
/**
* @throws \JsonException
*/
public function login(Request $request): JsonResponse
{
$response = new JsonResponse();
$faqConfig = Configuration::getConfigurationInstance();

$postBody = json_decode($request->getContent(), false, 512, JSON_THROW_ON_ERROR);

$faqUsername = Filter::filterVar($postBody->username, FILTER_SANITIZE_SPECIAL_CHARS);
$faqPassword = Filter::filterVar($postBody->password, FILTER_SANITIZE_SPECIAL_CHARS);

$user = new CurrentUser($faqConfig);
$userAuth = new UserAuthentication($faqConfig, $user);
try {
$user = $userAuth->authenticate($faqUsername, $faqPassword);
$response->setStatusCode(Response::HTTP_OK);
$result = [
'loggedin' => true
];
} catch (Exception $e) {
$faqConfig->getLogger()->error('Failed login: ' . $e->getMessage());
$response->setStatusCode(Response::HTTP_BAD_REQUEST);
$result = [
'loggedin' => false,
'error' => Translation::get('ad_auth_fail')
];
}
$response->setData($result);

return $response;
}
}
15 changes: 15 additions & 0 deletions phpmyfaq/src/phpMyFAQ/Api/Controller/NewsController.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
<?php

/**
* The News Controller for the REST API
*
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at https://mozilla.org/MPL/2.0/.
*
* @package phpMyFAQ
* @author Thorsten Rinne <[email protected]>
* @copyright 2023 phpMyFAQ Team
* @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
* @link https://www.phpmyfaq.de
* @since 2023-07-30
*/

namespace phpMyFAQ\Api\Controller;

use phpMyFAQ\Configuration;
Expand Down
15 changes: 15 additions & 0 deletions phpmyfaq/src/phpMyFAQ/Api/Controller/OpenQuestionController.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
<?php

/**
* The Open Questions Controller for the REST API
*
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at https://mozilla.org/MPL/2.0/.
*
* @package phpMyFAQ
* @author Thorsten Rinne <[email protected]>
* @copyright 2023 phpMyFAQ Team
* @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
* @link https://www.phpmyfaq.de
* @since 2023-07-29
*/

namespace phpMyFAQ\Api\Controller;

use phpMyFAQ\Configuration;
Expand Down
15 changes: 15 additions & 0 deletions phpmyfaq/src/phpMyFAQ/Api/Controller/TagController.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
<?php

/**
* The Tags Controller for the REST API
*
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at https://mozilla.org/MPL/2.0/.
*
* @package phpMyFAQ
* @author Thorsten Rinne <[email protected]>
* @copyright 2023 phpMyFAQ Team
* @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
* @link https://www.phpmyfaq.de
* @since 2023-07-29
*/

namespace phpMyFAQ\Api\Controller;

use phpMyFAQ\Configuration;
Expand Down

0 comments on commit 2811a0f

Please sign in to comment.