Db.php 5.69 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
11
12
use Zend\Authentication\Storage\Session;
use Zend\Authentication\Storage\StorageInterface;
use ZfcUser\Mapper\UserInterface as UserMapper;
13
use Doctrine\DBAL\DBALException;
Bertrand Gauthier's avatar
Bertrand Gauthier committed
14
15
16
17
18
19

/**
 * Db authentication storage.
 *
 * @author Bertrand GAUTHIER <bertrand.gauthier at unicaen.fr>
 */
20
class Db implements ChainableStorage, ServiceManagerAwareInterface
Bertrand Gauthier's avatar
Bertrand Gauthier committed
21
{
22
    /**
23
     * @var StorageInterface
24
     */
25
26
27
28
29
30
    protected $storage;

    /**
     * @var UserMapper
     */
    protected $mapper;
31
32
33
34
35
36
37

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

    /**
38
     * @var ServiceManager
39
     */
40
    protected $serviceManager;
41
    
Bertrand Gauthier's avatar
Bertrand Gauthier committed
42
43
44
45
46
    /**
     * Returns the contents of storage
     *
     * Behavior is undefined when storage is empty.
     *
47
     * @params ChainEvent $e
Bertrand Gauthier's avatar
Bertrand Gauthier committed
48
     * @throws InvalidArgumentException If reading contents from storage is impossible
49
     * @return void
Bertrand Gauthier's avatar
Bertrand Gauthier committed
50
     */
51
52
    public function read(ChainEvent $e)
    {
53
54
55
56
57
58
59
60
61
        if (!$this->resolvedIdentity) {
            $identity = $this->findIdentity();
            if ($identity) {
                $this->resolvedIdentity = $identity;
            } 
            else {
                $this->resolvedIdentity = null;
            }
        }
62
        
63
64
65
66
67
68
69
70
71
72
73
74
75
        $e->addContents('db', $this->resolvedIdentity);
    }

    /**
     * Writes $contents to storage
     *
     * @params ChainEvent $e
     * @throws \Zend\Authentication\Exception\InvalidArgumentException If writing $contents to storage is impossible
     * @return void
     */
    public function write(ChainEvent $e)
    {
        $contents = $e->getParam('contents');
76
        
77
78
        $this->resolvedIdentity = null;
        $this->getStorage()->write($contents);
79
80
81
82
83
    }

    /**
     * Clears contents from storage
     *
84
     * @params ChainEvent $e
85
86
87
88
89
90
     * @throws \Zend\Authentication\Exception\InvalidArgumentException If clearing contents from storage is impossible
     * @return void
     */
    public function clear(ChainEvent $e)
    {
        $this->resolvedIdentity = null;
91
        $this->getStorage()->clear();
92
93
94
95
96
97
98
    }
    
    /**
     * 
     * @return null
     */
    protected function findIdentity()
Bertrand Gauthier's avatar
Bertrand Gauthier committed
99
    {
100
101
102
103
104
105
106
107
        $id = $this->getStorage()->read();
        
        // si on obtient autre chose qu'un scalaire, l'utilisateur a déjà été 
        // recherché/trouvé dans la base de données
        if ($id && !is_scalar($id)) {
            return $id;
        }
            
108
        /**
109
         * 1ere tentative :
110
111
112
113
114
115
116
         * 
         * 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.
         */
117
118
119
120
        if (is_int($id) || is_scalar($id)) {
            try {
                $identity = $this->getMapper()->findById($id);
            }
121
122
123
            catch (DBALException $dbale) {
                $identity = null;
            }
124
125
126
127
128
129
            catch (PDOException $pdoe) {
                $identity = null;
            }
            catch (ServiceNotFoundException $e) {
                $identity = null;
            }
Bertrand Gauthier's avatar
Bertrand Gauthier committed
130
131
        }
        
132
133
134
135
136
137
138
139
140
        /**
         * 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.
         */
141
        if (is_string($id)) {
Bertrand Gauthier's avatar
Bertrand Gauthier committed
142
            try {
143
                $identity = $this->getMapper()->findByUsername($id);
Bertrand Gauthier's avatar
Bertrand Gauthier committed
144
            }
145
146
147
            catch (DBALException $dbale) {
                $identity = null;
            }
148
            catch (PDOException $pdoe) {
Bertrand Gauthier's avatar
Bertrand Gauthier committed
149
150
                $identity = null;
            }
151
152
153
            catch (ServiceNotFoundException $e) {
                $identity = null;
            }
Bertrand Gauthier's avatar
Bertrand Gauthier committed
154
        }
155
        
156
157
158
159
160
161
162
163
164
165
166
167
        return $identity;
    }

    /**
     * getStorage
     *
     * @return StorageInterface
     */
    public function getStorage()
    {
        if (null === $this->storage) {
            $this->setStorage(new Session());
Bertrand Gauthier's avatar
Bertrand Gauthier committed
168
        }
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
        return $this->storage;
    }

    /**
     * setStorage
     *
     * @param StorageInterface $storage
     * @access public
     * @return Db
     */
    public function setStorage(StorageInterface $storage)
    {
        $this->storage = $storage;
        return $this;
    }

    /**
     * getMapper
     *
     * @return UserMapper
     */
    public function getMapper()
    {
        if (null === $this->mapper) {
            $this->mapper = $this->getServiceManager()->get('zfcuser_user_mapper');
        }
        return $this->mapper;
    }

    /**
     * setMapper
     *
     * @param UserMapper $mapper
     * @return Db
     */
    public function setMapper(UserMapper $mapper = null)
    {
        $this->mapper = $mapper;
        return $this;
Bertrand Gauthier's avatar
Bertrand Gauthier committed
208
    }
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223

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

    /**
     * Set service manager instance
     *
     * @param ServiceManager $locator
224
     * @return self
225
226
227
228
     */
    public function setServiceManager(ServiceManager $serviceManager)
    {
        $this->serviceManager = $serviceManager;
229
        return $this;
230
    }
231
}