Graph-IT

Login

This example is about exentending the HelloWorld-example with a simple Login-Check for which we modify the existing controller and add two RequestHandlers and a new service.

In case the credentials provided are valid "Hello '< username >' will be shown, otherwise we ask for credentials.

Let's have a look at the source code first and then look at the other files.

src-folder

The src-folder gets three new files and we adjust the HelloController.php-file.

Important: This is not a proper way to handle passwords! Passwords should only be saved as hashs. Also they should not be hardcoded but looked up from a trusworthy source not a config file.

So far so good, but since the 'LoginRequestHandler' doesn't return a respons in case the authentication was a success, we need an additional RequestHandler.

HelloRequestHandler

The HelloRequestHandler is the DefaultRequestHandler which simple adds the LoginRequestHandler before the ControllerRequestHandler. So when the LoginRequestHandler returns a response to request the user credentials the ControllerRequestHandler will not be called, but when it returns null the ControllerRequestHandler will be called.

<?php

namespace Graphit\Examples;

class HelloRequestHandler implements \Graphit\Core\RequestHandlerInterface
{
  /** @var Kernel */
  protected $kernel;

  /** @var RequestHandlerInterface */
  protected $handler;

  public function __construct(\Graphit\Core\Kernel $kernel)
  {
    $this->kernel = $kernel;

    $this->handler = new \Graphit\Core\ChainRequestHandler();

    $default = $kernel['defaultLocale'] ?: 'de';
    $possible = $kernel['possibleLocales'] ?: ['de'];
    $handler = new \Graphit\Core\LocaleRequestHandler($default, $possible);
    $this->handler->addRequestHandler($handler);

    $router = $kernel['router'];
    $handler = new \Graphit\Core\RouterRequestHandler($router);
    $this->handler->addRequestHandler($handler);

    $handler = new \Graphit\Core\PossibleLocaleRequestHandler();
    $this->handler->addRequestHandler($handler);

    $handler = new LoginRequestHandler($kernel);
    $this->handler->addRequestHandler($handler);

    $handler = new \Graphit\Core\ControllerRequestHandler($kernel);
    $this->handler->addRequestHandler($handler);
  }

  public function handleRequest(\Graphit\Core\Request $request)
  {
    return $this->handler->handleRequest($request);
  }
}

composer.json

The composer.json stays the same as in HelloWorld example..

app.json

The app.json stays the same as in the HelloWorld example.

routes.json

In the routes.json, we throw out the "index"-route and just keep the "named"-route.

{
 "routes": {
    "named": {
      "pattern": "/{person}",
      "default": {
        "_controller": "Graphit\\Examples\\Hello:named"
      },
      "requirements": {
        "_method": "GET"
      }
    }
  }
}

services.json

The services.json got two new entries one for the LoginRequestHandler-class anmd one for the HelloRequestHandler-class.

{
  "authentication": {
    "username": "password",
    "test": "lalala"
  },
  "router": {
    "type": "service",
    "class": "Graphit\\Core\\Router",
    "config": [
      { "method": "addRoutes",
        "args": [
          { "type": "service", "name": "routes" }
        ]
      }
    ]
  },
  "twig": {
    "type": "service",
    "class": "Graphit\\Core\\Twig"
  },
  "LoginRequestHandler": {
    "type": "service",
    "class": "Graphit\\Examples\\LoginRequestHandler",
    "args": [
      { "type": "service", "name": "kernel" }
    ]
  },
  "requestHandler": {
    "type": "service",
    "class": "Graphit\\Examples\\HelloRequestHandler",
    "args": [
      { "type": "service", "name": "kernel" }
    ]
  }
}

Note that:

  1. we added an "autentification" array which holds the valid credentials. This was only done to show how one could add data for a service. This is not a recommend way to handle credentials.
  2. we added "args" to the LoginRequestHandler-service so that the LoginRequestHandler constructor is called with the current Kernel.
  3. we used the reserved Name "requestHandler" when we specified the "HelloRequestHandler"-service. This way our HelloRequestHandler is used instead of the default DefaultRequestHandler.