User.php 6.55 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
use UnicaenAuth\Options\ModuleOptions;
8
use UnicaenAuth\Event\UserAuthenticatedEvent;
9
10
use Zend\ServiceManager\ServiceManager;
use Zend\ServiceManager\ServiceManagerAwareInterface;
11
12
use Zend\EventManager\EventManagerAwareInterface;
use Zend\EventManager\EventManagerInterface;
13
14
use ZfcUser\Authentication\Adapter\AdapterChainEvent as AuthEvent;
use ZfcUser\Options\AuthenticationOptionsInterface;
15
use ZfcUser\Options\ModuleOptions as ZfcUserModuleOptions;
16
17
18
19

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

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

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

    /**
47
     * @var AuthenticationOptionsInterface
48
49
50
51
     */
    protected $zfcUserOptions;
    
    /**
52
     * @var LdapPeopleMapper
53
     */
54
    protected $ldapPeopleMapper;
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
    
    /**
     * 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)) {
71
            // c'est un id : cela signifie que l'utilisateur existe déjà dans la bdd (et pas dans le LDAP), rien à faire
72
73
74
75
            return true;
        }
        
        if (!is_string($username)) {
76
            throw new Exception("Identité rencontrée inattendue.");
77
78
79
        }
        
        // recherche de l'individu dans l'annuaire LDAP
80
        $ldapPeople = $this->getLdapPeopleMapper()->findOneByUsername($username);
81
82
83
84
85
86
87
88
        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);
89
90
91
92
93
94
95
96
97
98
99
100
101
            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);
102
            
103
104
105
106
107
108
109
            // déclenche l'événement donnant aux applications clientes l'opportunité de modifier l'entité 
            // utilisateur avant qu'elle ne soit persistée
            $event = new UserAuthenticatedEvent(UserAuthenticatedEvent::PRE_PERSIST);
            $event
                    ->setDbUser($entity)
                    ->setLdapUser($ldapPeople)
                    ->setTarget($this);
110
111
112
            $this->getEventManager()->trigger($event);
            
            // persist
113
114
            $mapper->$method($entity);
        }
115
        catch (PDOException $pdoe) {
116
            throw new Exception("Impossible d'enregistrer l'utilisateur authentifié dans la base de données.", null, $pdoe);
117
        }
118

119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
        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;
140
        return $this;
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
166
167
168
169
    
    /**
     * 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;
    }
170
171

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

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

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

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

    /**
217
     * @param ZfcUserModuleOptions $options
218
     */
219
    public function setZfcUserOptions(ZfcUserModuleOptions $options)
220
221
    {
        $this->zfcUserOptions = $options;
222
        return $this;
223
224
225
    }

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