LocalAdapter.php 2.28 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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php

namespace UnicaenAuth\Authentication\Adapter;

use UnicaenAuth\Options\Traits\ModuleOptionsAwareTrait;
use Zend\EventManager\EventInterface;
use Zend\EventManager\EventManagerAwareTrait;
use Zend\Stdlib\ResponseInterface as Response;
use ZfcUser\Authentication\Adapter\AdapterChainEvent;

/**
 * Adapter d'authentification locale (ldap ou db).
 *
 * @author Unicaen
 */
class LocalAdapter extends AbstractAdapter
{
    use ModuleOptionsAwareTrait;
    use EventManagerAwareTrait;
    
    const TYPE = 'local';

    /**
     * @var string
     */
    protected $type = self::TYPE;

    /**
     * @inheritDoc
     */
    public function authenticate(EventInterface $e): bool
    {
        // NB: Dans la version 3.0.0 de zf-commons/zfc-user, cette méthode prend un EventInterface.
        // Mais dans la branche 3.x, c'est un AdapterChainEvent !
        // Si un jour c'est un AdapterChainEvent qui est attendu, plus besoin de faire $e->getTarget().
        $event = $e->getTarget(); /* @var $event AdapterChainEvent */

        $type = $event->getRequest()->getPost()->get('type');
        if ($type !== $this->type) {
            return false;
        }

        $result = $this->getEventManager()->triggerUntil(function ($result) {
            return $result === true || $result instanceof Response;
        }, 'authenticate', $event);

        if ($result->stopped()) {
            $last = $result->last();
            if ($last === true || $last instanceof Response) {
                return $last;
            }
//            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())
//                )
//            );
        }

        return true;
    }

    /**
     * @inheritDoc
     */
    public function logout(EventInterface $e)
    {
        $responseCollection = $this->getEventManager()->triggerUntil(function ($test) {
            return ($test instanceof Response);
        }, 'logout', $e);

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