ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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
5include_once "Services/Cron/classes/class.ilCronJob.php";
6include_once('Services/LDAP/classes/class.ilLDAPServer.php');
7include_once('Services/LDAP/classes/class.ilLDAPQuery.php');
8include_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 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 $this->counter++;
109 }
110 else
111 {
112 $ilLog->write("LDAP: No users for update/create. Aborting.");
113 }
114 }
115 catch(ilLDAPQueryException $exc)
116 {
117 $mess = $exc->getMessage();
118 $ilLog->write($mess);
119
120 $messages[] = $mess;
121 }
122 }
123
124 if($this->counter)
125 {
127 }
128 $result = new ilCronJobResult();
129 if(sizeof($messages))
130 {
131 $result->setMessage(implode("\n", $messages));
132 }
133 $result->setStatus($status);
134 return $result;
135 }
136
140 private function deactivateUsers(ilLDAPServer $server,$a_ldap_users)
141 {
142 global $ilLog;
143
144 include_once './Services/User/classes/class.ilObjUser.php';
145
146 foreach($ext = ilObjUser::_getExternalAccountsByAuthMode($server->getAuthenticationMappingKey(),true) as $usr_id => $external_account)
147 {
148 if(!array_key_exists($external_account,$a_ldap_users))
149 {
150 $inactive[] = $usr_id;
151 }
152 }
153 if(count($inactive))
154 {
156 $ilLog->write('LDAP: Found '.count($inactive).' inactive users.');
157
158 $this->counter++;
159 }
160 else
161 {
162 $ilLog->write('LDAP: No inactive users found');
163 }
164 }
165
166 public function addToExternalSettingsForm($a_form_id, array &$a_fields, $a_is_active)
167 {
168 global $lng;
169
170 switch($a_form_id)
171 {
173 $a_fields["ldap_user_sync_cron"] = $a_is_active ?
174 $lng->txt("enabled") :
175 $lng->txt("disabled");
176 break;
177 }
178 }
179}
180
181?>
$result
const IL_LDAP_BIND_DEFAULT
Cron job result data container.
Cron job application base class.
const SCHEDULE_TYPE_DAILY
Update/create ILIAS user account by given LDAP attributes according to user attribute mapping setting...
hasAutoActivation()
Is to be activated on "installation".
hasFlexibleSchedule()
Can the schedule be configured?
addToExternalSettingsForm($a_form_id, array &$a_fields, $a_is_active)
Add external settings to form.
deactivateUsers(ilLDAPServer $server, $a_ldap_users)
Deactivate users that are disabled in LDAP.
static _getCronServerIds()
Get list of acticve servers with option 'SyncCron'.
static _toggleActiveStatusOfUsers($a_usr_ids, $a_status)
Toggle active status of users.
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 getInstance()
Get instance.
$server
$messages
Definition: en-x-test.php:7
global $lng
Definition: privfeed.php:40