-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
53 changed files
with
1,505 additions
and
299 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 |
---|---|---|
@@ -1,4 +1,6 @@ | ||
.DS_Store | ||
.vscode | ||
/docs | ||
/internal | ||
vendor | ||
composer.lock | ||
|
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 was deleted.
Oops, something went wrong.
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,69 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Me\AuthorizingRoutes; | ||
|
||
use Pike\Auth\ACL; | ||
use Auryn\Injector; | ||
|
||
abstract class MyAuthModule { | ||
private static $ctx; | ||
/** | ||
* @param \stdClass $ctx {\Pike\Router router} | ||
*/ | ||
public static function init(\stdClass $ctx) { | ||
$ctx->acl = new ACL; | ||
$ctx->acl->setRules(self::makeMyAclRules()); | ||
// | ||
$ctx->router->on('*', function ($req, $res, $next) use ($ctx) { | ||
// Tämä tulisi normaalisti sessiosta ($ctx->auth->getIdentity()->role) | ||
$userRole = LOGGED_IN_USER_ROLE; | ||
// ks. Step 1 | ||
[$action, $resource] = explode(':', $req->routeInfo->myCtx); | ||
if (!$ctx->acl->can($userRole, $action, $resource)) | ||
$res->status(403)->json(['err' => 'Not permitted']); | ||
else | ||
$next(); | ||
}); | ||
// | ||
self::$ctx = $ctx; | ||
} | ||
/** | ||
* @return \stdClass | ||
*/ | ||
private static function makeMyAclRules(): \stdClass { | ||
// Nämä tulisi normaalisti esim. tiedostosta tai tietokannasta. | ||
$resources = (object) [ | ||
'products' => (object) [ | ||
'create' => 1 << 1, | ||
'edit' => 1 << 2, | ||
'comment' => 1 << 3, | ||
], | ||
'reviews' => (object) [ | ||
'post' => 1 << 1, | ||
'approveOrReject' => 1 << 2, | ||
] | ||
]; | ||
$userPermissions = (object) [ | ||
ACL::ROLE_EDITOR => (object) [ | ||
'products' => ACL::makePermissions(['comment', 'edit'], $resources->products), | ||
'reviews' => ACL::makePermissions('*', $resources->reviews), | ||
], | ||
ACL::ROLE_CONTRIBUTOR => (object) [ | ||
'products' => ACL::makePermissions(['comment'], $resources->products), | ||
'reviews' => ACL::makePermissions(['post'], $resources->reviews), | ||
] | ||
]; | ||
return (object) [ | ||
'resources' => $resources, | ||
'userPermissions' => $userPermissions | ||
]; | ||
} | ||
/** | ||
* @param \Auryn\Injector $container | ||
*/ | ||
public static function alterIoc(Injector $container) { | ||
$container->share(self::$ctx->acl); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
examples/AuthorizingRoutes/src/Product/ProductController.php
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,51 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Me\AuthorizingRoutes\Product; | ||
|
||
use Pike\Response; | ||
|
||
class ProductController { | ||
/** | ||
* POST /products | ||
* | ||
* @param \Pike\Response $res | ||
*/ | ||
public function handleCreateProduct(Response $res): void { | ||
// Validoi $req->body, ja insertoi data tietokantaan ... | ||
// | ||
if ('jokinEhto') | ||
$res->json(['insertId' => 1]); | ||
else | ||
$res->status(500)->json(['err' => 'Foo']); | ||
} | ||
/** | ||
* PUT /products/[i:productId] | ||
* | ||
* @param \Pike\Response $res | ||
*/ | ||
public function handleEditProduct(Response $res): void { | ||
// Validoi $req->body, päivitä data tietokantaan id:llä | ||
// $req->params->productId ... | ||
// | ||
if ('jokinEhto') | ||
$res->json(['ok' => 'ok']); | ||
else | ||
$res->status(500)->json(['err' => 'Foo']); | ||
} | ||
/** | ||
* POST /products/[i:productId]/comment | ||
* | ||
* @param \Pike\Response $res | ||
*/ | ||
public function handleAddComment(Response $res): void { | ||
// Validoi $req->body, ja insertoi kommentti tietokantaan tuotteelle | ||
// $req->params->productId ... | ||
// | ||
if ('jokinEhto') | ||
$res->json(['ok' => 'ok']); | ||
else | ||
$res->status(500)->json(['err' => 'Foo']); | ||
} | ||
} |
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,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Me\AuthorizingRoutes\Product; | ||
|
||
abstract class ProductModule { | ||
/** | ||
* @param \stdClass $ctx {\Pike\Router router} | ||
*/ | ||
public static function init(\stdClass $ctx) { | ||
$ctx->router->map('POST', '/products', | ||
[ProductController::class, 'handleCreateProduct', 'create:products'] | ||
); | ||
$ctx->router->map('PUT', '/products/[i:productId]', | ||
[ProductController::class, 'handleEditProduct', 'edit:products'] | ||
); | ||
$ctx->router->map('POST', '/products/[i:productId]/comment', | ||
[ProductController::class, 'handleAddComment', 'comment:products'] | ||
); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
examples/AuthorizingRoutes/src/Review/ReviewController.php
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,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Me\AuthorizingRoutes\Review; | ||
|
||
use Pike\Response; | ||
|
||
class ReviewController { | ||
/** | ||
* POST /reviews | ||
* | ||
* @param \Pike\Response $res | ||
*/ | ||
public function handleCreateReview(Response $res): void { | ||
// Validoi $req->body, ja insertoi data tietokantaan ... | ||
// | ||
if ('jokinEhto') | ||
$res->json(['insertId' => 1]); | ||
else | ||
$res->status(500)->json(['err' => 'Foo']); | ||
} | ||
/** | ||
* PUT /reviews/[i:reviewId]/approve-or-reject | ||
* | ||
* @param \Pike\Response $res | ||
*/ | ||
public function handleApproveOrRejectReview(Response $res): void { | ||
// Validoi $req->body, ja päivitä review $req->params->reviewId tietokantaan ... | ||
// | ||
if ('jokinEhto') | ||
$res->json(['ok' => 'ok']); | ||
else | ||
$res->status(500)->json(['err' => 'Foo']); | ||
} | ||
} |
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,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Me\AuthorizingRoutes\Review; | ||
|
||
abstract class ReviewModule { | ||
/** | ||
* @param \stdClass $ctx {\Pike\Router router} | ||
*/ | ||
public static function init(\stdClass $ctx) { | ||
$ctx->router->map('POST', '/reviews', | ||
[ReviewController::class, 'handleCreateReview', 'post:reviews'] | ||
); | ||
$ctx->router->map('PUT', '/reviews/[i:reviewId]/approve-or-reject', | ||
[ReviewController::class, 'handleApproveOrRejectReview', 'approveOrReject:reviews'] | ||
); | ||
} | ||
} |
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 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Me\HelloWorld; | ||
|
||
class SomeClass { | ||
/** | ||
* @return string|null | ||
*/ | ||
public function doSomething(): ?string { | ||
return 'Hello'; | ||
} | ||
} |
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,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Me\HelloWorld; | ||
|
||
use Pike\Response; | ||
use Pike\Request; | ||
|
||
class SomeController { | ||
/** | ||
* @param \Me\HelloWorld\SomeClass $myClass | ||
* @param \Pike\Response $res | ||
*/ | ||
public function handleSomeRoute(SomeClass $myClass, Response $res): void { | ||
$data = $myClass->doSomething(); | ||
if ($data) | ||
$res->json([$data]); | ||
else | ||
$res->status(500)->json(['err' => 1]); | ||
} | ||
/** | ||
* @param \Pike\Request $req | ||
* @param \Pike\Response $res | ||
*/ | ||
public function handleAnotherRoute(Request $req, Response $res): void { | ||
$res->json(['yourParamWas' => $req->params->someParam, | ||
'requestBodyWas' => $req->body]); | ||
} | ||
} |
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,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Me\HelloWorld; | ||
|
||
abstract class SomeModule { | ||
/** | ||
* @param \stdClass $ctx {\Pike\Router router} | ||
*/ | ||
public static function init(\stdClass $ctx) { | ||
$ctx->router->map('GET', '/some-route', | ||
[SomeController::class, 'handleSomeRoute'] | ||
); | ||
$ctx->router->map('POST', '/another-route/[*:someParam]', | ||
[SomeController::class, 'handleAnotherRoute'] | ||
); | ||
} | ||
} |
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,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Me\MappingRoutes; | ||
|
||
use Pike\Response; | ||
use Pike\Request; | ||
|
||
class Controller { | ||
public function handleRouteA(Request $req, Response $res): void { | ||
$res->json((object) [ | ||
'params' => $req->params, | ||
'body' => $req->body, | ||
'routeInfo' => $req->routeInfo, | ||
]); | ||
} | ||
public function handleRouteB(Request $req, Response $res): void { | ||
$this->handleRouteA($req, $res); | ||
} | ||
public function handleRouteC(Request $req, Response $res): void { | ||
$this->handleRouteA($req, $res); | ||
} | ||
public function handleRouteD(Request $req, Response $res): void { | ||
$this->handleRouteA($req, $res); | ||
} | ||
} |
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,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Me\MappingRoutes; | ||
|
||
abstract class Module { | ||
/** | ||
* @param \stdClass $ctx {\Pike\Router router} | ||
*/ | ||
public static function init(\stdClass $ctx) { | ||
$ctx->router->map('GET', '/route-a', | ||
[Controller::class, 'handleRouteA'] | ||
); | ||
$ctx->router->map('GET', '/route-b/[i:myNumber]/[w:myOptionalSlug]?', | ||
[Controller::class, 'handleRouteB'] | ||
); | ||
$ctx->router->map('GET', '/route-c/[foo|bar:fooOrBar]', | ||
[Controller::class, 'handleRouteC'], | ||
'nameOfRouteC' | ||
); | ||
$ctx->router->map('POST', '/route-d/[i:id]', | ||
[Controller::class, 'handleRouteC', ['my' => 'context']] | ||
); | ||
} | ||
} |
Oops, something went wrong.