ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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 $lng;
32 
33  $lng->loadLanguageModule('ldap');
34  return $lng->txt('ldap_user_sync_cron');
35  }
36 
37  public function getDescription()
38  {
39  global $lng;
40 
41  $lng->loadLanguageModule("ldap");
42  return $lng->txt("ldap_user_sync_cron_info");
43  }
44 
45  public function getDefaultScheduleType()
46  {
47  return self::SCHEDULE_TYPE_DAILY;
48  }
49 
50  public function getDefaultScheduleValue()
51  {
52  return;
53  }
54 
55  public function hasAutoActivation()
56  {
57  return false;
58  }
59 
60  public function hasFlexibleSchedule()
61  {
62  return false;
63  }
64 
65  public function run()
66  {
67  global $ilLog;
68 
70 
71  $messages = array();
72  foreach (ilLDAPServer::_getCronServerIds() as $server_id) {
73  try {
74  $this->current_server = new ilLDAPServer($server_id);
75  $this->current_server->doConnectionCheck();
76  $ilLog->write("LDAP: starting user synchronization for " . $this->current_server->getName());
77 
78  $this->ldap_query = new ilLDAPQuery($this->current_server);
79  $this->ldap_query->bind(IL_LDAP_BIND_DEFAULT);
80 
81  if (is_array($users = $this->ldap_query->fetchUsers())) {
82  // Deactivate ldap users that are not in the list
83  $this->deactivateUsers($this->current_server, $users);
84  }
85 
86  if (count($users)) {
87  include_once './Services/User/classes/class.ilUserCreationContext.php';
89 
90  $offset = 0;
91  $limit = 500;
92  while ($user_sliced = array_slice($users, $offset, $limit, true)) {
93  $ilLog->write("LDAP: Starting update/creation of users ...");
94  $ilLog->write("LDAP: Offset: " . $offset);
95  $this->ldap_to_ilias = new ilLDAPAttributeToUser($this->current_server);
96  $this->ldap_to_ilias->setNewUserAuthMode($this->current_server->getAuthenticationMappingKey());
97  $this->ldap_to_ilias->setUserData($user_sliced);
98  $this->ldap_to_ilias->refresh();
99  $ilLog->write("LDAP: Finished update/creation");
100 
101  $offset += $limit;
102 
103  ilCronManager::ping($this->getId());
104  }
105  $this->counter++;
106  } else {
107  $ilLog->write("LDAP: No users for update/create. Aborting.");
108  }
109  } catch (ilLDAPQueryException $exc) {
110  $mess = $exc->getMessage();
111  $ilLog->write($mess);
112 
113  $messages[] = $mess;
114  }
115  }
116 
117  if ($this->counter) {
118  $status = ilCronJobResult::STATUS_OK;
119  }
120  $result = new ilCronJobResult();
121  if (sizeof($messages)) {
122  $result->setMessage(implode("\n", $messages));
123  }
124  $result->setStatus($status);
125  return $result;
126  }
127 
131  private function deactivateUsers(ilLDAPServer $server, $a_ldap_users)
132  {
133  global $ilLog;
134 
135  include_once './Services/User/classes/class.ilObjUser.php';
136 
137  foreach ($ext = ilObjUser::_getExternalAccountsByAuthMode($server->getAuthenticationMappingKey(), true) as $usr_id => $external_account) {
138  if (!array_key_exists($external_account, $a_ldap_users)) {
139  $inactive[] = $usr_id;
140  }
141  }
142  if (count($inactive)) {
143  ilObjUser::_toggleActiveStatusOfUsers($inactive, false);
144  $ilLog->write('LDAP: Found ' . count($inactive) . ' inactive users.');
145 
146  $this->counter++;
147  } else {
148  $ilLog->write('LDAP: No inactive users found');
149  }
150  }
151 
152  public function addToExternalSettingsForm($a_form_id, array &$a_fields, $a_is_active)
153  {
154  global $lng;
155 
156  switch ($a_form_id) {
158  $a_fields["ldap_user_sync_cron"] = $a_is_active ?
159  $lng->txt("enabled") :
160  $lng->txt("disabled");
161  break;
162  }
163  }
164 }
$result
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...
static _toggleActiveStatusOfUsers($a_usr_ids, $a_status)
Toggle active status of users.
addToExternalSettingsForm($a_form_id, array &$a_fields, $a_is_active)
deactivateUsers(ilLDAPServer $server, $a_ldap_users)
Deactivate users that are disabled in LDAP.
static _getCronServerIds()
Get list of acticve servers with option &#39;SyncCron&#39;.
Create styles array
The data for the language used.
$users
Definition: authpage.php:44
$server
Definition: getUserInfo.php:12
$messages
Definition: en-x-test.php:7
static ping($a_job_id)
Keep cron job alive.
global $lng
Definition: privfeed.php:17
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.