ILIAS  release_7 Revision v7.30-3-g800a261c036
class.ilLDAPCronSynchronization.php
Go to the documentation of this file.
1<?php
2
3/* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
4
5include_once "Services/Cron/classes/class.ilCronJob.php";
6include_once('Services/LDAP/classes/class.ilLDAPServer.php');
7include_once('Services/LDAP/classes/class.ilLDAPQuery.php');
8include_once('Services/LDAP/classes/class.ilLDAPAttributeToUser.php');
9
18{
19 private $current_server = null;
20 private $ldap_query = null;
21 private $ldap_to_ilias = null;
22 private $counter = 0;
23
24 public function getId()
25 {
26 return "ldap_sync";
27 }
28
29 public function getTitle()
30 {
31 global $DIC;
32
33 $lng = $DIC['lng'];
34
35 $lng->loadLanguageModule('ldap');
36 return $lng->txt('ldap_user_sync_cron');
37 }
38
39 public function getDescription()
40 {
41 global $DIC;
42
43 $lng = $DIC['lng'];
44
45 $lng->loadLanguageModule("ldap");
46 return $lng->txt("ldap_user_sync_cron_info");
47 }
48
49 public function getDefaultScheduleType()
50 {
52 }
53
54 public function getDefaultScheduleValue()
55 {
56 return;
57 }
58
59 public function hasAutoActivation()
60 {
61 return false;
62 }
63
64 public function hasFlexibleSchedule()
65 {
66 return false;
67 }
68
69 public function run()
70 {
71 global $DIC;
72
73 $ilLog = $DIC['ilLog'];
74
76
77 $messages = array();
78 foreach (ilLDAPServer::_getCronServerIds() as $server_id) {
79 try {
80 $this->current_server = new ilLDAPServer($server_id);
81 $this->current_server->doConnectionCheck();
82 $ilLog->write("LDAP: starting user synchronization for " . $this->current_server->getName());
83
84 $this->ldap_query = new ilLDAPQuery($this->current_server);
85 $this->ldap_query->bind(IL_LDAP_BIND_DEFAULT);
86
87 if (is_array($users = $this->ldap_query->fetchUsers())) {
88 // Deactivate ldap users that are not in the list
89 $this->deactivateUsers($this->current_server, $users);
90 }
91
92 if (count($users)) {
93 include_once './Services/User/classes/class.ilUserCreationContext.php';
95
96 $offset = 0;
97 $limit = 500;
98 while ($user_sliced = array_slice($users, $offset, $limit, true)) {
99 $ilLog->write("LDAP: Starting update/creation of users ...");
100 $ilLog->write("LDAP: Offset: " . $offset);
101 $this->ldap_to_ilias = new ilLDAPAttributeToUser($this->current_server);
102 $this->ldap_to_ilias->setNewUserAuthMode($this->current_server->getAuthenticationMappingKey());
103 $this->ldap_to_ilias->setUserData($user_sliced);
104 $this->ldap_to_ilias->refresh();
105 $ilLog->write("LDAP: Finished update/creation");
106
107 $offset += $limit;
108
109 ilCronManager::ping($this->getId());
110 }
111 $this->counter++;
112 } else {
113 $ilLog->write("LDAP: No users for update/create. Aborting.");
114 }
115 } catch (ilLDAPQueryException $exc) {
116 $mess = $exc->getMessage();
117 $ilLog->write($mess);
118
119 $messages[] = $mess;
120 }
121 }
122
123 if ($this->counter) {
125 }
126 $result = new ilCronJobResult();
127 if (sizeof($messages)) {
128 $result->setMessage(implode("\n", $messages));
129 }
130 $result->setStatus($status);
131 return $result;
132 }
133
137 private function deactivateUsers(ilLDAPServer $server, $a_ldap_users)
138 {
139 global $DIC;
140
141 $ilLog = $DIC['ilLog'];
142
143 include_once './Services/User/classes/class.ilObjUser.php';
144
145
146 $inactive = [];
147
148 foreach ($ext = ilObjUser::_getExternalAccountsByAuthMode($server->getAuthenticationMappingKey(), true) as $usr_id => $external_account) {
149 if (!array_key_exists($external_account, $a_ldap_users)) {
150 $inactive[] = $usr_id;
151 }
152 }
153 if (count($inactive)) {
155 $ilLog->write('LDAP: Found ' . count($inactive) . ' inactive users.');
156
157 $this->counter++;
158 } else {
159 $ilLog->write('LDAP: No inactive users found');
160 }
161 }
162
163 public function addToExternalSettingsForm($a_form_id, array &$a_fields, $a_is_active)
164 {
165 global $DIC;
166
167 $lng = $DIC['lng'];
168
169 switch ($a_form_id) {
171 $a_fields["ldap_user_sync_cron"] = $a_is_active ?
172 $lng->txt("enabled") :
173 $lng->txt("disabled");
174 break;
175 }
176 }
177}
$result
An exception for terminatinating execution or to throw for unit testing.
const IL_LDAP_BIND_DEFAULT
Cron job result data container.
Cron job application base class.
const SCHEDULE_TYPE_DAILY
static ping($a_job_id)
Keep cron job alive.
Update/create ILIAS user account by given LDAP attributes according to user attribute mapping setting...
hasAutoActivation()
Is to be activated on "installation".
hasFlexibleSchedule()
Can the schedule be configured?
addToExternalSettingsForm($a_form_id, array &$a_fields, $a_is_active)
Add external settings to form.
deactivateUsers(ilLDAPServer $server, $a_ldap_users)
Deactivate users that are disabled in LDAP.
static _getCronServerIds()
Get list of acticve servers with option 'SyncCron'.
static _toggleActiveStatusOfUsers($a_usr_ids, $a_status)
Toggle active status of users.
static _getExternalAccountsByAuthMode($a_auth_mode, $a_read_auth_default=false)
Get list of external account by authentication method Note: If login == ext_account for two user with...
static getInstance()
Get instance.
$server
global $DIC
Definition: goto.php:24
$lng
$messages
Definition: xapiexit.php:5