Db.php 5.48 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;
Bertrand Gauthier's avatar
Bertrand Gauthier committed
13
14
15
16
17
18

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

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

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

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

    /**
     * Clears contents from storage
     *
83
     * @params ChainEvent $e
84
85
86
87
88
89
     * @throws \Zend\Authentication\Exception\InvalidArgumentException If clearing contents from storage is impossible
     * @return void
     */
    public function clear(ChainEvent $e)
    {
        $this->resolvedIdentity = null;
90
        $this->getStorage()->clear();
91
92
93
94
95
96
97
    }
    
    /**
     * 
     * @return null
     */
    protected function findIdentity()
Bertrand Gauthier's avatar
Bertrand Gauthier committed
98
    {
99
100
101
102
103
104
105
106
        $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;
        }
            
107
        /**
108
         * 1ere tentative :
109
110
111
112
113
114
115
         * 
         * 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.
         */
116
117
118
119
120
121
122
123
124
125
        if (is_int($id) || is_scalar($id)) {
            try {
                $identity = $this->getMapper()->findById($id);
            }
            catch (PDOException $pdoe) {
                $identity = null;
            }
            catch (ServiceNotFoundException $e) {
                $identity = null;
            }
Bertrand Gauthier's avatar
Bertrand Gauthier committed
126
127
        }
        
128
129
130
131
132
133
134
135
136
        /**
         * 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.
         */
137
        if (is_string($id)) {
Bertrand Gauthier's avatar
Bertrand Gauthier committed
138
            try {
139
                $identity = $this->getMapper()->findByUsername($id);
Bertrand Gauthier's avatar
Bertrand Gauthier committed
140
            }
141
            catch (PDOException $pdoe) {
Bertrand Gauthier's avatar
Bertrand Gauthier committed
142
143
                $identity = null;
            }
144
145
146
            catch (ServiceNotFoundException $e) {
                $identity = null;
            }
Bertrand Gauthier's avatar
Bertrand Gauthier committed
147
        }
148
        
149
150
151
152
153
154
155
156
157
158
159
160
        return $identity;
    }

    /**
     * getStorage
     *
     * @return StorageInterface
     */
    public function getStorage()
    {
        if (null === $this->storage) {
            $this->setStorage(new Session());
Bertrand Gauthier's avatar
Bertrand Gauthier committed
161
        }
162
163
164
165
166
167
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
        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
201
    }
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216

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

    /**
     * Set service manager instance
     *
     * @param ServiceManager $locator
217
     * @return self
218
219
220
221
     */
    public function setServiceManager(ServiceManager $serviceManager)
    {
        $this->serviceManager = $serviceManager;
222
        return $this;
223
    }
224
}