Db.php 2.23 KB
Newer Older
1
2
3
4
<?php
namespace UnicaenAuth\Authentication\Adapter;

use PDOException;
5
use Zend\ServiceManager\Exception\ServiceNotFoundException;
6
7
8
9
use UnicaenAuth\Options\ModuleOptions;
use Zend\ServiceManager\ServiceManager;
use Zend\ServiceManager\ServiceManagerAwareInterface;
use ZfcUser\Authentication\Adapter\AdapterChainEvent as AuthEvent;
10
use ZfcUser\Options\AuthenticationOptionsInterface;
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
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
 *
 * @author Bertrand GAUTHIER <bertrand.gauthier@unicaen.fr>
 */
class Db extends \ZfcUser\Authentication\Adapter\Db implements ServiceManagerAwareInterface
{
    /**
     * @var ServiceManager
     */
    protected $serviceManager;

    /**
     * Authentification.
     *
     * @param AuthEvent $e
     * @return boolean
     */
    public function authenticate(AuthEvent $e)
    {
35
36
37
38
        if ($e->getIdentity()) {
            return;
        }
        
39
40
41
42
        try {
            $result = parent::authenticate($e);
        }
        catch (PDOException $e) {
43
44
45
            return false;
        }
        catch (ServiceNotFoundException $e) {
46
47
48
49
50
51
52
            return false;
        }
       
        return $result;
    }

    /**
53
     * @param AuthenticationOptionsInterface $options
54
55
     * @return self
     */
56
    public function setOptions(AuthenticationOptionsInterface $options)
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
    {
        $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;
    }

    /**
     * Get service manager
     *
     * @return ServiceManager
     */
    public function getServiceManager()
    {
        return $this->serviceManager;
    }

    /**
     * Set service manager
     *
     * @param ServiceManager $serviceManager
     * @return self
     */
    public function setServiceManager(ServiceManager $serviceManager)
    {
        $this->serviceManager = $serviceManager;
        return $this;
    }
}