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

4
5
use PDOException;
use UnicaenApp\Exception;
6
use UnicaenApp\Service\Ldap\People as LdapPeopleService;
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
13
14
15
16
17
18

/**
 * Service d'enregistrement dans la table des utilisateurs de l'application 
 * de l'utilisateur authentifié avec succès.
 *
 * @author Bertrand GAUTHIER <bertrand.gauthier at unicaen.fr>
 */
19
class User implements ServiceManagerAwareInterface
20
21
22
23
24
25
26
{
    /**
     * @var ServiceManager
     */
    protected $serviceManager;

    /**
27
     * @var ModuleOptions
28
29
30
31
     */
    protected $options;

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

    /**
     * get ldap people service
     * 
     * @return LdapPeopleService
     */
    public function getLdapPeopleService()
    {
        if (null === $this->ldapPeopleService) {
            $this->ldapPeopleService = $this->getServiceManager()->get('ldap_people_service');
        }
        return $this->ldapPeopleService;
    }

    /**
     * set ldap people service
     *  
     * @param LdapPeopleService $service
     * @return User
     */
    public function setLdapPeopleService(LdapPeopleService $service)
    {
        $this->ldapPeopleService = $service;
        return $this;
    }

    /**
155
     * @param ModuleOptions $options
156
     */
157
    public function setOptions(ModuleOptions $options)
158
159
160
161
162
    {
        $this->options = $options;
    }

    /**
163
     * @return ModuleOptions
164
165
166
     */
    public function getOptions()
    {
167
        if (!$this->options instanceof ModuleOptions) {
168
            $this->setOptions($this->getServiceManager()->get('unicaen-auth_module_options'));
169
170
171
172
173
        }
        return $this->options;
    }

    /**
174
     * @param AuthenticationOptionsInterface $options
175
     */
176
    public function setZfcUserOptions(AuthenticationOptionsInterface $options)
177
178
179
180
181
    {
        $this->zfcUserOptions = $options;
    }

    /**
182
     * @return AuthenticationOptionsInterface
183
184
185
     */
    public function getZfcUserOptions()
    {
186
        if (!$this->zfcUserOptions instanceof AuthenticationOptionsInterface) {
187
188
189
190
191
            $this->setZfcUserOptions($this->getServiceManager()->get('zfcuser_module_options'));
        }
        return $this->zfcUserOptions;
    }
}