User.php 5.08 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
        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);
79
80
81
82
83
84
85
86
87
88
89
90
91
            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);
92
93
            $mapper->$method($entity);
        }
94
        catch (PDOException $pdoe) {
95
            throw new Exception("Impossible d'enregistrer l'utilisateur authentifié dans la base de données.", null, $pdoe);
96
        }
97

98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
        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;
119
        return $this;
120
121
122
    }

    /**
123
     * get ldap people mapper
124
     * 
125
     * @return LdapPeopleMapper
126
     */
127
    public function getLdapPeopleMapper()
128
    {
129
130
        if (null === $this->ldapPeopleMapper) {
            $this->ldapPeopleMapper = $this->getServiceManager()->get('ldap_people_mapper');
131
        }
132
        return $this->ldapPeopleMapper;
133
134
135
    }

    /**
136
     * set ldap people mapper
137
     *  
138
     * @param LdapPeopleMapper $mapper
139
140
     * @return User
     */
141
    public function setLdapPeopleMapper(LdapPeopleMapper $mapper)
142
    {
143
        $this->ldapPeopleMapper = $mapper;
144
145
146
147
        return $this;
    }

    /**
148
     * @param ModuleOptions $options
149
     */
150
    public function setOptions(ModuleOptions $options)
151
152
    {
        $this->options = $options;
153
        return $this;
154
155
156
    }

    /**
157
     * @return ModuleOptions
158
159
160
     */
    public function getOptions()
    {
161
        if (!$this->options instanceof ModuleOptions) {
162
            $this->setOptions($this->getServiceManager()->get('unicaen-auth_module_options'));
163
164
165
166
167
        }
        return $this->options;
    }

    /**
168
     * @param ZfcUserModuleOptions $options
169
     */
170
    public function setZfcUserOptions(ZfcUserModuleOptions $options)
171
172
    {
        $this->zfcUserOptions = $options;
173
        return $this;
174
175
176
    }

    /**
177
     * @return ZfcUserModuleOptions
178
179
180
     */
    public function getZfcUserOptions()
    {
181
        if (!$this->zfcUserOptions instanceof ZfcUserModuleOptions) {
182
183
184
185
            $this->setZfcUserOptions($this->getServiceManager()->get('zfcuser_module_options'));
        }
        return $this->zfcUserOptions;
    }
186
}