ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilLDAPCronSynchronization.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
26 {
27  private ilLanguage $lng;
28  private ilLogger $logger;
30 
31  private int $counter = 0;
32 
33  public function __construct()
34  {
35  global $DIC;
36 
37  $this->logger = $DIC->logger()->auth();
38  $this->cronManager = $DIC->cron()->manager();
39  $this->lng = $DIC->language();
40  $this->lng->loadLanguageModule('ldap');
41  }
42 
43  public function getId(): string
44  {
45  return "ldap_sync";
46  }
47 
48  public function getTitle(): string
49  {
50  return $this->lng->txt('ldap_user_sync_cron');
51  }
52 
53  public function getDescription(): string
54  {
55  return $this->lng->txt("ldap_user_sync_cron_info");
56  }
57 
58  public function getDefaultScheduleType(): int
59  {
60  return self::SCHEDULE_TYPE_DAILY;
61  }
62 
63  public function getDefaultScheduleValue(): ?int
64  {
65  return null;
66  }
67 
68  public function hasAutoActivation(): bool
69  {
70  return false;
71  }
72 
73  public function hasFlexibleSchedule(): bool
74  {
75  return false;
76  }
77 
78  public function run(): ilCronJobResult
79  {
81 
82  $messages = array();
83  foreach (ilLDAPServer::_getCronServerIds() as $server_id) {
84  try {
85  $current_server = new ilLDAPServer($server_id);
86  $current_server->doConnectionCheck();
87  $this->logger->info("LDAP: starting user synchronization for " . $current_server->getName());
88 
89  $ldap_query = new ilLDAPQuery($current_server);
90  $ldap_query->bind();
91 
92  if (is_array($users = $ldap_query->fetchUsers())) {
93  // Deactivate ldap users that are not in the list
94  $this->deactivateUsers($current_server, $users);
95  }
96 
97  if (count($users)) {
99 
100  $offset = 0;
101  $limit = 500;
102  while ($user_sliced = array_slice($users, $offset, $limit, true)) {
103  $this->logger->info("LDAP: Starting update/creation of users ...");
104  $this->logger->info("LDAP: Offset: " . $offset);
105  $ldap_to_ilias = new ilLDAPAttributeToUser($current_server);
106  $ldap_to_ilias->setNewUserAuthMode($current_server->getAuthenticationMappingKey());
107  $ldap_to_ilias->setUserData($user_sliced);
108  $ldap_to_ilias->refresh();
109  $this->logger->info("LDAP: Finished update/creation");
110 
111  $offset += $limit;
112 
113  $this->cronManager->ping($this->getId());
114  }
115  $this->counter++;
116  } else {
117  $this->logger->info("LDAP: No users for update/create. Aborting.");
118  }
119  } catch (ilLDAPQueryException $exc) {
120  $mess = $exc->getMessage();
121  $this->logger->info($mess);
122 
123  $messages[] = $mess;
124  }
125  }
126 
127  if ($this->counter) {
128  $status = ilCronJobResult::STATUS_OK;
129  }
130  $result = new ilCronJobResult();
131  if (count($messages)) {
132  $result->setMessage(implode("\n", $messages));
133  }
134  $result->setStatus($status);
135  return $result;
136  }
137 
141  private function deactivateUsers(ilLDAPServer $server, array $a_ldap_users): void
142  {
143  $inactive = [];
144 
145  foreach (ilObjUser::_getExternalAccountsByAuthMode($server->getAuthenticationMappingKey(), true) as $usr_id => $external_account) {
146  if (!array_key_exists($external_account, $a_ldap_users)) {
147  $inactive[] = $usr_id;
148  }
149  }
150  if (count($inactive)) {
151  ilObjUser::_toggleActiveStatusOfUsers($inactive, false);
152  $this->logger->info('LDAP: Found ' . count($inactive) . ' inactive users.');
153 
154  $this->counter++;
155  } else {
156  $this->logger->info('LDAP: No inactive users found');
157  }
158  }
159 
160  public function addToExternalSettingsForm(int $a_form_id, array &$a_fields, bool $a_is_active): void
161  {
163  $a_fields["ldap_user_sync_cron"] = [$a_is_active ?
164  $this->lng->txt("enabled") :
165  $this->lng->txt("disabled"),
167  }
168  }
169 }
global $DIC
Definition: feed.php:28
static _getExternalAccountsByAuthMode(string $a_auth_mode, bool $a_read_auth_default=false)
Get list of external account by authentication method Note: If login == ext_account for two user with...
$messages
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: xapiexit.php:22
static _toggleActiveStatusOfUsers(array $a_usr_ids, bool $a_status)
static _getCronServerIds()
Get list of acticve servers with option &#39;SyncCron&#39;.
addToExternalSettingsForm(int $a_form_id, array &$a_fields, bool $a_is_active)
$server
getAuthenticationMappingKey()
Get authentication mapping key Default is ldap.
Update/create ILIAS user account by given LDAP attributes according to user attribute mapping setting...
deactivateUsers(ilLDAPServer $server, array $a_ldap_users)
Deactivate users that are disabled in LDAP.