Skip to content

Commit

Permalink
Removing ServiceAbstractBase.php
Browse files Browse the repository at this point in the history
  • Loading branch information
byjg committed Dec 10, 2023
1 parent 547a464 commit 7ec35d2
Show file tree
Hide file tree
Showing 12 changed files with 102 additions and 101 deletions.
6 changes: 3 additions & 3 deletions docs/getting_started_03_create_rest_method.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,10 @@ public function putExampleCrudStatus(HttpResponse $response, HttpRequest $reques
// Use one of the following methods:

// a. Require a user with role admin
$data = $this->requireRole("admin");
$data = JwtContext::requireRole("admin");

// b. OR require any logged user
$data = $this->requireAuthenticated();
$data = JwtContext::requireAuthenticated();

// c. OR do nothing to make the endpoint public
}
Expand Down Expand Up @@ -137,7 +137,7 @@ public function putExampleCrudStatus(HttpResponse $response, HttpRequest $reques
// The line below will validate again the OpenAPI attributes
// If the request doesn't match, an exception `Error400Exception` will be thrown
// If the request matches, the payload will be returned
$payload = $this->validateRequest($request);
$payload = OpenApiContext::validateRequest($request);
}
```

Expand Down
4 changes: 2 additions & 2 deletions docs/rest.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ namespace RestReferenceArchitecture\Rest;
use ByJG\RestServer\HttpRequest;
use ByJG\RestServer\HttpResponse;

class Login extends ServiceAbstractBase
class Login
public function mymethod(HttpRequest $request, HttpResponse $response)
{
// ...
Expand All @@ -59,7 +59,7 @@ use ByJG\RestServer\HttpRequest;
use ByJG\RestServer\HttpResponse;
use OpenApi\Attributes as OA;

class Login extends ServiceAbstractBase
class Login

/**
* Do login
Expand Down
2 changes: 1 addition & 1 deletion src/Model/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use OpenApi\Attributes as OA;
use RestReferenceArchitecture\Psr11;

#[OA\Schema(required: ["email"], type: "object", xml: new \OpenApi\Attributes\Xml(name: "User"))]
#[OA\Schema(required: ["email"], type: "object", xml: new OA\Xml(name: "User"))]
class User extends UserModel
{
// Property Fields
Expand Down
5 changes: 3 additions & 2 deletions src/Repository/BaseRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use ByJG\MicroOrm\Mapper;
use ByJG\MicroOrm\Query;
use ByJG\MicroOrm\Repository;
use ByJG\MicroOrm\UpdateConstraint;
use ByJG\Serializer\Exception\InvalidArgumentException;
use RestReferenceArchitecture\Psr11;
use RestReferenceArchitecture\Util\HexUuidLiteral;
Expand Down Expand Up @@ -186,9 +187,9 @@ protected function setClosureFixBinaryUUID(?Mapper $mapper, $binPropertyName = '
* @throws OrmInvalidFieldsException
* @throws \ByJG\MicroOrm\Exception\InvalidArgumentException
*/
public function save($model)
public function save($model, ?UpdateConstraint $updateConstraint = null)
{
$model = $this->repository->save($model);
$model = $this->repository->save($model, $updateConstraint);

$primaryKey = $this->repository->getMapper()->getPrimaryKey()[0];

Expand Down
16 changes: 9 additions & 7 deletions src/Rest/DummyHexRest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@
use RestReferenceArchitecture\Model\User;
use RestReferenceArchitecture\Psr11;
use RestReferenceArchitecture\Repository\DummyHexRepository;
use RestReferenceArchitecture\Util\JwtContext;
use RestReferenceArchitecture\Util\OpenApiContext;

class DummyHexRest extends ServiceAbstractBase
class DummyHexRest
{
/**
* Get the DummyHex by id
Expand Down Expand Up @@ -66,7 +68,7 @@ class DummyHexRest extends ServiceAbstractBase
)]
public function getDummyHex(HttpResponse $response, HttpRequest $request): void
{
$data = $this->requireAuthenticated();
$data = JwtContext::requireAuthenticated();

$dummyHexRepo = Psr11::container()->get(DummyHexRepository::class);
$id = $request->param('id');
Expand Down Expand Up @@ -152,7 +154,7 @@ public function getDummyHex(HttpResponse $response, HttpRequest $request): void
)]
public function listDummyHex(HttpResponse $response, HttpRequest $request): void
{
$data = $this->requireAuthenticated();
$data = JwtContext::requireAuthenticated();

$repo = Psr11::container()->get(DummyHexRepository::class);

Expand Down Expand Up @@ -225,9 +227,9 @@ public function listDummyHex(HttpResponse $response, HttpRequest $request): void
)]
public function postDummyHex(HttpResponse $response, HttpRequest $request): void
{
$data = $this->requireRole(User::ROLE_ADMIN);
$data = JwtContext::requireRole(User::ROLE_ADMIN);

$payload = $this->validateRequest($request);
$payload = OpenApiContext::validateRequest($request);

$model = new DummyHex();
BinderObject::bind($payload, $model);
Expand Down Expand Up @@ -284,9 +286,9 @@ public function postDummyHex(HttpResponse $response, HttpRequest $request): void
)]
public function putDummyHex(HttpResponse $response, HttpRequest $request): void
{
$data = $this->requireRole(User::ROLE_ADMIN);
$data = JwtContext::requireRole(User::ROLE_ADMIN);

$payload = $this->validateRequest($request);
$payload = OpenApiContext::validateRequest($request);

$dummyHexRepo = Psr11::container()->get(DummyHexRepository::class);
$model = $dummyHexRepo->get($payload['id']);
Expand Down
16 changes: 9 additions & 7 deletions src/Rest/DummyRest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@
use RestReferenceArchitecture\Model\User;
use RestReferenceArchitecture\Psr11;
use RestReferenceArchitecture\Repository\DummyRepository;
use RestReferenceArchitecture\Util\JwtContext;
use RestReferenceArchitecture\Util\OpenApiContext;

class DummyRest extends ServiceAbstractBase
class DummyRest
{
/**
* Get the Dummy by id
Expand Down Expand Up @@ -66,7 +68,7 @@ class DummyRest extends ServiceAbstractBase
)]
public function getDummy(HttpResponse $response, HttpRequest $request): void
{
$data = $this->requireAuthenticated();
$data = JwtContext::requireAuthenticated();

$dummyRepo = Psr11::container()->get(DummyRepository::class);
$id = $request->param('id');
Expand Down Expand Up @@ -152,7 +154,7 @@ public function getDummy(HttpResponse $response, HttpRequest $request): void
)]
public function listDummy(HttpResponse $response, HttpRequest $request): void
{
$data = $this->requireAuthenticated();
$data = JwtContext::requireAuthenticated();

$repo = Psr11::container()->get(DummyRepository::class);

Expand Down Expand Up @@ -225,9 +227,9 @@ public function listDummy(HttpResponse $response, HttpRequest $request): void
)]
public function postDummy(HttpResponse $response, HttpRequest $request): void
{
$data = $this->requireRole(User::ROLE_ADMIN);
$data = JwtContext::requireRole(User::ROLE_ADMIN);

$payload = $this->validateRequest($request);
$payload = OpenApiContext::validateRequest($request);

$model = new Dummy();
BinderObject::bind($payload, $model);
Expand Down Expand Up @@ -284,9 +286,9 @@ public function postDummy(HttpResponse $response, HttpRequest $request): void
)]
public function putDummy(HttpResponse $response, HttpRequest $request): void
{
$data = $this->requireRole(User::ROLE_ADMIN);
$data = JwtContext::requireRole(User::ROLE_ADMIN);

$payload = $this->validateRequest($request);
$payload = OpenApiContext::validateRequest($request);

$dummyRepo = Psr11::container()->get(DummyRepository::class);
$model = $dummyRepo->get($payload['id']);
Expand Down
20 changes: 11 additions & 9 deletions src/Rest/Login.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
use RestReferenceArchitecture\Psr11;
use RestReferenceArchitecture\Repository\BaseRepository;
use RestReferenceArchitecture\Util\HexUuidLiteral;
use RestReferenceArchitecture\Util\JwtContext;
use RestReferenceArchitecture\Util\OpenApiContext;

class Login extends ServiceAbstractBase
class Login
{
/**
* Do login
Expand Down Expand Up @@ -53,16 +55,16 @@ class Login extends ServiceAbstractBase
)]
public function post(HttpResponse $response, HttpRequest $request)
{
$this->validateRequest($request);
OpenApiContext::validateRequest($request);

$json = json_decode($request->payload());

$users = Psr11::container()->get(UsersDBDataset::class);
$user = $users->isValidUser($json->username, $json->password);
$metadata = $this->createUserMetadata($user);
$metadata = JwtContext::createUserMetadata($user);

$response->getResponseBag()->setSerializationRule(ResponseBag::SINGLE_OBJECT);
$response->write(['token' => $this->createToken($metadata)]);
$response->write(['token' => JwtContext::createToken($metadata)]);
$response->write(['data' => $metadata]);
}

Expand Down Expand Up @@ -99,7 +101,7 @@ public function post(HttpResponse $response, HttpRequest $request)
)]
public function refreshToken(HttpResponse $response, HttpRequest $request)
{
$result = $this->requireAuthenticated(null, true);
$result = JwtContext::requireAuthenticated(null, true);

$diff = ($result["exp"] - time()) / 60;

Expand All @@ -110,10 +112,10 @@ public function refreshToken(HttpResponse $response, HttpRequest $request)
$users = Psr11::container()->get(UsersDBDataset::class);
$user = $users->getById(new HexUuidLiteral($result["data"]["userid"]));

$metadata = $this->createUserMetadata($user);
$metadata = JwtContext::createUserMetadata($user);

$response->getResponseBag()->setSerializationRule(ResponseBag::SINGLE_OBJECT);
$response->write(['token' => $this->createToken($metadata)]);
$response->write(['token' => JwtContext::createToken($metadata)]);
$response->write(['data' => $metadata]);

}
Expand Down Expand Up @@ -147,7 +149,7 @@ public function refreshToken(HttpResponse $response, HttpRequest $request)
)]
public function postResetRequest(HttpResponse $response, HttpRequest $request)
{
$this->validateRequest($request);
OpenApiContext::validateRequest($request);

$json = json_decode($request->payload());

Expand Down Expand Up @@ -179,7 +181,7 @@ public function postResetRequest(HttpResponse $response, HttpRequest $request)

protected function validateResetToken($response, $request): array
{
$this->validateRequest($request);
OpenApiContext::validateRequest($request);

$json = json_decode($request->payload());

Expand Down
2 changes: 1 addition & 1 deletion src/Rest/Sample.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use ByJG\RestServer\HttpResponse;
use OpenApi\Attributes as OA;

class Sample extends ServiceAbstractBase
class Sample
{
/**
* Simple ping
Expand Down
7 changes: 4 additions & 3 deletions src/Rest/SampleProtected.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
use ByJG\RestServer\HttpResponse;
use OpenApi\Attributes as OA;
use Psr\SimpleCache\InvalidArgumentException;
use RestReferenceArchitecture\Util\JwtContext;

class SampleProtected extends ServiceAbstractBase
class SampleProtected
{
/**
* Sample Ping Only Authenticated
Expand Down Expand Up @@ -43,7 +44,7 @@ class SampleProtected extends ServiceAbstractBase
)]
public function getPing(HttpResponse $response, HttpRequest $request)
{
$this->requireAuthenticated();
JwtContext::requireAuthenticated();

$response->write([
'result' => 'pong'
Expand Down Expand Up @@ -83,7 +84,7 @@ public function getPing(HttpResponse $response, HttpRequest $request)
)]
public function getPingAdm(HttpResponse $response, HttpRequest $request)
{
$this->requireRole('admin');
JwtContext::requireRole('admin');

$response->write([
'result' => 'pongadm'
Expand Down
Loading

0 comments on commit 7ec35d2

Please sign in to comment.