Db.php 4.49 KB
Newer Older
Bertrand Gauthier's avatar
Bertrand Gauthier committed
1
2
3
4
<?php

namespace UnicaenAuth\Authentication\Storage;

5
use PDOException;
6
7
use Zend\ServiceManager\ServiceManager;
use Zend\ServiceManager\ServiceManagerAwareInterface;
8
use Zend\ServiceManager\Exception\ServiceNotFoundException;
Bertrand Gauthier's avatar
Bertrand Gauthier committed
9
use Zend\Authentication\Exception\InvalidArgumentException;
10
use ZfcUser\Authentication\Storage\Db as DbStorage;
Bertrand Gauthier's avatar
Bertrand Gauthier committed
11
12
13
14
15
16

/**
 * Db authentication storage.
 *
 * @author Bertrand GAUTHIER <bertrand.gauthier at unicaen.fr>
 */
17
class Db implements ChainableStorage, ServiceManagerAwareInterface
Bertrand Gauthier's avatar
Bertrand Gauthier committed
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
    /**
     * @var DbStorage 
     */
    protected $delegate;

    /**
     * @var mixed
     */
    protected $resolvedIdentity;

    /**
     * Writes $contents to storage
     *
     * @param  mixed $contents
     * @throws \Zend\Authentication\Exception\InvalidArgumentException If writing $contents to storage is impossible
     * @return void
     */
    public function write(ChainEvent $e)
    {
        $contents = $e->getParam('contents');
        
        $this->resolvedIdentity = null;
        $this->getDelegate()->write($contents);
    }
    
Bertrand Gauthier's avatar
Bertrand Gauthier committed
44
45
46
47
48
49
50
51
    /**
     * Returns the contents of storage
     *
     * Behavior is undefined when storage is empty.
     *
     * @throws InvalidArgumentException If reading contents from storage is impossible
     * @return \ZfcUser\Entity\UserInterface
     */
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
    public function read(ChainEvent $e)
    {
        $identity = $this->findIdentity();
        
        $e->addContents('db', $identity);
        
        return $identity;
    }

    /**
     * Clears contents from storage
     *
     * @throws \Zend\Authentication\Exception\InvalidArgumentException If clearing contents from storage is impossible
     * @return void
     */
    public function clear(ChainEvent $e)
    {
        $this->resolvedIdentity = null;
        $this->getDelegate()->getStorage()->clear();
    }
    
    /**
     * 
     * @return null
     */
    protected function findIdentity()
Bertrand Gauthier's avatar
Bertrand Gauthier committed
78
    {
79
80
81
82
83
84
85
86
87
        /**
         * 1ere tentative (mécanisme standard du module ZfcUser) :
         * 
         * Recherche dans la base de données de l'utilisateur dont l'id correspond à ce qui
         * est stoqué en session.
         * 
         * NB: En cas de problème de connexion ou de service 'zfcuser_user_mapper' introuvable,
         * cela signifie sans doute que l'application n'utilise pas de table des utilisateurs.
         */
Bertrand Gauthier's avatar
Bertrand Gauthier committed
88
        try {
89
            $identity = $this->getDelegate()->read();
Bertrand Gauthier's avatar
Bertrand Gauthier committed
90
        }
91
        catch (PDOException $pdoe) {
Bertrand Gauthier's avatar
Bertrand Gauthier committed
92
93
            $identity = null;
        }
94
95
96
        catch (ServiceNotFoundException $e) {
            $identity = null;
        }
97
98
        // si on obtient autre chose qu'un scalaire, l'utilisateur a déjà été 
        // recherché/trouvé dans la base de données
Bertrand Gauthier's avatar
Bertrand Gauthier committed
99
100
101
102
        if ($identity && !is_scalar($identity)) {
            return $identity;
        }
        
103
104
105
106
107
108
109
110
111
        /**
         * 2e tentative : 
         * 
         * Recherche de l'utilisateur dont le supannAliasLogin correspond à ce qui
         * est stoqué en session.
         * 
         * NB: En cas de problème de connexion ou de service 'zfcuser_user_mapper' introuvable,
         * cela signifie sans doute que l'application n'utilise pas de table des utilisateurs.
         */
112
        $username = $this->getDelegate()->getStorage()->read();
Bertrand Gauthier's avatar
Bertrand Gauthier committed
113
114
        if (is_string($username)) {
            try {
115
                $identity = $this->getDelegate()->getMapper()->findByUsername($username);
Bertrand Gauthier's avatar
Bertrand Gauthier committed
116
            }
117
            catch (PDOException $pdoe) {
Bertrand Gauthier's avatar
Bertrand Gauthier committed
118
119
                $identity = null;
            }
120
121
122
            catch (ServiceNotFoundException $e) {
                $identity = null;
            }
Bertrand Gauthier's avatar
Bertrand Gauthier committed
123
        }
124
        
Bertrand Gauthier's avatar
Bertrand Gauthier committed
125
126
        if ($identity) {
            $this->resolvedIdentity = $identity;
127
128
        } 
        else {
Bertrand Gauthier's avatar
Bertrand Gauthier committed
129
130
131
132
133
            $this->resolvedIdentity = null;
        }
        
        return $this->resolvedIdentity;
    }
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165

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

    /**
     * Set service manager instance
     *
     * @param ServiceManager $locator
     * @return void
     */
    public function setServiceManager(ServiceManager $serviceManager)
    {
        $this->serviceManager = $serviceManager;
    }
    
    /**
     * @return DbStorage
     */
    protected function getDelegate()
    {
        if (null === $this->delegate) {
            $this->delegate = $this->getServiceManager()->get('ZfcUser\Authentication\Storage\Db');
        }
        return $this->delegate;
    }
166
}