Db.php 2.07 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;
11
12

/**
Bertrand Gauthier's avatar
Bertrand Gauthier committed
13
14
15
 * 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,
16
 * l'authentification ne plante pas (i.e. renvoit false).
17
18
19
 *
 * @author Bertrand GAUTHIER <bertrand.gauthier@unicaen.fr>
 */
20
class Db extends \ZfcUser\Authentication\Adapter\Db implements ServiceLocatorAwareInterface
21
{
22
23
    use ServiceLocatorAwareTrait;

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

        return $this;
    }
38
39
40
41

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

58
59
60
61
        return $result;
    }

    /**
62
     * @param \ZfcUser\Options\ModuleOptions $options
63
64
     * @return self
     */
65
    public function setOptions(\ZfcUser\Options\ModuleOptions $options)
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
    {
        $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;
    }
}