ILIAS  release_7 Revision v7.30-3-g800a261c036
class.ilLDAPUserSynchronisation.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4include_once './Services/LDAP/classes/class.ilLDAPServer.php';
5include_once './Services/LDAP/exceptions/class.ilLDAPSynchronisationForbiddenException.php';
6include_once './Services/LDAP/exceptions/class.ilLDAPAccountMigrationRequiredException.php';
7
15{
16 private $authmode = 0;
17
18 private $server = null;
19
20 private $extaccount = '';
21 private $intaccount = '';
22
23 private $user_data = array();
24
25 private $force_creation = false;
26 private $force_read_ldap_data = false;
27
28 private $logger;
29
30
36 public function __construct($a_authmode, $a_server_id)
37 {
38 global $DIC;
39
40 $this->logger = $DIC->logger()->auth();
41 $this->initServer($a_authmode, $a_server_id);
42 }
43
48 public function getServer()
49 {
50 return $this->server;
51 }
52
57 public function getAuthMode()
58 {
59 return $this->authmode;
60 }
61
66 public function setExternalAccount($a_ext)
67 {
68 $this->extaccount = $a_ext;
69 }
70
75 public function getExternalAccount()
76 {
77 return $this->extaccount;
78 }
79
84 public function getInternalAccount()
85 {
86 return $this->intaccount;
87 }
88
93 public function forceCreation($a_force)
94 {
95 $this->force_creation = $a_force;
96 }
97
98 public function forceReadLdapData($a_status)
99 {
100 $this->force_read_ldap_data = $a_status;
101 }
102
107 public function isCreationForced()
108 {
109 return (bool) $this->force_creation;
110 }
111
116 public function getUserData()
117 {
118 return (array) $this->user_data;
119 }
120
125 public function setUserData($a_data)
126 {
127 $this->user_data = (array) $a_data;
128 }
129
137 public function sync()
138 {
139 $this->readInternalAccount();
140
141 if (!$this->getInternalAccount()) {
142 ilLoggerFactory::getLogger('auth')->debug('Creating new account');
143 $this->handleCreation();
144 }
145
146 // Nothing to do if sync on login is disabled
147 if (!$this->getServer()->enabledSyncOnLogin()) {
148 return $this->getInternalAccount();
149 }
150
151 // For performance reasons, check if (an update is required)
152 if ($this->isUpdateRequired()) {
153 ilLoggerFactory::getLogger('auth')->debug('Perform update of user data');
154 $this->readUserData();
155 $this->performUpdate();
156 }
157 return $this->getInternalAccount();
158 }
159
165 protected function handleCreation()
166 {
167 // Disabled sync on login
168 if (!$this->getServer()->enabledSyncOnLogin()) {
169 throw new ilLDAPSynchronisationForbiddenException('User synchronisation forbidden.');
170 }
171 // Account migration
172 if ($this->getServer()->isAccountMigrationEnabled() and !$this->isCreationForced()) {
173 $this->readUserData();
174 throw new ilLDAPAccountMigrationRequiredException('Account migration check required.');
175 }
176 }
177
182 protected function performUpdate()
183 {
184 include_once './Services/User/classes/class.ilUserCreationContext.php';
186
187 include_once 'Services/LDAP/classes/class.ilLDAPAttributeToUser.php';
188 $update = new ilLDAPAttributeToUser($this->getServer());
189 if ($this->isCreationForced()) {
191 }
192 $update->setNewUserAuthMode($this->getAuthMode());
193 $update->setUserData(
194 array(
195 $this->getExternalAccount() => $this->getUserData()
196 )
197 );
198
199 $update->refresh();
200
201 // User has been created, now read internal account again
202 $this->readInternalAccount();
203 return true;
204 }
205
211 protected function readUserData() : bool
212 {
213 // Add internal account to user data
214 $this->user_data['ilInternalAccount'] = $this->getInternalAccount();
215 if (!$this->force_read_ldap_data && strpos($this->getAuthMode(), 'ldap') === 0) {
216 return true;
217 }
218
219 try {
220 $query = new ilLDAPQuery($this->getServer());
222 $user = $query->fetchUser($this->getExternalAccount());
223 $this->logger->dump($user, ilLogLevel::DEBUG);
224 $this->user_data = (array) $user[strtolower($this->getExternalAccount())];
225 } catch (ilLDAPQueryException $e) {
226 $this->logger->error('LDAP bind failed with message: ' . $e->getMessage());
227 throw new ilLDAPSynchronisationFailedException($e->getMessage());
228 }
229
230 return true;
231 }
232
233
238 protected function readInternalAccount()
239 {
240 if (!$this->getExternalAccount()) {
241 throw new UnexpectedValueException('No external account given.');
242 }
243 $this->intaccount = ilObjUser::_checkExternalAuthAccount(
244 $this->getAuthMode(),
245 $this->getExternalAccount()
246 );
247 }
248
253 protected function isUpdateRequired()
254 {
255 if ($this->isCreationForced()) {
256 return true;
257 }
258 if (!$this->getInternalAccount()) {
259 return true;
260 }
261
262 // Check attribute mapping on login
263 include_once './Services/LDAP/classes/class.ilLDAPAttributeMapping.php';
264 if (ilLDAPAttributeMapping::hasRulesForUpdate($this->getServer()->getServerId())) {
265 return true;
266 }
267
268 // Check if there is any change in role assignments
269 include_once './Services/LDAP/classes/class.ilLDAPRoleAssignmentRule.php';
271 return true;
272 }
273 return false;
274 }
275
276
281 protected function initServer($a_auth_mode, $a_server_id)
282 {
283 $this->authmode = $a_auth_mode;
284 $this->server = ilLDAPServer::getInstanceByServerId($a_server_id);
285 }
286}
An exception for terminatinating execution or to throw for unit testing.
Description of ilLDAPAccountMigrationRequiredException.
static hasRulesForUpdate($a_server_id)
Check if there is ldap attribute -> user data mapping which which is updated on login.
Update/create ILIAS user account by given LDAP attributes according to user attribute mapping setting...
static hasRulesForUpdate()
Check if there any rule for updates.
static getInstanceByServerId($a_server_id)
Get instance by server id.
Thrown in case of failed synchronisation settings.
Synchronization of user accounts used in auth container ldap, radius , cas,...
getInternalAccount()
Get ILIAS unique internal account name.
isCreationForced()
Check if creation of user account is forced (account migration)
forceCreation($a_force)
Force cration of user accounts (Account migration enabled)
performUpdate()
Update user account and role assignments.
initServer($a_auth_mode, $a_server_id)
Init LDAP server.
isUpdateRequired()
Check if an update is required.
handleCreation()
Handle creation of user accounts.
readInternalAccount()
Read internal account of user.
__construct($a_authmode, $a_server_id)
Constructor.
setExternalAccount($a_ext)
Set external account (unique for each auth mode)
static getLogger($a_component_id)
Get component logger.
static _checkExternalAuthAccount($a_auth, $a_account, $tryFallback=true)
check whether external account and authentication method matches with a user
static getInstance()
Get instance.
global $DIC
Definition: goto.php:24
$query