User.php 6.36 KB
Newer Older
1
2
3
<?php
namespace UnicaenAuth\Service;

4
5
use PDOException;
use UnicaenApp\Exception;
6
use UnicaenApp\Mapper\Ldap\People as LdapPeopleMapper;
7
8
9
use UnicaenAuth\Options\ModuleOptions;
use Zend\ServiceManager\ServiceManager;
use Zend\ServiceManager\ServiceManagerAwareInterface;
10
11
use Zend\EventManager\EventManagerAwareInterface;
use Zend\EventManager\EventManagerInterface;
12
13
use ZfcUser\Authentication\Adapter\AdapterChainEvent as AuthEvent;
use ZfcUser\Options\AuthenticationOptionsInterface;
14
use ZfcUser\Options\ModuleOptions as ZfcUserModuleOptions;
15
16
17
18

/**
 * Service d'enregistrement dans la table des utilisateurs de l'application 
 * de l'utilisateur authentifié avec succès.
19
20
21
 * 
 * Est notifié via la méthode 'userAuthenticated()' lorsque l'authentification 
 * est terminée avec succès.
22
 *
23
 * @see \UnicaenAuth\Authentication\Adapter\AbstractFactory
24
25
 * @author Bertrand GAUTHIER <bertrand.gauthier at unicaen.fr>
 */
26
class User implements ServiceManagerAwareInterface, EventManagerAwareInterface
27
{
28
29
    const EVENT_USER_AUTHENTICATED_PRE_PERSIST = 'userAuthenticated.prePersist';
    
30
31
32
33
34
    /**
     * @var ServiceManager
     */
    protected $serviceManager;

35
36
37
38
39
    /**
     * @var EventManagerInterface
     */
    protected $eventManager;

40
    /**
41
     * @var ModuleOptions
42
43
44
45
     */
    protected $options;

    /**
46
     * @var AuthenticationOptionsInterface
47
48
49
50
     */
    protected $zfcUserOptions;
    
    /**
51
     * @var LdapPeopleMapper
52
     */
53
    protected $ldapPeopleMapper;
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
    
    /**
     * Save authenticated user in database from LDAP data.
     * 
     * @return bool
     */
    public function userAuthenticated(AuthEvent $e)
    {
        if (!$this->getOptions()->getSaveLdapUserInDatabase()) {
            return false;
        }
        if (!($username = $e->getIdentity())) {
            return false;
        }
        
        if (is_int($username)) {
70
            // c'est un id : cela signifie que l'utilisateur existe déjà dans la bdd (et pas dans le LDAP), rien à faire
71
72
73
74
            return true;
        }
        
        if (!is_string($username)) {
75
            throw new Exception("Identité rencontrée inattendue.");
76
77
78
        }
        
        // recherche de l'individu dans l'annuaire LDAP
79
        $ldapPeople = $this->getLdapPeopleMapper()->findOneByUsername($username);
80
81
82
83
84
85
86
87
        if (!$ldapPeople) {
            return false;
        }
        
        // update/insert de l'utilisateur dans la table de l'appli
        $mapper = $this->getServiceManager()->get('zfcuser_user_mapper'); /* @var $mapper \ZfcUser\Mapper\User */
        try {
            $entity = $mapper->findByUsername($username);
88
89
90
91
92
93
94
95
96
97
98
99
100
            if (!$entity) {
                $entityClass = $this->getZfcUserOptions()->getUserEntityClass();
                $entity = new $entityClass;
                $entity->setUsername($username);
                $method = 'insert';
            }
            else {
                $method = 'update';
            }
            $entity->setEmail($ldapPeople->getMail());
            $entity->setDisplayName($ldapPeople->getDisplayName());
            $entity->setPassword('ldap');
            $entity->setState(in_array('deactivated', ldap_explode_dn($ldapPeople->getDn(), 1)) ? 0 : 1);
101
102
103
104
105
106
107
108
            
            // trigger pre-persist event
            $event = new \Zend\EventManager\Event(self::EVENT_USER_AUTHENTICATED_PRE_PERSIST);
            $event->setTarget($this)
                ->setParams(array('entity' => $entity, 'ldapPeople' => $ldapPeople));
            $this->getEventManager()->trigger($event);
            
            // persist
109
110
            $mapper->$method($entity);
        }
111
        catch (PDOException $pdoe) {
112
            throw new Exception("Impossible d'enregistrer l'utilisateur authentifié dans la base de données.", null, $pdoe);
113
        }
114

115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
        return true;
    }
    
    /**
     * Retrieve service manager instance
     *
     * @return ServiceManager
     */
    public function getServiceManager()
    {
        return $this->serviceManager;
    }

    /**
     * Set service manager
     *
     * @param ServiceManager $serviceManager
     */
    public function setServiceManager(ServiceManager $serviceManager)
    {
        $this->serviceManager = $serviceManager;
136
        return $this;
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 the event manager
     *
     * Lazy-loads an EventManager instance if none registered.
     *
     * @return EventManagerInterface
     */
    public function getEventManager()
    {
        return $this->eventManager;
    }
    
    /**
     * Inject an EventManager instance
     *
     * @param  EventManagerInterface $eventManager
     * @return void
     */
    public function setEventManager(EventManagerInterface $eventManager)
    {
        $eventManager->setIdentifiers(array(
            __CLASS__,
            get_called_class(),
        ));
        $this->eventManager = $eventManager;
        return $this;
    }
166
167

    /**
168
     * get ldap people mapper
169
     * 
170
     * @return LdapPeopleMapper
171
     */
172
    public function getLdapPeopleMapper()
173
    {
174
175
        if (null === $this->ldapPeopleMapper) {
            $this->ldapPeopleMapper = $this->getServiceManager()->get('ldap_people_mapper');
176
        }
177
        return $this->ldapPeopleMapper;
178
179
180
    }

    /**
181
     * set ldap people mapper
182
     *  
183
     * @param LdapPeopleMapper $mapper
184
185
     * @return User
     */
186
    public function setLdapPeopleMapper(LdapPeopleMapper $mapper)
187
    {
188
        $this->ldapPeopleMapper = $mapper;
189
190
191
192
        return $this;
    }

    /**
193
     * @param ModuleOptions $options
194
     */
195
    public function setOptions(ModuleOptions $options)
196
197
    {
        $this->options = $options;
198
        return $this;
199
200
201
    }

    /**
202
     * @return ModuleOptions
203
204
205
     */
    public function getOptions()
    {
206
        if (!$this->options instanceof ModuleOptions) {
207
            $this->setOptions($this->getServiceManager()->get('unicaen-auth_module_options'));
208
209
210
211
212
        }
        return $this->options;
    }

    /**
213
     * @param ZfcUserModuleOptions $options
214
     */
215
    public function setZfcUserOptions(ZfcUserModuleOptions $options)
216
217
    {
        $this->zfcUserOptions = $options;
218
        return $this;
219
220
221
    }

    /**
222
     * @return ZfcUserModuleOptions
223
224
225
     */
    public function getZfcUserOptions()
    {
226
        if (!$this->zfcUserOptions instanceof ZfcUserModuleOptions) {
227
228
229
230
            $this->setZfcUserOptions($this->getServiceManager()->get('zfcuser_module_options'));
        }
        return $this->zfcUserOptions;
    }
231
}