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

namespace UnicaenAuth\Authentication\Storage;

5
use Doctrine\DBAL\DBALException;
6
7
use Zend\Authentication\Storage\Session;
use Zend\Authentication\Storage\StorageInterface;
8
9
10
use Zend\ServiceManager\Exception\ServiceNotFoundException;
use Zend\ServiceManager\ServiceManager;
use Zend\ServiceManager\ServiceManagerAwareInterface;
11
use ZfcUser\Mapper\UserInterface as UserMapper;
Bertrand Gauthier's avatar
Bertrand Gauthier committed
12
13
14
15
16
17

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

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

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

    /**
36
     * @var ServiceManager
37
     */
38
    protected $serviceManager;
39

Bertrand Gauthier's avatar
Bertrand Gauthier committed
40
41
42
43
44
    /**
     * Returns the contents of storage
     *
     * Behavior is undefined when storage is empty.
     *
45
     * @param ChainEvent $e
Bertrand Gauthier's avatar
Bertrand Gauthier committed
46
     */
47
48
    public function read(ChainEvent $e)
    {
49
50
51
52
53
54
55
56
57
        if (!$this->resolvedIdentity) {
            $identity = $this->findIdentity();
            if ($identity) {
                $this->resolvedIdentity = $identity;
            } 
            else {
                $this->resolvedIdentity = null;
            }
        }
58
        
59
60
61
62
63
64
        $e->addContents('db', $this->resolvedIdentity);
    }

    /**
     * Writes $contents to storage
     *
65
     * @param ChainEvent $e
66
67
68
69
     */
    public function write(ChainEvent $e)
    {
        $contents = $e->getParam('contents');
70
        
71
72
        $this->resolvedIdentity = null;
        $this->getStorage()->write($contents);
73
74
75
76
77
    }

    /**
     * Clears contents from storage
     *
78
     * @param ChainEvent $e
79
80
81
82
     */
    public function clear(ChainEvent $e)
    {
        $this->resolvedIdentity = null;
83
        $this->getStorage()->clear();
84
85
86
87
88
89
    }
    
    /**
     * @return null
     */
    protected function findIdentity()
Bertrand Gauthier's avatar
Bertrand Gauthier committed
90
    {
91
92
93
94
95
96
97
        $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;
        }
98
99
100

        $identity = null;

101
        /**
102
         * 1ere tentative :
103
104
105
106
107
108
109
         * 
         * 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.
         */
110
111
112
113
        if (is_int($id) || is_scalar($id)) {
            try {
                $identity = $this->getMapper()->findById($id);
            }
114
115
116
            catch (DBALException $dbale) {
                $identity = null;
            }
117
118
119
            catch (ServiceNotFoundException $e) {
                $identity = null;
            }
Bertrand Gauthier's avatar
Bertrand Gauthier committed
120
121
        }
        
122
123
124
125
126
127
128
129
130
        /**
         * 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.
         */
131
        if (is_string($id)) {
Bertrand Gauthier's avatar
Bertrand Gauthier committed
132
            try {
133
                $identity = $this->getMapper()->findByUsername($id);
Bertrand Gauthier's avatar
Bertrand Gauthier committed
134
            }
135
136
137
            catch (DBALException $dbale) {
                $identity = null;
            }
138
139
140
            catch (ServiceNotFoundException $e) {
                $identity = null;
            }
Bertrand Gauthier's avatar
Bertrand Gauthier committed
141
        }
142
        
143
144
145
146
147
148
149
150
151
152
153
154
        return $identity;
    }

    /**
     * getStorage
     *
     * @return StorageInterface
     */
    public function getStorage()
    {
        if (null === $this->storage) {
            $this->setStorage(new Session());
Bertrand Gauthier's avatar
Bertrand Gauthier committed
155
        }
156
157
158
159
160
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
        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
195
    }
196
197
198
199
200
201
202
203
204
205
206
207
208
209

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

    /**
     * Set service manager instance
     *
210
     * @param ServiceManager $serviceManager
211
     * @return self
212
213
214
215
     */
    public function setServiceManager(ServiceManager $serviceManager)
    {
        $this->serviceManager = $serviceManager;
216
        return $this;
217
    }
218
}