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

namespace UnicaenAuth\Authentication\Storage;

5
use UnicaenAuth\Entity\Ldap\People;
6
use UnicaenApp\Mapper\Ldap\People as LdapPeopleMapper;
7
use UnicaenAuth\Options\ModuleOptions;
Bertrand Gauthier's avatar
Bertrand Gauthier committed
8
9
10
11
use Zend\Authentication\Exception\InvalidArgumentException;
use Zend\Authentication\Storage\StorageInterface;
use Zend\ServiceManager\ServiceManager;
use Zend\ServiceManager\ServiceManagerAwareInterface;
12
use Zend\Authentication\Storage\Session;
Bertrand Gauthier's avatar
Bertrand Gauthier committed
13
14
15
16
17
18

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

    /**
27
     * @var LdapPeopleMapper
Bertrand Gauthier's avatar
Bertrand Gauthier committed
28
29
     */
    protected $mapper;
30
31
32
33
34
    
    /**
     * @var ModuleOptions
     */
    protected $options;
Bertrand Gauthier's avatar
Bertrand Gauthier committed
35
36

    /**
37
     * @var People
Bertrand Gauthier's avatar
Bertrand Gauthier committed
38
39
40
41
42
43
44
45
46
47
48
49
50
51
     */
    protected $resolvedIdentity;

    /**
     * @var ServiceManager
     */
    protected $serviceManager;

    /**
     * Returns the contents of storage
     *
     * Behavior is undefined when storage is empty.
     *
     * @throws InvalidArgumentException If reading contents from storage is impossible
52
     * @return People
Bertrand Gauthier's avatar
Bertrand Gauthier committed
53
     */
54
55
56
57
58
59
60
61
62
63
64
65
66
67
    public function read(ChainEvent $e)
    {
        $identity = $this->findIdentity();
        
        $e->addContents('ldap', $identity);
        
        return $identity;
    }

    /**
     * 
     * @return null
     */
    protected function findIdentity()
Bertrand Gauthier's avatar
Bertrand Gauthier committed
68
69
70
71
72
73
74
75
    {
        if (null !== $this->resolvedIdentity) {
            return $this->resolvedIdentity;
        }

        $identity = $this->getStorage()->read();

        if (is_scalar($identity)) {
76
77
78
79
80
81
82
83
84
            try {
                $identity = $this->getMapper()->findOneByUsername($identity);
            }
            catch (\Zend\Ldap\Exception\LdapException $exc) {
                $identity = null;
            }
            catch (\UnicaenApp\Exception $exc) {
                $identity = null;
            }
Bertrand Gauthier's avatar
Bertrand Gauthier committed
85
86
87
        }

        if ($identity) {
88
            $this->resolvedIdentity = new People($identity);
Bertrand Gauthier's avatar
Bertrand Gauthier committed
89
90
91
92
93
94
        } else {
            $this->resolvedIdentity = null;
        }

        return $this->resolvedIdentity;
    }
95
    
Bertrand Gauthier's avatar
Bertrand Gauthier committed
96
97
98
99
100
101
102
    /**
     * Writes $contents to storage
     *
     * @param  mixed $contents
     * @throws InvalidArgumentException If writing $contents to storage is impossible
     * @return void
     */
103
    public function write(ChainEvent $e)
Bertrand Gauthier's avatar
Bertrand Gauthier committed
104
    {
105
106
        $contents = $e->getParam('contents');
        
Bertrand Gauthier's avatar
Bertrand Gauthier committed
107
108
109
110
111
112
113
114
115
116
        $this->resolvedIdentity = null;
        $this->getStorage()->write($contents);
    }

    /**
     * Clears contents from storage
     *
     * @throws InvalidArgumentException If clearing contents from storage is impossible
     * @return void
     */
117
    public function clear(ChainEvent $e)
Bertrand Gauthier's avatar
Bertrand Gauthier committed
118
119
120
121
122
123
124
125
    {
        $this->resolvedIdentity = null;
        $this->getStorage()->clear();
    }

    /**
     * getStorage
     *
126
     * @return StorageInterface
Bertrand Gauthier's avatar
Bertrand Gauthier committed
127
128
129
130
     */
    public function getStorage()
    {
        if (null === $this->storage) {
131
            $this->setStorage(new Session());
Bertrand Gauthier's avatar
Bertrand Gauthier committed
132
133
134
135
136
137
138
        }
        return $this->storage;
    }

    /**
     * setStorage
     *
139
     * @param StorageInterface $storage
Bertrand Gauthier's avatar
Bertrand Gauthier committed
140
141
142
     * @access public
     * @return Ldap
     */
143
    public function setStorage(StorageInterface $storage)
Bertrand Gauthier's avatar
Bertrand Gauthier committed
144
145
146
147
148
149
150
151
    {
        $this->storage = $storage;
        return $this;
    }

    /**
     * getMapper
     *
152
     * @return LdapPeopleMapper
Bertrand Gauthier's avatar
Bertrand Gauthier committed
153
154
155
156
     */
    public function getMapper()
    {
        if (null === $this->mapper) {
157
            $this->mapper = $this->getServiceManager()->get('ldap_people_mapper');
Bertrand Gauthier's avatar
Bertrand Gauthier committed
158
159
160
161
162
163
164
        }
        return $this->mapper;
    }

    /**
     * setMapper
     *
165
     * @param LdapPeopleMapper $mapper
Bertrand Gauthier's avatar
Bertrand Gauthier committed
166
167
     * @return Ldap
     */
168
    public function setMapper(LdapPeopleMapper $mapper = null)
Bertrand Gauthier's avatar
Bertrand Gauthier committed
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
    {
        $this->mapper = $mapper;
        return $this;
    }

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

    /**
     * Set service manager instance
     *
     * @param ServiceManager $locator
188
     * @return self
Bertrand Gauthier's avatar
Bertrand Gauthier committed
189
190
191
192
     */
    public function setServiceManager(ServiceManager $serviceManager)
    {
        $this->serviceManager = $serviceManager;
193
        return $this;
Bertrand Gauthier's avatar
Bertrand Gauthier committed
194
195
196
    }

    /**
197
     * @param ModuleOptions $options
Bertrand Gauthier's avatar
Bertrand Gauthier committed
198
     */
199
    public function setOptions(ModuleOptions $options = null)
Bertrand Gauthier's avatar
Bertrand Gauthier committed
200
201
    {
        $this->options = $options;
202
        return $this;
Bertrand Gauthier's avatar
Bertrand Gauthier committed
203
204
205
    }

    /**
206
     * @return ModuleOptions
Bertrand Gauthier's avatar
Bertrand Gauthier committed
207
208
209
     */
    public function getOptions()
    {
210
        if (!$this->options instanceof ModuleOptions) {
211
            $this->setOptions($this->getServiceManager()->get('unicaen-auth_module_options'));
Bertrand Gauthier's avatar
Bertrand Gauthier committed
212
213
214
215
        }
        return $this->options;
    }
}