LoginSso.php 1.61 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
<?php
/**
 * gredu_labs
 * 
 * @link https://github.com/eellak/gredu_labs for the canonical source repository
 * @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\Action\User;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
14 15
use Slim\Flash\Messages;
use Zend\Authentication\AuthenticationServiceInterface;
16 17 18 19 20

class LoginSso
{

    /**
21
     * @var AuthenticationServiceInterface
22
     */
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
    protected $authService;

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

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

    /**
     * @var string
     */
    protected $failureUrl;
39 40 41

    /**
     * Constructor
42 43
     * @param AuthenticationServiceInterface $authService
     * @param Messages $flash
44
     */
45 46 47 48 49 50 51 52 53 54
    public function __construct(
        AuthenticationServiceInterface $authService,
        Messages $flash,
        $successUrl,
        $failureUrl
    ) {
        $this->authService = $authService;
        $this->flash       = $flash;
        $this->successUrl  = $successUrl;
        $this->failureUrl  = $failureUrl;
55 56
    }

57 58 59 60 61 62 63
    public function __invoke(
        ServerRequestInterface $req,
        ResponseInterface $res
    ) {
        $result = $this->authService->authenticate();
        if (!$result->isValid()) {
            $this->flash->addMessage('danger', reset($result->getMessages()));
64

65 66
            return $res->withRedirect($this->failureUrl);
        }
67

68
        return $res->withRedirect($this->successUrl);
69 70
    }
}