AdapterChain.php 2.43 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?php

namespace UnicaenAuth\Authentication\Adapter;

use Zend\Stdlib\RequestInterface as Request;
use Zend\Stdlib\ResponseInterface as Response;
use ZfcUser\Exception;

class AdapterChain extends \ZfcUser\Authentication\Adapter\AdapterChain
{
    /**
     * prepareForAuthentication
     *
     * @param  Request $request
     * @return Response|bool
     * @throws Exception\AuthenticationEventException
     */
    public function prepareForAuthentication(Request $request)
    {
        $e = $this->getEvent();
        $e->setRequest($request);

        $this->getEventManager()->trigger('authenticate.pre', $e);

25
26
        $result = $this->getEventManager()->triggerUntil(function ($result) {
            return $result === true || $result instanceof Response;
27
28
29
        }, 'authenticate', $e);

        if ($result->stopped()) {
30
31
32
            $last = $result->last();
            if ($last === true || $last instanceof Response) {
                return $last;
33
            }
34
35
36
37
38
39
//            throw new Exception\AuthenticationEventException(
//                sprintf(
//                    'Auth event was stopped without a response. Got "%s" instead',
//                    is_object($result->last()) ? get_class($result->last()) : gettype($result->last())
//                )
//            );
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
        }

        if ($e->getIdentity()) {
            $this->getEventManager()->trigger('authenticate.success', $e);
            return true;
        }

        $this->getEventManager()->trigger('authenticate.fail', $e);

        return false;
    }

    /**
     * logoutAdapters
     *
     * @return Response|null
     */
57
    public function logoutAdapters(): ?Response
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
    {
        //Adapters might need to perform additional cleanup after logout
        $responseCollection = $this->getEventManager()->triggerUntil(function ($test) {
            return ($test instanceof Response);
        }, 'logout', $this->getEvent());

        if ($responseCollection->stopped()) {
            if ($responseCollection->last() instanceof Response) {
                return $responseCollection->last();
            }

            throw new Exception\AuthenticationEventException(
                sprintf(
                    'Auth event was stopped without a response. Got "%s" instead',
                    is_object($responseCollection->last()) ? get_class($responseCollection->last()) : gettype($responseCollection->last())
                )
            );
        }

        return null;
    }
}