From 3a3b0357e42172a0c2bc80ef963df458872b1bb7 Mon Sep 17 00:00:00 2001 From: Mehmet Korkmaz Date: Mon, 23 Jul 2018 09:52:32 +0300 Subject: [PATCH] proper demo issues fixed --- composer.json | 2 +- composer.lock | 20 +++++------ config/autoload/dependencies.global.php | 4 +-- src/apps/www/bootstrap.php | 36 ++++++++++++++----- src/apps/www/middlewares/App/Middleware.php | 6 +++- .../App/controllers/Contents/Category.php | 2 +- .../Contents/ContentsController.php | 1 + .../App/controllers/Contents/NotFound.php | 2 +- .../App/controllers/Contents/Post.php | 2 +- .../Auth/controllers/Auth/NotFound.php | 14 -------- src/apps/www/middlewares/Auth/routes.php | 10 ------ .../{Auth => Authentication}/Middleware.php | 17 ++++++--- .../controllers/Application.php | 2 +- .../AuthenticationController.php} | 6 ++-- .../controllers/Authentication/Check.php | 20 +++++++++++ .../controllers/Authentication}/Main.php | 6 ++-- .../controllers/Authentication/NotFound.php | 17 +++++++++ .../www/middlewares/Authentication/routes.php | 12 +++++++ .../{_auth.twig => _authentication.twig} | 0 .../{auth => authentication}/main.twig | 6 ++-- .../{auth => authentication}/not_found.twig | 0 src/factories/ModelFactory.php | 16 --------- src/factories/SelamiAuthFactory.php | 17 --------- 23 files changed, 122 insertions(+), 96 deletions(-) delete mode 100644 src/apps/www/middlewares/Auth/controllers/Auth/NotFound.php delete mode 100644 src/apps/www/middlewares/Auth/routes.php rename src/apps/www/middlewares/{Auth => Authentication}/Middleware.php (55%) rename src/apps/www/middlewares/{Auth => Authentication}/controllers/Application.php (85%) rename src/apps/www/middlewares/{Auth/controllers/Auth/AuthController.php => Authentication/controllers/Authentication/AuthenticationController.php} (67%) create mode 100644 src/apps/www/middlewares/Authentication/controllers/Authentication/Check.php rename src/apps/www/middlewares/{Auth/controllers/Auth => Authentication/controllers/Authentication}/Main.php (60%) create mode 100644 src/apps/www/middlewares/Authentication/controllers/Authentication/NotFound.php create mode 100644 src/apps/www/middlewares/Authentication/routes.php rename src/apps/www/templates/{_auth.twig => _authentication.twig} (100%) rename src/apps/www/templates/{auth => authentication}/main.twig (69%) rename src/apps/www/templates/{auth => authentication}/not_found.twig (100%) delete mode 100644 src/factories/ModelFactory.php delete mode 100644 src/factories/SelamiAuthFactory.php diff --git a/composer.json b/composer.json index 54e25d9..107bbd3 100644 --- a/composer.json +++ b/composer.json @@ -33,7 +33,7 @@ "minimum-stability": "stable", "autoload": { "psr-4": { - "Selami\\Auth\\Controller\\": "src/apps/www/middlewares/Auth/controllers", + "Selami\\Authentication\\Controller\\": "src/apps/www/middlewares/Authentication/controllers", "Selami\\App\\Controller\\": "src/apps/www/middlewares/App/controllers", "Selami\\Middleware\\": "src/apps/www/middlewares", "Selami\\AppFactories\\": "src/factories", diff --git a/composer.lock b/composer.lock index 8c49ddf..b389f7f 100644 --- a/composer.lock +++ b/composer.lock @@ -422,16 +422,16 @@ }, { "name": "selami/foundation", - "version": "0.11.1", + "version": "0.11.2", "source": { "type": "git", "url": "https://github.com/selamiphp/foundation.git", - "reference": "52ce1a74c28d3d909ccb577a681d47cef0d0a55b" + "reference": "abbd348a2fe07fa31bb02c596d4575947d2273fd" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/selamiphp/foundation/zipball/52ce1a74c28d3d909ccb577a681d47cef0d0a55b", - "reference": "52ce1a74c28d3d909ccb577a681d47cef0d0a55b", + "url": "https://api.github.com/repos/selamiphp/foundation/zipball/abbd348a2fe07fa31bb02c596d4575947d2273fd", + "reference": "abbd348a2fe07fa31bb02c596d4575947d2273fd", "shasum": "" }, "require": { @@ -477,7 +477,7 @@ "micro framework", "twig" ], - "time": "2018-07-21T16:23:11+00:00" + "time": "2018-07-23T03:36:25+00:00" }, { "name": "selami/router", @@ -632,16 +632,16 @@ }, { "name": "selami/zend-servicemanager-factories", - "version": "0.6.5", + "version": "0.6.7", "source": { "type": "git", "url": "https://github.com/selamiphp/zend-servicemanager-factories.git", - "reference": "05cde89e0fd6ae4db49e0171dd67a232e150c4f7" + "reference": "9ad157c76395d601fc28e50c7c873212aae7a2d3" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/selamiphp/zend-servicemanager-factories/zipball/05cde89e0fd6ae4db49e0171dd67a232e150c4f7", - "reference": "05cde89e0fd6ae4db49e0171dd67a232e150c4f7", + "url": "https://api.github.com/repos/selamiphp/zend-servicemanager-factories/zipball/9ad157c76395d601fc28e50c7c873212aae7a2d3", + "reference": "9ad157c76395d601fc28e50c7c873212aae7a2d3", "shasum": "" }, "require": { @@ -668,7 +668,7 @@ } ], "description": "Predefined factories to used with Zend Service Manager and Selami", - "time": "2018-07-22T11:34:27+00:00" + "time": "2018-07-23T06:35:36+00:00" }, { "name": "symfony/console", diff --git a/config/autoload/dependencies.global.php b/config/autoload/dependencies.global.php index a31124d..adac4b5 100644 --- a/config/autoload/dependencies.global.php +++ b/config/autoload/dependencies.global.php @@ -6,14 +6,14 @@ use Selami\View\ViewInterface as SelamiView; use Twig_Environment as TwigEnvironment; use Selami\Factories; -use Selami\AppFactories; +use Selami\AppFactories; +use Selami\Foundation\App as SelamiApplication; return [ 'dependencies' => [ 'factories' => [ ServerRequestInterface::class => Factories\ServerRequestFactory::class, SelamiApplication::class => AppFactories\SelamiAppFactory::class, - SelamiAuth::class => AppFactories\SelamiAuthFactory::class, Router::class => Factories\RouterFactory::class, TwigEnvironment::class => Factories\TwigFactory::class, SelamiView::class => Factories\SelamiViewTwigFactory::class diff --git a/src/apps/www/bootstrap.php b/src/apps/www/bootstrap.php index 78009b1..b2d65a8 100644 --- a/src/apps/www/bootstrap.php +++ b/src/apps/www/bootstrap.php @@ -1,35 +1,55 @@ pipe(doublePassMiddleware(function (ServerRequestInterface $request, $response, $next) { - $request = $request->withAttribute('selamiAuth', 'pass'); - $response = $next($request, $response->withHeader('X-Auth', 'Pass')); +// Check Authorization +$app->pipe(doublePassMiddleware(function (ServerRequestInterface $request, ResponseInterface $response, $next) use ($container) { + /** + * @var $session Session + */ + $session = $container->get(Session::class); + $loggedUserId = $session->get('logged_user_id'); + $urlPath = $request->getUri()->getPath(); + if ($loggedUserId === null && strpos($urlPath, '/auth') !== 0) { + return $response->withHeader('Location', '/auth')->withStatus(301); + } + if ($loggedUserId !== null) { + $request = $request->withAttribute('loggedUserId', $loggedUserId); + } + $response = $next($request, $response); return $response; }, new Response())); -// Auth Middleware -$app->pipe(path('/auth', new AuthMiddleware($container))); +// Authentication Middleware +$app->pipe(path('/auth', new AuthenticationMiddleware($container))); // APP $app->pipe(new ApplicationMiddleware($container)); diff --git a/src/apps/www/middlewares/App/Middleware.php b/src/apps/www/middlewares/App/Middleware.php index fd5ef3a..ac0f4b3 100644 --- a/src/apps/www/middlewares/App/Middleware.php +++ b/src/apps/www/middlewares/App/Middleware.php @@ -10,6 +10,7 @@ use Psr\Http\Server\RequestHandlerInterface; use Zend\Diactoros\Response; use Psr\Container\ContainerInterface; +use Selami\Foundation\App as SelamiApplication; class Middleware implements MiddlewareInterface { @@ -24,8 +25,11 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface { $appRoutes = require __DIR__ . '/routes.php'; $this->container->setService('routes', $appRoutes); + //$config = $this->container->get('config'); + //$config['app']['cache_file'] = './cache/application.fastroute.cache'; + //$this->container->setService('config', $config); $this->container->setService(ServerRequestInterface::class, $request); - $myApp = $this->container->get('SelamiApplication'); + $myApp = $this->container->get(SelamiApplication::class); return $myApp($request, new Response()); } diff --git a/src/apps/www/middlewares/App/controllers/Contents/Category.php b/src/apps/www/middlewares/App/controllers/Contents/Category.php index 7b058e3..57074dc 100644 --- a/src/apps/www/middlewares/App/controllers/Contents/Category.php +++ b/src/apps/www/middlewares/App/controllers/Contents/Category.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace SelamiApp\Controller\Contents; +namespace Selami\App\Controller\Contents; use Psr\Container\ContainerInterface; use Selami\Interfaces\Application; diff --git a/src/apps/www/middlewares/App/controllers/Contents/ContentsController.php b/src/apps/www/middlewares/App/controllers/Contents/ContentsController.php index 3015aa6..3c8bbc2 100644 --- a/src/apps/www/middlewares/App/controllers/Contents/ContentsController.php +++ b/src/apps/www/middlewares/App/controllers/Contents/ContentsController.php @@ -13,6 +13,7 @@ abstract class ContentsController extends Application public function __construct(ServerRequestInterface $request, Session $session, ?array $args) { + $this->session = $session; $this->request = $request; $this->args = $args; diff --git a/src/apps/www/middlewares/App/controllers/Contents/NotFound.php b/src/apps/www/middlewares/App/controllers/Contents/NotFound.php index 69c8727..08aca8c 100644 --- a/src/apps/www/middlewares/App/controllers/Contents/NotFound.php +++ b/src/apps/www/middlewares/App/controllers/Contents/NotFound.php @@ -1,7 +1,7 @@ 404, 'data' => ['t' => 'Main']]; - } -} diff --git a/src/apps/www/middlewares/Auth/routes.php b/src/apps/www/middlewares/Auth/routes.php deleted file mode 100644 index 5ca2e29..0000000 --- a/src/apps/www/middlewares/Auth/routes.php +++ /dev/null @@ -1,10 +0,0 @@ -getUri()->getPath(); + //var_dump($urlPath .'<<<-'); $appRoutes = require __DIR__ . '/routes.php'; + $config = $this->container->get('config'); + $config['app']['cache_file'] = './cache/authentication.fastroute.cache'; + $this->container->setAllowOverride(true); + $this->container->setService('config', $config); $this->container->setService('routes', $appRoutes); $this->container->setService(ServerRequestInterface::class, $request); - $myApp = $this->container->get('SelamiAuth'); - return $myApp($request, new Response()); + $this->container->setAllowOverride(false); + + $myApp = $this->container->get(SelamiApplication::class); + $response = $myApp($request, new Response()); + return $response; } diff --git a/src/apps/www/middlewares/Auth/controllers/Application.php b/src/apps/www/middlewares/Authentication/controllers/Application.php similarity index 85% rename from src/apps/www/middlewares/Auth/controllers/Application.php rename to src/apps/www/middlewares/Authentication/controllers/Application.php index 5de6f27..0cc5ba6 100644 --- a/src/apps/www/middlewares/Auth/controllers/Application.php +++ b/src/apps/www/middlewares/Authentication/controllers/Application.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Selami\Auth\Controller; +namespace Selami\Authentication\Controller; use Symfony\Component\HttpFoundation\Session\Session as SymfonySession; use Selami\Helper; diff --git a/src/apps/www/middlewares/Auth/controllers/Auth/AuthController.php b/src/apps/www/middlewares/Authentication/controllers/Authentication/AuthenticationController.php similarity index 67% rename from src/apps/www/middlewares/Auth/controllers/Auth/AuthController.php rename to src/apps/www/middlewares/Authentication/controllers/Authentication/AuthenticationController.php index e713326..197a75e 100644 --- a/src/apps/www/middlewares/Auth/controllers/Auth/AuthController.php +++ b/src/apps/www/middlewares/Authentication/controllers/Authentication/AuthenticationController.php @@ -2,13 +2,13 @@ declare(strict_types=1); -namespace Selami\Auth\Controller\Auth; +namespace Selami\Authentication\Controller\Authentication; -use Selami\Auth\Controller\Application; +use Selami\Authentication\Controller\Application; use Psr\Http\Message\ServerRequestInterface; use Symfony\Component\HttpFoundation\Session\Session; -abstract class AuthController extends Application +abstract class AuthenticationController extends Application { public function __construct(ServerRequestInterface $request, Session $session, ?array $args) diff --git a/src/apps/www/middlewares/Authentication/controllers/Authentication/Check.php b/src/apps/www/middlewares/Authentication/controllers/Authentication/Check.php new file mode 100644 index 0000000..57cf3fc --- /dev/null +++ b/src/apps/www/middlewares/Authentication/controllers/Authentication/Check.php @@ -0,0 +1,20 @@ + 200, + + 'data' => $this->getParams() + ]; + } +} diff --git a/src/apps/www/middlewares/Auth/controllers/Auth/Main.php b/src/apps/www/middlewares/Authentication/controllers/Authentication/Main.php similarity index 60% rename from src/apps/www/middlewares/Auth/controllers/Auth/Main.php rename to src/apps/www/middlewares/Authentication/controllers/Authentication/Main.php index 27a6dda..95cd313 100644 --- a/src/apps/www/middlewares/Auth/controllers/Auth/Main.php +++ b/src/apps/www/middlewares/Authentication/controllers/Authentication/Main.php @@ -1,18 +1,18 @@ 200, 'meta' => [ - 'layout' => 'auth' + 'layout' => 'authentication' ], 'data' => ['t' => self::class] ]; diff --git a/src/apps/www/middlewares/Authentication/controllers/Authentication/NotFound.php b/src/apps/www/middlewares/Authentication/controllers/Authentication/NotFound.php new file mode 100644 index 0000000..e61029b --- /dev/null +++ b/src/apps/www/middlewares/Authentication/controllers/Authentication/NotFound.php @@ -0,0 +1,17 @@ + 404, 'data' => [] + ]; + } +} diff --git a/src/apps/www/middlewares/Authentication/routes.php b/src/apps/www/middlewares/Authentication/routes.php new file mode 100644 index 0000000..d4d0f11 --- /dev/null +++ b/src/apps/www/middlewares/Authentication/routes.php @@ -0,0 +1,12 @@ + -
+

Fake Login Form

- + - +