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

3
4
namespace UnicaenAuth\Authentication\Adapter;

5
use UnicaenApp\ServiceManager\ServiceLocatorAwareInterface;
6
use UnicaenAuth\Options\ModuleOptions;
7
use Zend\ServiceManager\Exception\ServiceNotFoundException;
8
9
use Zend\ServiceManager\ServiceLocatorAwareTrait;
use Zend\ServiceManager\ServiceLocatorInterface;
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
28
29
30
     * Set service locator
     *
     * @param ServiceLocatorInterface $serviceLocator
     * @return self
     * @deprecated Abandonnez l'injection du service locator, svp
31
     */
32
33
34
35
36
37
38
    public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
    {
        $this->serviceLocator = $serviceLocator;
        $this->serviceManager = $serviceLocator;

        return $this;
    }
39
40
41
42
43
44
45
46
47

    /**
     * Authentification.
     *
     * @param AuthEvent $e
     * @return boolean
     */
    public function authenticate(AuthEvent $e)
    {
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;
    }
}