-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit provides the REST bundle and the createSession action. Also update the REST API documentation so that the property names match those use in the domain model classes. It also adds the CI stuff for the new code.
- Loading branch information
1 parent
dbc87a3
commit eaad8ec
Showing
13 changed files
with
4,609 additions
and
18 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
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 |
---|---|---|
|
@@ -3,6 +3,5 @@ | |
/.idea/ | ||
/.project | ||
/.webprj | ||
/composer.lock | ||
/nbproject | ||
/vendor/ |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace PhpList\RestBundle\Controller; | ||
|
||
use Doctrine\Common\Persistence\ObjectRepository; | ||
use Doctrine\ORM\EntityManagerInterface; | ||
use PhpList\PhpList4\Core\Bootstrap; | ||
use PhpList\PhpList4\Domain\Model\Identity\Administrator; | ||
use PhpList\PhpList4\Domain\Model\Identity\AdministratorToken; | ||
use PhpList\PhpList4\Domain\Repository\Identity\AdministratorRepository; | ||
use PhpList\PhpList4\Domain\Repository\Identity\AdministratorTokenRepository; | ||
use Symfony\Bundle\FrameworkBundle\Controller\Controller; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
|
||
/** | ||
* This controller provides methods to create and destroy REST API sessions. | ||
* | ||
* @author Oliver Klee <[email protected]> | ||
*/ | ||
class SessionController extends Controller | ||
{ | ||
/** | ||
* @var EntityManagerInterface | ||
*/ | ||
private $entityManager = null; | ||
|
||
/** | ||
* @var AdministratorRepository|ObjectRepository | ||
*/ | ||
private $administratorRepository = null; | ||
|
||
/** | ||
* @var AdministratorTokenRepository|ObjectRepository | ||
*/ | ||
private $administratorTokenRepository = null; | ||
|
||
/** | ||
* The constructor. | ||
*/ | ||
public function __construct() | ||
{ | ||
// This will later be replaced by dependency injection. | ||
$this->entityManager = Bootstrap::getInstance()->getEntityManager(); | ||
$this->administratorRepository = $this->entityManager->getRepository(Administrator::class); | ||
$this->administratorTokenRepository = $this->entityManager->getRepository(AdministratorToken::class); | ||
} | ||
|
||
/** | ||
* Creates a new session (if the provided credentials are valid). | ||
* | ||
* @param Request $request | ||
* | ||
* @return Response | ||
*/ | ||
public function createAction(Request $request): Response | ||
{ | ||
$rawRequestContent = $request->getContent(); | ||
$response = new Response(); | ||
if (!$this->validateCreateRequest($rawRequestContent, $response)) { | ||
return $response; | ||
} | ||
|
||
$parsedRequestContent = json_decode($rawRequestContent, true); | ||
|
||
$loginName = $parsedRequestContent['loginName']; | ||
$password = $parsedRequestContent['password']; | ||
$administrator = $this->administratorRepository->findOneByLoginCredentials($loginName, $password); | ||
if ($administrator !== null) { | ||
$token = $this->createAndPersistToken($administrator); | ||
$statusCode = 201; | ||
$responseContent = [ | ||
'id' => $token->getId(), | ||
'key' => $token->getKey(), | ||
'expiry' => $token->getExpiry()->format(\DateTime::ATOM), | ||
]; | ||
} else { | ||
$statusCode = 401; | ||
$responseContent = [ | ||
'code' => 1500567098798, | ||
'message' => 'Not authorized', | ||
'description' => 'The user name and password did not match any existing user.', | ||
]; | ||
} | ||
|
||
$response->setStatusCode($statusCode); | ||
$response->setContent(json_encode($responseContent, JSON_NUMERIC_CHECK | JSON_PRETTY_PRINT)); | ||
|
||
return $response; | ||
} | ||
|
||
/** | ||
* Validated the request. If is it not valid, sets a status code and a response content. | ||
* | ||
* @param string $rawRequestContent | ||
* @param Response $response | ||
* | ||
* @return bool whether the response is valid | ||
* | ||
* @return void | ||
*/ | ||
private function validateCreateRequest(string $rawRequestContent, Response $response): bool | ||
{ | ||
$parsedRequestContent = json_decode($rawRequestContent, true); | ||
$isValid = false; | ||
|
||
if ($rawRequestContent === '') { | ||
$responseContent = [ | ||
'code' => 1500559729794, | ||
'message' => 'No data', | ||
'description' => 'The request does not contain any data.', | ||
]; | ||
} elseif ($parsedRequestContent === null) { | ||
$responseContent = [ | ||
'code' => 1500562402438, | ||
'message' => 'Invalid JSON data', | ||
'description' => 'The data in the request is invalid JSON.', | ||
]; | ||
} elseif (empty($parsedRequestContent['loginName']) || empty($parsedRequestContent['password'])) { | ||
$responseContent = [ | ||
'code' => 1500562647846, | ||
'message' => 'Incomplete credentials', | ||
'description' => 'The request does not contain both loginName and password.', | ||
]; | ||
} else { | ||
$responseContent = []; | ||
$isValid = true; | ||
} | ||
|
||
if (!$isValid) { | ||
$response->setStatusCode(500); | ||
$response->setContent(json_encode($responseContent, JSON_NUMERIC_CHECK | JSON_PRETTY_PRINT)); | ||
} | ||
|
||
return $isValid; | ||
} | ||
|
||
/** | ||
* @param Administrator $administrator | ||
* | ||
* @return AdministratorToken | ||
*/ | ||
private function createAndPersistToken(Administrator $administrator): AdministratorToken | ||
{ | ||
$token = new AdministratorToken(); | ||
$token->setAdministrator($administrator); | ||
$token->generateExpiry(); | ||
$token->generateKey(); | ||
|
||
$this->entityManager->persist($token); | ||
$this->entityManager->flush(); | ||
|
||
return $token; | ||
} | ||
} |
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,15 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace PhpList\RestBundle; | ||
|
||
use Symfony\Component\HttpKernel\Bundle\Bundle; | ||
|
||
/** | ||
* This bundle provides the REST API for phpList. | ||
* | ||
* @author Oliver Klee <[email protected]> | ||
*/ | ||
class PhpListRestBundle extends Bundle | ||
{ | ||
} |
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,14 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<!-- https://phpunit.de/manual/current/en/appendixes.configuration.html --> | ||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/6.2/phpunit.xsd" | ||
backupGlobals="false" | ||
colors="true" | ||
bootstrap="../../vendor/autoload.php" | ||
> | ||
<php> | ||
<ini name="error_reporting" value="-1"/> | ||
<server name="KERNEL_CLASS" value="PhpList\PhpList4\Core\ApplicationKernel"/> | ||
</php> | ||
</phpunit> |
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.