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