-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE] Add HTTP-REST API for Events (#105)
* Add api for event list --------- Co-authored-by: Andreas Pfeiffer <[email protected]> Co-authored-by: Alexander Bigga <[email protected]> Co-authored-by: Andreas Pfeiffer <[email protected]>
- Loading branch information
1 parent
f024616
commit 0fc1445
Showing
19 changed files
with
1,289 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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,149 @@ | ||
<?php | ||
|
||
namespace Slub\SlubEvents\Authentication; | ||
|
||
/* | ||
* This file is part of the TYPO3 CMS project. | ||
* | ||
* It is free software; you can redistribute it and/or modify it under | ||
* the terms of the GNU General Public License, either version 3 | ||
* of the License, or any later version. | ||
* | ||
* For the full copyright and license information, please read the | ||
* LICENSE.txt file that was distributed with this source code. | ||
* | ||
* The TYPO3 project - inspiring people to share! | ||
*/ | ||
|
||
use Slub\SlubEvents\Mvc\View\JsonView; | ||
use TYPO3\CMS\Core\Utility\GeneralUtility; | ||
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface; | ||
use TYPO3\CMS\Extbase\Object\ObjectManager; | ||
|
||
/** | ||
* Class ApiAuthentication | ||
* @package Slub\SlubEvents\Authentication | ||
*/ | ||
class ApiAuthentication | ||
{ | ||
public const LL_PATH = 'LLL:EXT:slub_events/Resources/Private/Language/locallang_api.xlf'; | ||
public const EXTENSION_NAME = 'slubevents'; | ||
|
||
/** | ||
* @var string[] | ||
*/ | ||
public $error = [ | ||
401 => 'Invalid authorization' | ||
]; | ||
|
||
/** | ||
* @var ConfigurationManagerInterface | ||
*/ | ||
protected $configurationManager; | ||
|
||
public function __construct() | ||
{ | ||
/** @var ObjectManager $objectManager */ | ||
$objectManager = GeneralUtility::makeInstance(ObjectManager::class); | ||
|
||
$this->configurationManager = $objectManager->get(ConfigurationManagerInterface::class); | ||
} | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
public function authenticateUser(): bool | ||
{ | ||
$users = $this->findAllUsers(); | ||
$apiUser = $this->getApiUser(); | ||
|
||
return $this->isValiduser($users, $apiUser); | ||
} | ||
|
||
/** | ||
* @param JsonView $view | ||
* @param int $status | ||
* @return JsonView | ||
*/ | ||
public function getError(JsonView $view, int $status): JsonView | ||
{ | ||
$view->setVariablesToRender(['error']); | ||
$view->assign('error', [ | ||
'error' => [ | ||
'status' => $status, | ||
'message' => $this->error[$status] | ||
] | ||
]); | ||
|
||
return $view; | ||
} | ||
|
||
/** | ||
* @param array $users | ||
* @param array $apiUser | ||
* @return bool | ||
*/ | ||
protected function isValidUser(array $users, array $apiUser): bool | ||
{ | ||
if (count($users) === 0 || count($apiUser) === 0) { | ||
return false; | ||
} | ||
|
||
foreach ($users as $user) { | ||
// Security risk if there is a user with empty username and empty password | ||
// Well, close the api in general | ||
if (empty($user['username']) || empty($user['password'])) { | ||
return false; | ||
} | ||
|
||
if ($user['username'] === $apiUser['username'] && | ||
$user['password'] === $apiUser['password'] | ||
) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
protected function getApiUser(): array | ||
{ | ||
$user = []; | ||
$authorization = $_SERVER['HTTP_AUTHORIZATION'] ?? null; | ||
|
||
if (stripos($authorization, 'Basic ') === 0) { | ||
$user = GeneralUtility::trimExplode(':', base64_decode(substr($authorization, 6)), 2); | ||
} | ||
|
||
if (count($user) === 2) { | ||
return [ | ||
'username' => $user[0], | ||
'password' => $user[1] | ||
]; | ||
} | ||
|
||
return []; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
protected function findAllUsers(): array | ||
{ | ||
return $this->getExtensionSettings()['api']['users'] ?? []; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
protected function getExtensionSettings(): array | ||
{ | ||
return (array)$this->configurationManager->getConfiguration( | ||
ConfigurationManagerInterface::CONFIGURATION_TYPE_SETTINGS, | ||
self::EXTENSION_NAME | ||
); | ||
} | ||
} |
This file contains 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,118 @@ | ||
<?php | ||
|
||
namespace Slub\SlubEvents\Controller\Api; | ||
|
||
/* | ||
* This file is part of the TYPO3 CMS project. | ||
* | ||
* It is free software; you can redistribute it and/or modify it under | ||
* the terms of the GNU General Public License, either version 3 | ||
* of the License, or any later version. | ||
* | ||
* For the full copyright and license information, please read the | ||
* LICENSE.txt file that was distributed with this source code. | ||
* | ||
* The TYPO3 project - inspiring people to share! | ||
*/ | ||
|
||
use Slub\SlubEvents\Authentication\ApiAuthentication; | ||
use Slub\SlubEvents\Controller\AbstractController; | ||
use Slub\SlubEvents\Mvc\View\JsonView; | ||
use Slub\SlubEvents\Service\ApiService; | ||
use Slub\SlubEvents\Service\EventService; | ||
use TYPO3\CMS\Core\Utility\GeneralUtility; | ||
use TYPO3\CMS\Extbase\Mvc\View\ViewInterface; | ||
use TYPO3\CMS\Extbase\Object\ObjectManager; | ||
|
||
/** | ||
* @package slub_events | ||
* @license http://www.gnu.org/licenses/gpl.html GNU General Public License, version 3 or later | ||
*/ | ||
class EventController extends AbstractController | ||
{ | ||
/** | ||
* @var ApiService | ||
*/ | ||
protected $apiService; | ||
|
||
/** | ||
* @var ApiAuthentication | ||
*/ | ||
protected $apiAuthentication; | ||
|
||
/** | ||
* @var EventService | ||
*/ | ||
protected $eventService; | ||
|
||
/** | ||
* @var JsonView | ||
*/ | ||
protected $view; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $defaultViewObjectName = JsonView::class; | ||
|
||
/** | ||
* @var bool | ||
*/ | ||
protected $allowApiAccess = false; | ||
|
||
/** | ||
* EventController constructor. | ||
*/ | ||
public function __construct() | ||
{ | ||
/** @var ObjectManager $objectManager */ | ||
$objectManager = GeneralUtility::makeInstance(ObjectManager::class); | ||
|
||
$this->apiService = $objectManager->get(ApiService::class); | ||
$this->apiAuthentication = $objectManager->get(ApiAuthentication::class); | ||
$this->eventService = $objectManager->get(EventService::class); | ||
|
||
$this->allowApiAccess = $this->apiAuthentication->authenticateUser(); | ||
} | ||
|
||
/** | ||
* @param ViewInterface $view | ||
*/ | ||
public function initializeView(ViewInterface $view): void | ||
{ | ||
parent::initializeView($view); | ||
|
||
if (!$this->allowApiAccess) { | ||
$this->view = $this->apiAuthentication->getError($this->view, 401); | ||
} | ||
} | ||
|
||
/** | ||
* @return void | ||
*/ | ||
public function listAction(): void | ||
{ | ||
if ($this->allowApiAccess) { | ||
$arguments = $this->apiService->prepareArgumentsDefault($this->request->getArguments()); | ||
$events = $this->eventService->findAllBySettings($arguments); | ||
|
||
$this->view->setVariablesToRender(['events']); | ||
$this->view->assign('events', $events); | ||
} | ||
} | ||
|
||
/** | ||
* @return void | ||
*/ | ||
public function listUserAction(): void | ||
{ | ||
if ($this->allowApiAccess) { | ||
$arguments = $this->apiService->prepareArgumentsUser($this->request->getArguments()); | ||
$events = $arguments['user'] === 0 ? [] : $this->eventService->findAllBySettings($arguments); | ||
$eventsUser = $this->eventService->prepareForUser($arguments['user'], $events, $this->settings); | ||
|
||
$this->view->setVariablesToRender(['eventsUser']); | ||
$this->view->assign('eventsUser', $eventsUser); | ||
} | ||
} | ||
} |
This file contains 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 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 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 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
Oops, something went wrong.