ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
ilLDAPAttributeToUser Class Reference

Update/create ILIAS user account by given LDAP attributes according to user attribute mapping settings. More...

+ Collaboration diagram for ilLDAPAttributeToUser:

Public Member Functions

 __construct (ilLDAPServer $a_server)
 Construct of ilLDAPAttribute2XML Defines between LDAP and ILIAS user attributes. More...
 
 getServer ()
 Get server settings. More...
 
 setUserData (array $a_data)
 Set user data received from pear auth or by ldap_search. More...
 
 setNewUserAuthMode (string $a_authmode)
 Set auth mode for new users. More...
 
 getNewUserAuthMode ()
 Get auth mode for new users. More...
 
 addMode (int $a_mode)
 Add import mode. More...
 
 isModeActive (int $a_mode)
 Check if mode is active. More...
 
 refresh ()
 Create/Update non existing users. More...
 

Data Fields

const MODE_INITIALIZE_ROLES = 1
 

Protected Member Functions

 parseRoleAssignmentsForUpdate (int $a_usr_id, string $a_external_account, array $user)
 Parse role assignments for update of user account. More...
 
 parseRoleAssignmentsForCreation (string $a_external_account, array $a_user)
 Parse role assignments for update of user account. More...
 

Private Member Functions

 usersToXML ()
 Create xml string of user according to mapping rules. More...
 
 convertInput ($a_value)
 A value can be an array or a string This function converts arrays to strings. More...
 
 doMapping (array $user, array $rule)
 
 initLDAPAttributeMapping ()
 
 initUserDefinedFields ()
 

Private Attributes

array $modes = []
 
ilLDAPServer $server_settings
 
Profile $profile
 
array $user_data = []
 
ilLDAPAttributeMapping $mapping
 
string $new_user_auth_mode = 'ldap'
 
ilLogger $logger
 
ilXmlWriter $writer
 
array $user_defined_fields = null
 

Detailed Description

Update/create ILIAS user account by given LDAP attributes according to user attribute mapping settings.

Author
Stefan Meyer meyer.nosp@m.@lei.nosp@m.fos.c.nosp@m.om

Definition at line 29 of file class.ilLDAPAttributeToUser.php.

Constructor & Destructor Documentation

◆ __construct()

ilLDAPAttributeToUser::__construct ( ilLDAPServer  $a_server)

Construct of ilLDAPAttribute2XML Defines between LDAP and ILIAS user attributes.

Definition at line 50 of file class.ilLDAPAttributeToUser.php.

51 {
52 global $DIC;
53
54 $this->logger = $DIC->logger()->auth();
55 $this->profile = $DIC['user']->profile();
56
57 $this->server_settings = $a_server;
58
60 }
global $DIC
Definition: shib_login.php:26

References $DIC, initLDAPAttributeMapping(), ILIAS\Repository\logger(), and ILIAS\Repository\profile().

+ Here is the call graph for this function:

Member Function Documentation

◆ addMode()

ilLDAPAttributeToUser::addMode ( int  $a_mode)

Add import mode.

Definition at line 102 of file class.ilLDAPAttributeToUser.php.

102 : void
103 {
104 //TODO check for proper value
105 if (!in_array($a_mode, $this->modes, true)) {
106 $this->modes[] = $a_mode;
107 }
108 }

◆ convertInput()

ilLDAPAttributeToUser::convertInput (   $a_value)
private

A value can be an array or a string This function converts arrays to strings.

Parameters
array|stringvalue
Returns
string

Definition at line 389 of file class.ilLDAPAttributeToUser.php.

389 : string
390 {
391 if (is_array($a_value)) {
392 return $a_value[0];
393 }
394
395 return $a_value;
396 }

Referenced by doMapping().

+ Here is the caller graph for this function:

◆ doMapping()

ilLDAPAttributeToUser::doMapping ( array  $user,
array  $rule 
)
private

Definition at line 398 of file class.ilLDAPAttributeToUser.php.

398 : string
399 {
400 $mapping = strtolower(trim($rule['value']));
401
402 if (strpos($mapping, ',') === false) {
403 return $this->convertInput($user[$mapping] ?? '');
404 }
405 // Is multiple mapping
406
407 $fields = explode(',', $mapping);
408 $value = '';
409 foreach ($fields as $field) {
410 if ($value !== '') {
411 $value .= ' ';
412 }
413 $value .= ($this->convertInput($user[trim($field)] ?? ''));
414 }
415 return $value;
416 }
convertInput($a_value)
A value can be an array or a string This function converts arrays to strings.
ilLDAPAttributeMapping $mapping

References $mapping, and convertInput().

Referenced by usersToXML().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getNewUserAuthMode()

ilLDAPAttributeToUser::getNewUserAuthMode ( )

Get auth mode for new users.

Definition at line 94 of file class.ilLDAPAttributeToUser.php.

94 : string
95 {
97 }

References $new_user_auth_mode.

Referenced by usersToXML().

+ Here is the caller graph for this function:

◆ getServer()

ilLDAPAttributeToUser::getServer ( )

Get server settings.

Returns
ilLDAPServer

Definition at line 66 of file class.ilLDAPAttributeToUser.php.

References $server_settings.

Referenced by parseRoleAssignmentsForCreation(), parseRoleAssignmentsForUpdate(), and refresh().

+ Here is the caller graph for this function:

◆ initLDAPAttributeMapping()

ilLDAPAttributeToUser::initLDAPAttributeMapping ( )
private

Definition at line 418 of file class.ilLDAPAttributeToUser.php.

418 : void
419 {
420 $this->mapping = ilLDAPAttributeMapping::_getInstanceByServerId($this->server_settings->getServerId());
421 }
static _getInstanceByServerId(int $a_server_id)

References ilLDAPAttributeMapping\_getInstanceByServerId().

Referenced by __construct().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ initUserDefinedFields()

ilLDAPAttributeToUser::initUserDefinedFields ( )
private

Definition at line 423 of file class.ilLDAPAttributeToUser.php.

423 : void
424 {
425 if ($this->user_defined_fields === null) {
426 $this->user_defined_fields = $this->profile->getAllUserDefinedFields();
427 }
428 }

References ILIAS\Repository\profile().

Referenced by usersToXML().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ isModeActive()

ilLDAPAttributeToUser::isModeActive ( int  $a_mode)

Check if mode is active.

Parameters
int$a_mode
Returns
bool

Definition at line 115 of file class.ilLDAPAttributeToUser.php.

115 : bool
116 {
117 return in_array($a_mode, $this->modes, true);
118 }

Referenced by usersToXML().

+ Here is the caller graph for this function:

◆ parseRoleAssignmentsForCreation()

ilLDAPAttributeToUser::parseRoleAssignmentsForCreation ( string  $a_external_account,
array  $a_user 
)
protected

Parse role assignments for update of user account.

Parameters
string$a_external_account
array$a_user

Definition at line 168 of file class.ilLDAPAttributeToUser.php.

168 : void
169 {
171 $this->getServer()->getServerId(),
172 $a_external_account,
173 $a_user
174 ) as $role_data) {
175 $this->writer->xmlElement(
176 'Role',
177 [
178 'Id' => $role_data['id'],
179 'Type' => $role_data['type'],
180 'Action' => $role_data['action']
181 ],
182 ''
183 );
184 }
185 }
static getAssignmentsForCreation(int $a_server_id, string $a_usr_name, array $a_usr_data)

References ilLDAPRoleAssignmentRules\getAssignmentsForCreation(), and getServer().

Referenced by usersToXML().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ parseRoleAssignmentsForUpdate()

ilLDAPAttributeToUser::parseRoleAssignmentsForUpdate ( int  $a_usr_id,
string  $a_external_account,
array  $user 
)
protected

Parse role assignments for update of user account.

Parameters
int$a_usr_id
string$a_external_account
array$user

Definition at line 143 of file class.ilLDAPAttributeToUser.php.

143 : void
144 {
146 $this->getServer()->getServerId(),
147 $a_usr_id,
148 $a_external_account,
149 $user
150 ) as $role_data) {
151 $this->writer->xmlElement(
152 'Role',
153 [
154 'Id' => $role_data['id'],
155 'Type' => $role_data['type'],
156 'Action' => $role_data['action']
157 ],
158 ''
159 );
160 }
161 }
static getAssignmentsForUpdate(int $a_server_id, $a_usr_id, $a_usr_name, $a_usr_data)

References ilLDAPRoleAssignmentRules\getAssignmentsForUpdate(), and getServer().

Referenced by usersToXML().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ refresh()

ilLDAPAttributeToUser::refresh ( )

Create/Update non existing users.

Definition at line 124 of file class.ilLDAPAttributeToUser.php.

124 : bool
125 {
126 $this->usersToXML();
127
128 $importParser = new ilUserImportParser();
129 $importParser->setXMLContent($this->writer->xmlDumpMem(false));
130 $importParser->setRoleAssignment(ilLDAPRoleAssignmentRules::getAllPossibleRoles($this->getServer()->getServerId()));
131 $importParser->setFolderId(7);
132 $importParser->startParsing();
133
134 return true;
135 }
usersToXML()
Create xml string of user according to mapping rules.
static getAllPossibleRoles(int $a_server_id)
Get all assignable roles (used for import parser)

References ilLDAPRoleAssignmentRules\getAllPossibleRoles(), getServer(), and usersToXML().

+ Here is the call graph for this function:

◆ setNewUserAuthMode()

ilLDAPAttributeToUser::setNewUserAuthMode ( string  $a_authmode)

Set auth mode for new users.

Parameters
string$a_authmode

Definition at line 86 of file class.ilLDAPAttributeToUser.php.

86 : void
87 {
88 $this->new_user_auth_mode = $a_authmode;
89 }

◆ setUserData()

ilLDAPAttributeToUser::setUserData ( array  $a_data)

Set user data received from pear auth or by ldap_search.

Parameters
arrayarray of auth data. array('ilias_account1' => array(firstname => 'Stefan',...),...)

Definition at line 77 of file class.ilLDAPAttributeToUser.php.

77 : void
78 {
79 $this->user_data = $a_data;
80 }

◆ usersToXML()

ilLDAPAttributeToUser::usersToXML ( )
private

Create xml string of user according to mapping rules.

Definition at line 190 of file class.ilLDAPAttributeToUser.php.

190 : void
191 {
192 $this->writer = new ilXmlWriter();
193 $this->writer->xmlStartTag('Users');
194
195 $cnt_update = 0;
196 $cnt_create = 0;
197
198 // Single users
199 foreach ($this->user_data as $external_account => $user) {
200 $external_account = (string) $external_account;
201
202 $user['ilExternalAccount'] = $external_account;
203
204 // Required fields
205 if ($user['ilInternalAccount']) {
206 $usr_id = ilObjUser::_lookupId($user['ilInternalAccount']);
207
208 ++$cnt_update;
209 // User exists
210 $this->writer->xmlStartTag('User', ['Id' => $usr_id, 'Action' => 'Update']);
211 $this->writer->xmlElement('Login', [], $user['ilInternalAccount']);
212 $this->writer->xmlElement('ExternalAccount', [], $external_account);
213 $this->writer->xmlElement('AuthMode', ['type' => $this->getNewUserAuthMode()]);
214
215 if ($this->isModeActive(self::MODE_INITIALIZE_ROLES)) {
216 $this->parseRoleAssignmentsForCreation($external_account, $user);
217 } else {
218 $this->parseRoleAssignmentsForUpdate($usr_id, $external_account, $user);
219 }
220 $rules = $this->mapping->getRulesForUpdate();
221 } else {
222 ++$cnt_create;
223 // Create user
224 $this->writer->xmlStartTag('User', ['Action' => 'Insert']);
225 $this->writer->xmlElement('Login', [], ilAuthUtils::_generateLogin($external_account));
226
227 $this->parseRoleAssignmentsForCreation($external_account, $user);
228 $rules = $this->mapping->getRules(true);
229 }
230
231 $this->writer->xmlElement('Active', [], "true");
232 $this->writer->xmlElement('TimeLimitOwner', [], 7);
233 $this->writer->xmlElement('TimeLimitUnlimited', [], 1);
234 $this->writer->xmlElement('TimeLimitFrom', [], time());
235 $this->writer->xmlElement('TimeLimitUntil', [], time());
236
237 // only for new users.
238 // If auth_mode is 'default' (ldap) this status should remain.
239 if (!$user['ilInternalAccount']) {
240 $this->writer->xmlElement(
241 'AuthMode',
242 ['type' => $this->getNewUserAuthMode()],
243 $this->getNewUserAuthMode()
244 );
245 $this->writer->xmlElement('ExternalAccount', [], $external_account);
246 }
247 foreach ($rules as $field => $data) {
248 // Do Mapping: it is possible to assign multiple ldap attribute to one user data field
249 if (!($value = $this->doMapping($user, $data))) {
250 continue;
251 }
252
253 switch ($field) {
254 case 'gender':
255 switch (strtolower($value)) {
256 case 'm':
257 case 'male':
258 $this->writer->xmlElement('Gender', [], 'm');
259 break;
260
261 case 'f':
262 case 'female':
263 $this->writer->xmlElement('Gender', [], 'f');
264 break;
265
266 default:
267 // use the default for anything that is not clearly m or f
268 $this->writer->xmlElement('Gender', [], 'n');
269 break;
270 }
271 break;
272
273 case 'firstname':
274 $this->writer->xmlElement('Firstname', [], $value);
275 break;
276
277 case 'lastname':
278 $this->writer->xmlElement('Lastname', [], $value);
279 break;
280
281 case 'hobby':
282 $this->writer->xmlElement('Hobby', [], $value);
283 break;
284
285 case 'title':
286 $this->writer->xmlElement('Title', [], $value);
287 break;
288
289 case 'institution':
290 $this->writer->xmlElement('Institution', [], $value);
291 break;
292
293 case 'department':
294 $this->writer->xmlElement('Department', [], $value);
295 break;
296
297 case 'street':
298 $this->writer->xmlElement('Street', [], $value);
299 break;
300
301 case 'city':
302 $this->writer->xmlElement('City', [], $value);
303 break;
304
305 case 'zipcode':
306 $this->writer->xmlElement('PostalCode', [], $value);
307 break;
308
309 case 'country':
310 $this->writer->xmlElement('Country', [], $value);
311 break;
312
313 case 'phone_office':
314 $this->writer->xmlElement('PhoneOffice', [], $value);
315 break;
316
317 case 'phone_home':
318 $this->writer->xmlElement('PhoneHome', [], $value);
319 break;
320
321 case 'phone_mobile':
322 $this->writer->xmlElement('PhoneMobile', [], $value);
323 break;
324
325 case 'fax':
326 $this->writer->xmlElement('Fax', [], $value);
327 break;
328
329 case 'email':
330 $this->writer->xmlElement('Email', [], $value);
331 break;
332
333 case 'second_email':
334 $this->writer->xmlElement('SecondEmail', [], $value);
335 break;
336
337 case 'matriculation':
338 $this->writer->xmlElement('Matriculation', [], $value);
339 break;
340
341 default:
342 // Handle user defined fields
343 if (strpos($field, 'udf_') !== 0) {
344 continue 2;
345 }
346 $id_data = explode('_', $field);
347 if (!isset($id_data[1])) {
348 continue 2;
349 }
350 $this->initUserDefinedFields();
351 if (!isset($this->user_defined_fields[$id_data[1]])) {
352 $this->logger->warning(sprintf(
353 "Invalid/Orphaned UD field mapping detected: %s",
354 $field
355 ));
356 break;
357 }
358
359 $this->writer->xmlElement(
360 'UserDefinedField',
361 [
362 'Id' => $this->user_defined_fields[$id_data[1]]->getIdentifier(),
363 'Name' => $this->user_defined_fields[$id_data[1]]->getLabel()
364 ],
365 $value
366 );
367 break;
368 }
369 }
370 $this->writer->xmlEndTag('User');
371 }
372
373 if ($cnt_create) {
374 $this->logger->info('LDAP: Started creation of ' . $cnt_create . ' users.');
375 }
376 if ($cnt_update) {
377 $this->logger->info('LDAP: Started update of ' . $cnt_update . ' users.');
378 }
379 $this->writer->xmlEndTag('Users');
380 }
static _generateLogin(string $a_login)
generate free login by starting with a default string and adding postfix numbers
parseRoleAssignmentsForCreation(string $a_external_account, array $a_user)
Parse role assignments for update of user account.
parseRoleAssignmentsForUpdate(int $a_usr_id, string $a_external_account, array $user)
Parse role assignments for update of user account.
getNewUserAuthMode()
Get auth mode for new users.
doMapping(array $user, array $rule)
isModeActive(int $a_mode)
Check if mode is active.
static _lookupId(string|array $a_user_str)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...

References $data, ilAuthUtils\_generateLogin(), ilObjUser\_lookupId(), doMapping(), ILIAS\Test\Questions\Presentation\getLabel, getNewUserAuthMode(), initUserDefinedFields(), isModeActive(), ILIAS\Repository\logger(), parseRoleAssignmentsForCreation(), and parseRoleAssignmentsForUpdate().

Referenced by refresh().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Field Documentation

◆ $logger

ilLogger ilLDAPAttributeToUser::$logger
private

Definition at line 39 of file class.ilLDAPAttributeToUser.php.

◆ $mapping

ilLDAPAttributeMapping ilLDAPAttributeToUser::$mapping
private

Definition at line 37 of file class.ilLDAPAttributeToUser.php.

Referenced by doMapping().

◆ $modes

array ilLDAPAttributeToUser::$modes = []
private

Definition at line 33 of file class.ilLDAPAttributeToUser.php.

◆ $new_user_auth_mode

string ilLDAPAttributeToUser::$new_user_auth_mode = 'ldap'
private

Definition at line 38 of file class.ilLDAPAttributeToUser.php.

Referenced by getNewUserAuthMode().

◆ $profile

Profile ilLDAPAttributeToUser::$profile
private

Definition at line 35 of file class.ilLDAPAttributeToUser.php.

◆ $server_settings

ilLDAPServer ilLDAPAttributeToUser::$server_settings
private

Definition at line 34 of file class.ilLDAPAttributeToUser.php.

Referenced by getServer().

◆ $user_data

array ilLDAPAttributeToUser::$user_data = []
private

Definition at line 36 of file class.ilLDAPAttributeToUser.php.

◆ $user_defined_fields

array ilLDAPAttributeToUser::$user_defined_fields = null
private

Definition at line 44 of file class.ilLDAPAttributeToUser.php.

◆ $writer

ilXmlWriter ilLDAPAttributeToUser::$writer
private

Definition at line 40 of file class.ilLDAPAttributeToUser.php.

◆ MODE_INITIALIZE_ROLES

const ilLDAPAttributeToUser::MODE_INITIALIZE_ROLES = 1

The documentation for this class was generated from the following file: