ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilCronDeleteInactivatedUserAccounts.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
22 
29 {
30  private const DEFAULT_INACTIVITY_PERIOD = 365;
31  private int $period;
33  private array $include_roles;
35  private ilLanguage $lng;
38  private \ILIAS\HTTP\GlobalHttpState $http;
39  private \ILIAS\Refinery\Factory $refinery;
40 
41  public function __construct()
42  {
43  global $DIC;
44 
45  if ($DIC) {
46  if (isset($DIC['http'])) {
47  $this->http = $DIC->http();
48  }
49 
50  if (isset($DIC['lng'])) {
51  $this->lng = $DIC->language();
52  }
53 
54  if (isset($DIC['refinery'])) {
55  $this->refinery = $DIC->refinery();
56  }
57 
58  if (isset($DIC['ilObjDataCache'])) {
59  $this->objectDataCache = $DIC['ilObjDataCache'];
60  }
61 
62  if (isset($DIC['rbacreview'])) {
63  $this->rbacReview = $DIC->rbac()->review();
64  }
65 
66  $rbacreview = $DIC->rbac()->review();
67  $ilObjDataCache = $DIC['ilObjDataCache'];
68 
69  if ($DIC['ilSetting']) {
70  $this->settings = $DIC->settings();
71 
72  $include_roles = $this->settings->get(
73  'cron_inactivated_user_delete_include_roles',
74  null
75  );
76  if ($include_roles === null) {
77  $this->include_roles = [];
78  } else {
79  $this->include_roles = array_filter(array_map('intval', explode(',', $include_roles)));
80  }
81 
82  $this->period = (int) $this->settings->get(
83  'cron_inactivated_user_delete_period',
84  (string) self::DEFAULT_INACTIVITY_PERIOD
85  );
86  }
87  }
88  }
89 
90  public function getId(): string
91  {
92  return "user_inactivated";
93  }
94 
95  public function getTitle(): string
96  {
97  return $this->lng->txt("delete_inactivated_user_accounts");
98  }
99 
100  public function getDescription(): string
101  {
102  return sprintf(
103  $this->lng->txt("delete_inactivated_user_accounts_desc"),
105  );
106  }
107 
108  public function getDefaultScheduleType(): int
109  {
110  return self::SCHEDULE_TYPE_DAILY;
111  }
112 
113  public function getDefaultScheduleValue(): ?int
114  {
115  return null;
116  }
117 
118  public function hasAutoActivation(): bool
119  {
120  return false;
121  }
122 
123  public function hasFlexibleSchedule(): bool
124  {
125  return true;
126  }
127 
128  public function hasCustomSettings(): bool
129  {
130  return true;
131  }
132 
133  public function run(): ilCronJobResult
134  {
135  global $DIC;
136 
137  $rbacreview = $DIC->rbac()->review();
138 
140 
141  $usr_ids = ilObjUser::_getUserIdsByInactivationPeriod($this->period);
142 
143  $counter = 0;
144  foreach ($usr_ids as $usr_id) {
145  if ($usr_id === ANONYMOUS_USER_ID || $usr_id === SYSTEM_USER_ID) {
146  continue;
147  }
148 
149  $continue = true;
150  foreach ($this->include_roles as $role_id) {
151  if ($rbacreview->isAssigned($usr_id, $role_id)) {
152  $continue = false;
153  break;
154  }
155  }
156 
157  if ($continue) {
158  continue;
159  }
160 
161  $user = ilObjectFactory::getInstanceByObjId($usr_id);
162  $user->delete();
163 
164  $counter++;
165  }
166 
167  if ($counter > 0) {
168  $status = ilCronJobResult::STATUS_OK;
169  }
170 
171  $result = new ilCronJobResult();
172  $result->setStatus($status);
173 
174  return $result;
175  }
176 
177  public function addCustomSettingsToForm(ilPropertyFormGUI $a_form): void
178  {
179  $sub_mlist = new ilMultiSelectInputGUI(
180  $this->lng->txt('delete_inactivated_user_accounts_include_roles'),
181  'cron_inactivated_user_delete_include_roles'
182  );
183  $sub_mlist->setInfo($this->lng->txt('delete_inactivated_user_accounts_include_roles_desc'));
184  $roles = [];
185  foreach ($this->rbacReview->getGlobalRoles() as $role_id) {
186  if ($role_id !== ANONYMOUS_ROLE_ID) {
187  $roles[$role_id] = $this->objectDataCache->lookupTitle($role_id);
188  }
189  }
190  $sub_mlist->setOptions($roles);
191  $setting = $this->settings->get('cron_inactivated_user_delete_include_roles', null);
192  if ($setting === null) {
193  $setting = [];
194  } else {
195  $setting = explode(',', $setting);
196  }
197  $sub_mlist->setValue($setting);
198  $sub_mlist->setWidth(300);
199  $a_form->addItem($sub_mlist);
200 
201  $sub_text = new ilNumberInputGUI(
202  $this->lng->txt('delete_inactivated_user_accounts_period'),
203  'cron_inactivated_user_delete_period'
204  );
205  $sub_text->allowDecimals(false);
206  $sub_text->setInfo($this->lng->txt('delete_inactivated_user_accounts_period_desc'));
207  $sub_text->setValue(
208  $this->settings->get(
209  'cron_inactivated_user_delete_period',
210  (string) self::DEFAULT_INACTIVITY_PERIOD
211  )
212  );
213  $sub_text->setSize(4);
214  $sub_text->setMaxLength(4);
215  $sub_text->setRequired(true);
216  $a_form->addItem($sub_text);
217  }
218 
219  public function saveCustomSettings(ilPropertyFormGUI $a_form): bool
220  {
221  $roles = implode(',', $this->http->wrapper()->post()->retrieve(
222  'cron_inactivated_user_delete_include_roles',
223  $this->refinery->byTrying([
224  $this->refinery->kindlyTo()->listOf($this->refinery->kindlyTo()->int()),
225  $this->refinery->always([])
226  ])
227  ));
228 
229  $period = $this->http->wrapper()->post()->retrieve(
230  'cron_inactivated_user_delete_period',
231  $this->refinery->byTrying([
232  $this->refinery->kindlyTo()->int(),
233  $this->refinery->always(null)
234  ])
235  );
236 
237  $this->settings->set('cron_inactivated_user_delete_include_roles', $roles);
238  $this->settings->set('cron_inactivated_user_delete_period', (string) ($period ?? self::DEFAULT_INACTIVITY_PERIOD));
239 
240  return true;
241  }
242 }
const ANONYMOUS_USER_ID
Definition: constants.php:27
const SYSTEM_USER_ID
This file contains constants for PHPStan analyis, see: https://phpstan.org/config-reference#constants...
Definition: constants.php:26
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
global $DIC
Definition: feed.php:28
allowDecimals(bool $a_value)
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.
This class represents a number property in a property form.
static _getUserIdsByInactivationPeriod(int $period)
get ids of all users that have been inactivated since at least the given period
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
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...