ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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  {
74  try
75  {
76  $this->current_server = new ilLDAPServer($server_id);
77  $this->current_server->doConnectionCheck();
78  $ilLog->write("LDAP: starting user synchronization for ".$this->current_server->getName());
79 
80  $this->ldap_query = new ilLDAPQuery($this->current_server);
81  $this->ldap_query->bind(IL_LDAP_BIND_DEFAULT);
82 
83  if(is_array($users = $this->ldap_query->fetchUsers()))
84  {
85  // Deactivate ldap users that are not in the list
86  $this->deactivateUsers($this->current_server,$users);
87  }
88 
89  if(count($users))
90  {
91  include_once './Services/User/classes/class.ilUserCreationContext.php';
93 
94  $offset = 0;
95  $limit = 500;
96  while($user_sliced = array_slice($users, $offset, $limit, true))
97  {
98  $ilLog->write("LDAP: Starting update/creation of users ...");
99  $ilLog->write("LDAP: Offset: " . $offset);
100  $this->ldap_to_ilias = new ilLDAPAttributeToUser($this->current_server);
101  $this->ldap_to_ilias->setNewUserAuthMode($this->current_server->getAuthenticationMappingKey());
102  $this->ldap_to_ilias->setUserData($user_sliced);
103  $this->ldap_to_ilias->refresh();
104  $ilLog->write("LDAP: Finished update/creation");
105 
106  $offset += $limit;
107 
108  ilCronManager::ping($this->getId());
109  }
110  $this->counter++;
111  }
112  else
113  {
114  $ilLog->write("LDAP: No users for update/create. Aborting.");
115  }
116  }
117  catch(ilLDAPQueryException $exc)
118  {
119  $mess = $exc->getMessage();
120  $ilLog->write($mess);
121 
122  $messages[] = $mess;
123  }
124  }
125 
126  if($this->counter)
127  {
128  $status = ilCronJobResult::STATUS_OK;
129  }
130  $result = new ilCronJobResult();
131  if(sizeof($messages))
132  {
133  $result->setMessage(implode("\n", $messages));
134  }
135  $result->setStatus($status);
136  return $result;
137  }
138 
142  private function deactivateUsers(ilLDAPServer $server,$a_ldap_users)
143  {
144  global $ilLog;
145 
146  include_once './Services/User/classes/class.ilObjUser.php';
147 
148  foreach($ext = ilObjUser::_getExternalAccountsByAuthMode($server->getAuthenticationMappingKey(),true) as $usr_id => $external_account)
149  {
150  if(!array_key_exists($external_account,$a_ldap_users))
151  {
152  $inactive[] = $usr_id;
153  }
154  }
155  if(count($inactive))
156  {
157  ilObjUser::_toggleActiveStatusOfUsers($inactive,false);
158  $ilLog->write('LDAP: Found '.count($inactive).' inactive users.');
159 
160  $this->counter++;
161  }
162  else
163  {
164  $ilLog->write('LDAP: No inactive users found');
165  }
166  }
167 
168  public function addToExternalSettingsForm($a_form_id, array &$a_fields, $a_is_active)
169  {
170  global $lng;
171 
172  switch($a_form_id)
173  {
175  $a_fields["ldap_user_sync_cron"] = $a_is_active ?
176  $lng->txt("enabled") :
177  $lng->txt("disabled");
178  break;
179  }
180  }
181 }
182 
183 ?>
$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.
$server
$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.