ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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 
5 include_once "Services/Cron/classes/class.ilCronJob.php";
6 include_once('Services/LDAP/classes/class.ilLDAPServer.php');
7 include_once('Services/LDAP/classes/class.ilLDAPQuery.php');
8 include_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  {
51  return self::SCHEDULE_TYPE_DAILY;
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) {
124  $status = ilCronJobResult::STATUS_OK;
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  $inactive = [];
146  foreach ($ext = ilObjUser::_getExternalAccountsByAuthMode($server->getAuthenticationMappingKey(), true) as $usr_id => $external_account) {
147  if (!array_key_exists($external_account, $a_ldap_users)) {
148  $inactive[] = $usr_id;
149  }
150  }
151  if (count($inactive)) {
152  ilObjUser::_toggleActiveStatusOfUsers($inactive, false);
153  $ilLog->write('LDAP: Found ' . count($inactive) . ' inactive users.');
154 
155  $this->counter++;
156  } else {
157  $ilLog->write('LDAP: No inactive users found');
158  }
159  }
160 
161  public function addToExternalSettingsForm($a_form_id, array &$a_fields, $a_is_active)
162  {
163  global $DIC;
164 
165  $lng = $DIC['lng'];
166 
167  switch ($a_form_id) {
169  $a_fields["ldap_user_sync_cron"] = $a_is_active ?
170  $lng->txt("enabled") :
171  $lng->txt("disabled");
172  break;
173  }
174  }
175 }
$result
global $DIC
Definition: saml.php:7
static getInstance()
Get instance.
Cron job application base class.
const IL_LDAP_BIND_DEFAULT
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...
$server
Definition: sabredav.php:48
static _toggleActiveStatusOfUsers($a_usr_ids, $a_status)
Toggle active status of users.
addToExternalSettingsForm($a_form_id, array &$a_fields, $a_is_active)
$messages
Definition: en.php:5
deactivateUsers(ilLDAPServer $server, $a_ldap_users)
Deactivate users that are disabled in LDAP.
$lng
static _getCronServerIds()
Get list of acticve servers with option &#39;SyncCron&#39;.
$users
Definition: authpage.php:44
static ping($a_job_id)
Keep cron job alive.
getAuthenticationMappingKey()
Get authentication mapping key Default is ldap.
Update/create ILIAS user account by given LDAP attributes according to user attribute mapping setting...
Cron job result data container.