ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
class.ilCronDeleteNeverLoggedInUserAccounts.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
24 
26 {
27  private const DEFAULT_CREATION_THRESHOLD = 365;
28 
29  private string $roleIdWhiteliste = '';
30  private int $thresholdInDays = self::DEFAULT_CREATION_THRESHOLD;
31  private Language $lng;
35  private \ILIAS\HTTP\GlobalHttpState $http;
36  private \ILIAS\Refinery\Factory $refinery;
37  private \ilGlobalTemplateInterface $main_tpl;
38 
39  public function __construct()
40  {
41  global $DIC;
42  $this->main_tpl = $DIC->ui()->mainTemplate();
43 
44  if ($DIC) {
45  if (isset($DIC['ilSetting'])) {
46  $this->settings = $DIC->settings();
47 
48  $this->roleIdWhiteliste = (string) $this->settings->get(
49  'cron_users_without_login_delete_incl_roles',
50  ''
51  );
52 
53  $this->thresholdInDays = (int) $this->settings->get(
54  'cron_users_without_login_delete_threshold',
55  (string) self::DEFAULT_CREATION_THRESHOLD
56  );
57  }
58 
59  if (isset($DIC['lng'])) {
60  $this->lng = $DIC->language();
61  $this->lng->loadLanguageModule('usr');
62  }
63 
64  if (isset($DIC['rbacreview'])) {
65  $this->rbacreview = $DIC->rbac()->review();
66  }
67 
68  if (isset($DIC['ilObjDataCache'])) {
69  $this->objectDataCache = $DIC['ilObjDataCache'];
70  }
71 
72  if (isset($DIC['http'])) {
73  $this->http = $DIC->http();
74  }
75 
76  if (isset($DIC['refinery'])) {
77  $this->refinery = $DIC->refinery();
78  }
79  }
80  }
81 
82  public function getId(): string
83  {
84  return 'user_never_logged_in';
85  }
86 
87  public function getTitle(): string
88  {
89  global $DIC;
90 
91  return $DIC->language()->txt('user_never_logged_in');
92  }
93 
94  public function getDescription(): string
95  {
96  global $DIC;
97 
98  return $DIC->language()->txt('user_never_logged_in_info');
99  }
100 
102  {
103  return CronJobScheduleType::SCHEDULE_TYPE_DAILY;
104  }
105 
106  public function getDefaultScheduleValue(): int
107  {
108  return 1;
109  }
110 
111  public function hasAutoActivation(): bool
112  {
113  return false;
114  }
115 
116  public function hasFlexibleSchedule(): bool
117  {
118  return true;
119  }
120 
121  public function hasCustomSettings(): bool
122  {
123  return true;
124  }
125 
126  public function run(): ilCronJobResult
127  {
128  global $DIC;
129 
130  $result = new ilCronJobResult();
131 
133  $message = 'No user deleted';
134 
136  $this->thresholdInDays ?: self::DEFAULT_CREATION_THRESHOLD
137  );
138 
139  $roleIdWhitelist = array_filter(array_map('intval', explode(',', $this->roleIdWhiteliste)));
140 
141  $counter = 0;
142  foreach ($userIds as $userId) {
143  if ($userId === ANONYMOUS_USER_ID || $userId === SYSTEM_USER_ID) {
144  continue;
145  }
146 
147  $user = ilObjectFactory::getInstanceByObjId($userId, false);
148  if (!($user instanceof ilObjUser)) {
149  continue;
150  }
151 
152  $ignoreUser = true;
153 
154  if (count($roleIdWhitelist) > 0) {
155  $assignedRoleIds = array_filter(array_map('intval', $this->rbacreview->assignedRoles($userId)));
156 
157  $respectedRolesToInclude = array_intersect($assignedRoleIds, $roleIdWhitelist);
158  if (count($respectedRolesToInclude) > 0) {
159  $ignoreUser = false;
160  }
161  }
162 
163  if ($ignoreUser) {
164  continue;
165  }
166 
167  $DIC->logger()->user()->info(sprintf(
168  "Deleting user account with id %s (login: %s)",
169  $user->getId(),
170  $user->getLogin()
171  ));
172  $user->delete();
173 
174  $counter++;
175  }
176 
177  if ($counter) {
178  $status = ilCronJobResult::STATUS_OK;
179  $message = sprintf('%s user(s) deleted', $counter);
180  }
181 
182  $result->setStatus($status);
183  $result->setMessage($message);
184 
185  return $result;
186  }
187 
188  public function addCustomSettingsToForm(ilPropertyFormGUI $a_form): void
189  {
190  $roleWhiteList = new ilMultiSelectInputGUI(
191  $this->lng->txt('cron_users_without_login_del_role_whitelist'),
192  'role_whitelist'
193  );
194  $roleWhiteList->setInfo($this->lng->txt('cron_users_without_login_del_role_whitelist_info'));
195  $roles = [];
196  foreach ($this->rbacreview->getGlobalRoles() as $role_id) {
197  if ($role_id !== ANONYMOUS_ROLE_ID) {
198  $roles[$role_id] = $this->objectDataCache->lookupTitle($role_id);
199  }
200  }
201  $roleWhiteList->setOptions($roles);
202  $roleWhiteList->setValue(array_filter(array_map('intval', explode(',', $this->roleIdWhiteliste))));
203  $roleWhiteList->setWidth(300);
204  $a_form->addItem($roleWhiteList);
205 
206  $threshold = new ilNumberInputGUI(
207  $this->lng->txt('cron_users_without_login_del_create_date_thr'),
208  'threshold'
209  );
210  $threshold->allowDecimals(false);
211  $threshold->setInfo($this->lng->txt('cron_users_without_login_del_create_date_thr_info'));
212  $threshold->setValue((string) $this->thresholdInDays);
213  $threshold->setSuffix($this->lng->txt('days'));
214  $threshold->setSize(4);
215  $threshold->setMaxLength(4);
216  $threshold->setRequired(true);
217  $a_form->addItem($threshold);
218  }
219 
220  public function saveCustomSettings(ilPropertyFormGUI $a_form): bool
221  {
222  $valid = true;
223 
224  $this->roleIdWhiteliste = implode(',', $this->http->wrapper()->post()->retrieve(
225  'role_whitelist',
226  $this->refinery->byTrying([
227  $this->refinery->kindlyTo()->listOf($this->refinery->kindlyTo()->int()),
228  $this->refinery->always([])
229  ])
230  ));
231 
232  try {
233  $this->thresholdInDays = $this->http->wrapper()->post()->retrieve(
234  'threshold',
235  $this->refinery->kindlyTo()->int()
236  );
237  } catch (ConstraintViolationException $e) {
238  $valid = false;
239  $a_form->getItemByPostVar('threshold')->setAlert($this->lng->txt('user_never_logged_in_info_threshold_err_num'));
240  }
241 
242  if ($valid) {
243  $this->settings->set(
244  'cron_users_without_login_delete_incl_roles',
245  $this->roleIdWhiteliste
246  );
247  $this->settings->set(
248  'cron_users_without_login_delete_threshold',
249  (string) $this->thresholdInDays
250  );
251  return true;
252  }
253 
254  $this->main_tpl->setOnScreenMessage('failure', $this->lng->txt('form_input_not_valid'));
255  return false;
256  }
257 }
const ANONYMOUS_USER_ID
Definition: constants.php:27
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
getItemByPostVar(string $a_post_var)
const SYSTEM_USER_ID
This file contains constants for PHPStan analyis, see: https://phpstan.org/config-reference#constants...
Definition: constants.php:26
$valid
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static http()
Fetches the global http state from ILIAS.
final const STATUS_NO_ACTION
global $DIC
Definition: shib_login.php:25
static getInstanceByObjId(?int $obj_id, bool $stop_on_error=true)
get an instance of an Ilias object by object id
const ANONYMOUS_ROLE_ID
Definition: constants.php:28
static getUserIdsNeverLoggedIn(int $thresholdInDays)
Get ids of all users that have never logged in.