Db.php 2.15 KB
Newer Older
1
<?php
2

3
4
namespace UnicaenAuth\Authentication\Adapter;

5
use Interop\Container\ContainerInterface;
6
use UnicaenApp\ServiceManager\ServiceLocatorAwareInterface;
7
use UnicaenApp\ServiceManager\ServiceLocatorAwareTrait;
8
use UnicaenAuth\Options\ModuleOptions;
9
use Zend\ServiceManager\Exception\ServiceNotFoundException;
10
use ZfcUser\Authentication\Adapter\AdapterChainEvent as AuthEvent;
11
use ZfcUser\Options\AuthenticationOptionsInterface;
12
13

/**
Bertrand Gauthier's avatar
Bertrand Gauthier committed
14
15
16
 * Adpater d'authentification à partir de la base de données.
 * 
 * Ajout par rapport à la classe mère : si aucune base de données ou table n'existe,
17
 * l'authentification ne plante pas (i.e. renvoit false).
18
19
20
 *
 * @author Bertrand GAUTHIER <bertrand.gauthier@unicaen.fr>
 */
21
class Db extends \ZfcUser\Authentication\Adapter\Db implements ServiceLocatorAwareInterface
22
{
23
24
    use ServiceLocatorAwareTrait;

25
    /**
26
27
     * Set service locator
     *
28
     * @param ContainerInterface $serviceLocator
29
30
     * @return self
     * @deprecated Abandonnez l'injection du service locator, svp
31
     */
32
    public function setServiceLocator(ContainerInterface $serviceLocator)
33
34
35
36
37
38
    {
        $this->serviceLocator = $serviceLocator;
        $this->serviceManager = $serviceLocator;

        return $this;
    }
39
40
41
42

    /**
     * Authentification.
     *
43
     * @param AuthEvent|\Zend\EventManager\Event $e
44
45
     * @return boolean
     */
46
    public function authenticate(\Zend\EventManager\Event $e)
47
    {
48
49
50
51
        if ($e->getIdentity()) {
            return;
        }
        
52
53
54
        try {
            $result = parent::authenticate($e);
        }
55
        catch (ServiceNotFoundException $e) {
56
57
            return false;
        }
58

59
60
61
62
        return $result;
    }

    /**
63
     * @param AuthenticationOptionsInterface $options
64
65
     * @return self
     */
66
    public function setOptions(AuthenticationOptionsInterface $options)
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
    {
        $this->options = $options;
        return $this;
    }

    /**
     * @return ModuleOptions
     */
    public function getOptions()
    {
        if (!$this->options instanceof ModuleOptions) {
            $this->setOptions($this->getServiceManager()->get('unicaen-auth_module_options'));
        }
        return $this->options;
    }
}