ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
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  {
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  $ilLog->write("LDAP: Starting update/creation of users ...");
92 
93  include_once './Services/User/classes/class.ilUserCreationContext.php';
95 
96  $this->ldap_to_ilias = new ilLDAPAttributeToUser($this->current_server);
97  $this->ldap_to_ilias->setNewUserAuthMode($this->current_server->getAuthenticationMappingKey());
98  #$ilLog->write(print_r($users,true));
99  $this->ldap_to_ilias->setUserData($users);
100  $this->ldap_to_ilias->refresh();
101  $ilLog->write("LDAP: Finished update/creation");
102 
103  $this->counter++;
104  }
105  else
106  {
107  $ilLog->write("LDAP: No users for update/create. Aborting.");
108  }
109  }
110  catch(ilLDAPQueryException $exc)
111  {
112  $mess = $exc->getMessage();
113  $ilLog->write($mess);
114 
115  $messages[] = $mess;
116  }
117  }
118 
119  if($this->counter)
120  {
121  $status = ilCronJobResult::STATUS_OK;
122  }
123  $result = new ilCronJobResult();
124  if(sizeof($messages))
125  {
126  $result->setMessage(implode("\n", $messages));
127  }
128  $result->setStatus($status);
129  return $result;
130  }
131 
135  private function deactivateUsers(ilLDAPServer $server,$a_ldap_users)
136  {
137  global $ilLog;
138 
139  include_once './Services/User/classes/class.ilObjUser.php';
140 
141  foreach($ext = ilObjUser::_getExternalAccountsByAuthMode($server->getAuthenticationMappingKey(),true) as $usr_id => $external_account)
142  {
143  if(!array_key_exists($external_account,$a_ldap_users))
144  {
145  $inactive[] = $usr_id;
146  }
147  }
148  if(count($inactive))
149  {
150  ilObjUser::_toggleActiveStatusOfUsers($inactive,false);
151  $ilLog->write('LDAP: Found '.count($inactive).' inactive users.');
152 
153  $this->counter++;
154  }
155  else
156  {
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 $lng;
164 
165  switch($a_form_id)
166  {
168  $a_fields["ldap_user_sync_cron"] = $a_is_active ?
169  $lng->txt("enabled") :
170  $lng->txt("disabled");
171  break;
172  }
173  }
174 }
175 
176 ?>