ILIAS  Release_4_4_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  $this->ldap_to_ilias = new ilLDAPAttributeToUser($this->current_server);
93  $this->ldap_to_ilias->setNewUserAuthMode($this->current_server->getAuthenticationMappingKey());
94  #$ilLog->write(print_r($users,true));
95  $this->ldap_to_ilias->setUserData($users);
96  $this->ldap_to_ilias->refresh();
97  $ilLog->write("LDAP: Finished update/creation");
98 
99  $this->counter++;
100  }
101  else
102  {
103  $ilLog->write("LDAP: No users for update/create. Aborting.");
104  }
105  }
106  catch(ilLDAPQueryException $exc)
107  {
108  $mess = $exc->getMessage();
109  $ilLog->write($mess);
110 
111  $messages[] = $mess;
112  }
113  }
114 
115  if($this->counter)
116  {
117  $status = ilCronJobResult::STATUS_OK;
118  }
119  $result = new ilCronJobResult();
120  if(sizeof($messages))
121  {
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  {
139  if(!array_key_exists($external_account,$a_ldap_users))
140  {
141  $inactive[] = $usr_id;
142  }
143  }
144  if(count($inactive))
145  {
146  ilObjUser::_toggleActiveStatusOfUsers($inactive,false);
147  $ilLog->write('LDAP: Found '.count($inactive).' inactive users.');
148 
149  $this->counter++;
150  }
151  else
152  {
153  $ilLog->write('LDAP: No inactive users found');
154  }
155  }
156 
157  public function addToExternalSettingsForm($a_form_id, array &$a_fields, $a_is_active)
158  {
159  global $lng;
160 
161  switch($a_form_id)
162  {
164  $a_fields["ldap_user_sync_cron"] = $a_is_active ?
165  $lng->txt("enabled") :
166  $lng->txt("disabled");
167  break;
168  }
169  }
170 }
171 
172 ?>