ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilCronDeleteInactivatedUserAccounts.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
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  {
44  global $DIC;
45 
46  if (isset($DIC['http'])) {
47  $this->http = $DIC['http'];
48  }
49 
50  if (isset($DIC['lng'])) {
51  $this->lng = $DIC['lng'];
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->rbac_review = $DIC['rbacreview'];
64  }
65 
66  if (isset($DIC['ilSetting'])) {
67  $this->settings = $DIC['ilSetting'];
68 
69  $include_roles = $this->settings->get(
70  'cron_inactivated_user_delete_include_roles',
71  null
72  );
73  if ($include_roles === null) {
74  $this->include_roles = [];
75  } else {
76  $this->include_roles = array_filter(array_map('intval', explode(',', $include_roles)));
77  }
78 
79  $this->period = (int) $this->settings->get(
80  'cron_inactivated_user_delete_period',
81  (string) self::DEFAULT_INACTIVITY_PERIOD
82  );
83  }
84  }
85 
86  public function getId(): string
87  {
88  return "user_inactivated";
89  }
90 
91  public function getTitle(): string
92  {
93  return $this->lng->txt("delete_inactivated_user_accounts");
94  }
95 
96  public function getDescription(): string
97  {
98  return sprintf(
99  $this->lng->txt("delete_inactivated_user_accounts_desc"),
101  );
102  }
103 
105  {
106  return CronJobScheduleType::SCHEDULE_TYPE_DAILY;
107  }
108 
109  public function getDefaultScheduleValue(): ?int
110  {
111  return null;
112  }
113 
114  public function hasAutoActivation(): bool
115  {
116  return false;
117  }
118 
119  public function hasFlexibleSchedule(): bool
120  {
121  return true;
122  }
123 
124  public function hasCustomSettings(): bool
125  {
126  return true;
127  }
128 
129  public function run(): ilCronJobResult
130  {
132 
133  $usr_ids = ilObjUser::_getUserIdsByInactivationPeriod($this->period);
134 
135  $counter = 0;
136  foreach ($usr_ids as $usr_id) {
137  if ($usr_id === ANONYMOUS_USER_ID || $usr_id === SYSTEM_USER_ID) {
138  continue;
139  }
140 
141  foreach ($this->include_roles as $role_id) {
142  if ($this->rbac_review->isAssigned($usr_id, $role_id)) {
143  $user = ilObjectFactory::getInstanceByObjId($usr_id);
144  $user->delete();
145  $counter++;
146  break;
147  }
148  }
149  }
150 
151  if ($counter > 0) {
152  $status = ilCronJobResult::STATUS_OK;
153  }
154 
155  $result = new ilCronJobResult();
156  $result->setStatus($status);
157 
158  return $result;
159  }
160 
161  public function addCustomSettingsToForm(ilPropertyFormGUI $a_form): void
162  {
163  $sub_mlist = new ilMultiSelectInputGUI(
164  $this->lng->txt('delete_inactivated_user_accounts_include_roles'),
165  'cron_inactivated_user_delete_include_roles'
166  );
167  $sub_mlist->setInfo($this->lng->txt('delete_inactivated_user_accounts_include_roles_desc'));
168  $roles = [];
169  foreach ($this->rbac_review->getGlobalRoles() as $role_id) {
170  if ($role_id !== ANONYMOUS_ROLE_ID) {
171  $roles[$role_id] = $this->objectDataCache->lookupTitle($role_id);
172  }
173  }
174  $sub_mlist->setOptions($roles);
175  $setting = $this->settings->get('cron_inactivated_user_delete_include_roles', null);
176  if ($setting === null) {
177  $setting = [];
178  } else {
179  $setting = explode(',', $setting);
180  }
181  $sub_mlist->setValue($setting);
182  $sub_mlist->setWidth(300);
183  $a_form->addItem($sub_mlist);
184 
185  $sub_text = new ilNumberInputGUI(
186  $this->lng->txt('delete_inactivated_user_accounts_period'),
187  'cron_inactivated_user_delete_period'
188  );
189  $sub_text->allowDecimals(false);
190  $sub_text->setInfo($this->lng->txt('delete_inactivated_user_accounts_period_desc'));
191  $sub_text->setValue(
192  $this->settings->get(
193  'cron_inactivated_user_delete_period',
194  (string) self::DEFAULT_INACTIVITY_PERIOD
195  )
196  );
197  $sub_text->setSize(4);
198  $sub_text->setMaxLength(4);
199  $sub_text->setRequired(true);
200  $a_form->addItem($sub_text);
201  }
202 
203  public function saveCustomSettings(ilPropertyFormGUI $a_form): bool
204  {
205  $roles = implode(',', $this->http->wrapper()->post()->retrieve(
206  'cron_inactivated_user_delete_include_roles',
207  $this->refinery->byTrying([
208  $this->refinery->kindlyTo()->listOf($this->refinery->kindlyTo()->int()),
209  $this->refinery->always([])
210  ])
211  ));
212 
213  $period = $this->http->wrapper()->post()->retrieve(
214  'cron_inactivated_user_delete_period',
215  $this->refinery->byTrying([
216  $this->refinery->kindlyTo()->int(),
217  $this->refinery->always(null)
218  ])
219  );
220 
221  $this->settings->set('cron_inactivated_user_delete_include_roles', $roles);
222  $this->settings->set('cron_inactivated_user_delete_period', (string) ($period ?? self::DEFAULT_INACTIVITY_PERIOD));
223 
224  return true;
225  }
226 }
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...
const SYSTEM_USER_ID
This file contains constants for PHPStan analyis, see: https://phpstan.org/config-reference#constants...
Definition: constants.php:26
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.
__construct(VocabulariesInterface $vocabularies)
final const STATUS_NO_ACTION
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