Login.php 2.05 KB
Newer Older
1 2
<?php
/**
3
 * gredu_labs.
4 5
 * 
 * @link https://github.com/eellak/gredu_labs for the canonical source repository
6
 *
7 8 9 10 11 12 13 14 15 16 17
 * @copyright Copyright (c) 2008-2015 Greek Free/Open Source Software Society (https://gfoss.ellak.gr/)
 * @license GNU GPLv3 http://www.gnu.org/licenses/gpl-3.0-standalone.html
 */

namespace GrEduLabs\Authentication\Action\User;

use Slim\Flash\Messages;
use Slim\Http\Request;
use Slim\Http\Response;
use Slim\Views\Twig;
use Zend\Authentication\Adapter\ValidatableAdapterInterface;
18
use Zend\Authentication\AuthenticationService;
19
use Zend\Crypt\Password\Bcrypt;
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42

class Login
{
    /**
     * @var Twig
     */
    protected $view;

    /**
     * @var AuthenticationService
     */
    protected $authService;

    /**
     * @var Messages
     */
    protected $flash;

    /**
     * @var string
     */
    protected $successUrl;

kanellov's avatar
kanellov committed
43

44
    /**
kanellov's avatar
kanellov committed
45 46
     * 
     * @param Twig $view
47
     * @param AuthenticationService $authService
kanellov's avatar
kanellov committed
48 49
     * @param Messages $flash
     * @param type $successUrl
50 51 52 53 54 55 56 57 58 59 60 61 62 63
     */
    public function __construct(
        Twig $view,
        AuthenticationService $authService,
        Messages $flash,
        $successUrl
    ) {
        $this->view        = $view;
        $this->authService = $authService;
        $this->flash       = $flash;
        $this->successUrl  = $successUrl;
    }

    public function __invoke(Request $req, Response $res)
64
    {
65 66
        if ($req->isPost()) {
            $adapter = $this->authService->getAdapter();
67

68 69 70 71
            if ($adapter instanceof ValidatableAdapterInterface) {
                $adapter->setIdentity($req->getParam('identity'));
                $adapter->setCredential($req->getParam('credential'));
            }
72

73
            $result = $this->authService->authenticate($adapter);
74

75 76 77 78 79 80
            if (!$result->isValid()) {
                $this->flash->addMessage('danger', reset($result->getMessages()));
                return $res->withRedirect($req->getUri());
            }

            return $res->withRedirect($this->successUrl);
81
        }
82

Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
83
        return $this->view->render($res, 'user/login.twig', []);
84 85
    }
}