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