User.php 5.46 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
10
11
use UnicaenAuth\Options\ModuleOptions;
use Zend\ServiceManager\ServiceManager;
use Zend\ServiceManager\ServiceManagerAwareInterface;
use ZfcUser\Authentication\Adapter\AdapterChainEvent as AuthEvent;
use ZfcUser\Options\AuthenticationOptionsInterface;
12
use ZfcUser\Options\ModuleOptions as ZfcUserModuleOptions;
13
14
15
16

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

    /**
32
     * @var ModuleOptions
33
34
35
36
     */
    protected $options;

    /**
37
     * @var AuthenticationOptionsInterface
38
39
40
41
     */
    protected $zfcUserOptions;
    
    /**
42
     * @var LdapPeopleMapper
43
     */
44
    protected $ldapPeopleMapper;
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
    
    /**
     * 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)) {
61
            // c'est un id : cela signifie que l'utilisateur existe déjà dans la bdd (et pas dans le LDAP), rien à faire
62
63
64
65
            return true;
        }
        
        if (!is_string($username)) {
66
            throw new Exception("Identité rencontrée inattendue.");
67
68
69
        }
        
        // recherche de l'individu dans l'annuaire LDAP
70
        $ldapPeople = $this->getLdapPeopleMapper()->findOneByUsername($username);
71
72
73
74
75
76
77
78
79
        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);
        }
80
81
        catch (PDOException $pdoe) {
            throw new Exception(
82
83
84
                    "Erreur lors de la recherche de l'utilisateur '$username' dans la base de données : " . $pdoe->getMessage(),
                    null,
                    $pdoe);
85
//            return true;
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
        }
        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);
        try {
            $mapper->$method($entity);
        }
103
104
        catch (PDOException $pdoe) {
            throw new Exception(
105
106
107
                    "Erreur lors de l'enregistrement de l'utilisateur '$username' dans la base de données : " . $pdoe->getMessage(),
                    null,
                    $pdoe);
108
//            return true;
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
        }
        
        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;
132
        return $this;
133
134
135
    }

    /**
136
     * get ldap people mapper
137
     * 
138
     * @return LdapPeopleMapper
139
     */
140
    public function getLdapPeopleMapper()
141
    {
142
143
        if (null === $this->ldapPeopleMapper) {
            $this->ldapPeopleMapper = $this->getServiceManager()->get('ldap_people_mapper');
144
        }
145
        return $this->ldapPeopleMapper;
146
147
148
    }

    /**
149
     * set ldap people mapper
150
     *  
151
     * @param LdapPeopleMapper $mapper
152
153
     * @return User
     */
154
    public function setLdapPeopleMapper(LdapPeopleMapper $mapper)
155
    {
156
        $this->ldapPeopleMapper = $mapper;
157
158
159
160
        return $this;
    }

    /**
161
     * @param ModuleOptions $options
162
     */
163
    public function setOptions(ModuleOptions $options)
164
165
    {
        $this->options = $options;
166
        return $this;
167
168
169
    }

    /**
170
     * @return ModuleOptions
171
172
173
     */
    public function getOptions()
    {
174
        if (!$this->options instanceof ModuleOptions) {
175
            $this->setOptions($this->getServiceManager()->get('unicaen-auth_module_options'));
176
177
178
179
180
        }
        return $this->options;
    }

    /**
181
     * @param ZfcUserModuleOptions $options
182
     */
183
    public function setZfcUserOptions(ZfcUserModuleOptions $options)
184
185
    {
        $this->zfcUserOptions = $options;
186
        return $this;
187
188
189
    }

    /**
190
     * @return ZfcUserModuleOptions
191
192
193
     */
    public function getZfcUserOptions()
    {
194
        if (!$this->zfcUserOptions instanceof ZfcUserModuleOptions) {
195
196
197
198
            $this->setZfcUserOptions($this->getServiceManager()->get('zfcuser_module_options'));
        }
        return $this->zfcUserOptions;
    }
199
}