ILIAS  trunk Revision v11.0_alpha-1713-gd8962da2f67
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilCronDeleteInactivatedUserAccounts.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
25 
32 {
33  private const DEFAULT_INACTIVITY_PERIOD = 365;
34  private int $period;
36  private array $include_roles;
38  private Language $lng;
41  private \ILIAS\HTTP\GlobalHttpState $http;
42  private \ILIAS\Refinery\Factory $refinery;
43 
44  public function __construct()
45  {
47  global $DIC;
48 
49  if (isset($DIC['http'])) {
50  $this->http = $DIC['http'];
51  }
52 
53  if (isset($DIC['lng'])) {
54  $this->lng = $DIC['lng'];
55  }
56 
57  if (isset($DIC['refinery'])) {
58  $this->refinery = $DIC['refinery'];
59  }
60 
61  if (isset($DIC['ilObjDataCache'])) {
62  $this->objectDataCache = $DIC['ilObjDataCache'];
63  }
64 
65  if (isset($DIC['rbacreview'])) {
66  $this->rbac_review = $DIC['rbacreview'];
67  }
68 
69  if (isset($DIC['ilSetting'])) {
70  $this->settings = $DIC['ilSetting'];
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  public function getId(): string
90  {
91  return "user_inactivated";
92  }
93 
94  public function getTitle(): string
95  {
96  return $this->lng->txt("delete_inactivated_user_accounts");
97  }
98 
99  public function getDescription(): string
100  {
101  return sprintf(
102  $this->lng->txt("delete_inactivated_user_accounts_desc"),
104  );
105  }
106 
108  {
109  return JobScheduleType::DAILY;
110  }
111 
112  public function getDefaultScheduleValue(): ?int
113  {
114  return null;
115  }
116 
117  public function hasAutoActivation(): bool
118  {
119  return false;
120  }
121 
122  public function hasFlexibleSchedule(): bool
123  {
124  return true;
125  }
126 
127  public function hasCustomSettings(): bool
128  {
129  return true;
130  }
131 
132  public function run(): JobResult
133  {
134  $status = JobResult::STATUS_NO_ACTION;
135 
136  $usr_ids = ilObjUser::_getUserIdsByInactivationPeriod($this->period);
137 
138  $counter = 0;
139  foreach ($usr_ids as $usr_id) {
140  if ($usr_id === ANONYMOUS_USER_ID || $usr_id === SYSTEM_USER_ID) {
141  continue;
142  }
143 
144  foreach ($this->include_roles as $role_id) {
145  if ($this->rbac_review->isAssigned($usr_id, $role_id)) {
146  $user = ilObjectFactory::getInstanceByObjId($usr_id);
147  $user->delete();
148  $counter++;
149  break;
150  }
151  }
152  }
153 
154  if ($counter > 0) {
155  $status = JobResult::STATUS_OK;
156  }
157 
158  $result = new JobResult();
159  $result->setStatus($status);
160 
161  return $result;
162  }
163 
164  public function addCustomSettingsToForm(ilPropertyFormGUI $a_form): void
165  {
166  $sub_mlist = new ilMultiSelectInputGUI(
167  $this->lng->txt('delete_inactivated_user_accounts_include_roles'),
168  'cron_inactivated_user_delete_include_roles'
169  );
170  $sub_mlist->setInfo($this->lng->txt('delete_inactivated_user_accounts_include_roles_desc'));
171  $roles = [];
172  foreach ($this->rbac_review->getGlobalRoles() as $role_id) {
173  if ($role_id !== ANONYMOUS_ROLE_ID) {
174  $roles[$role_id] = $this->objectDataCache->lookupTitle($role_id);
175  }
176  }
177  $sub_mlist->setOptions($roles);
178  $setting = $this->settings->get('cron_inactivated_user_delete_include_roles', null);
179  if ($setting === null) {
180  $setting = [];
181  } else {
182  $setting = explode(',', $setting);
183  }
184  $sub_mlist->setValue($setting);
185  $sub_mlist->setWidth(300);
186  $a_form->addItem($sub_mlist);
187 
188  $sub_text = new ilNumberInputGUI(
189  $this->lng->txt('delete_inactivated_user_accounts_period'),
190  'cron_inactivated_user_delete_period'
191  );
192  $sub_text->allowDecimals(false);
193  $sub_text->setInfo($this->lng->txt('delete_inactivated_user_accounts_period_desc'));
194  $sub_text->setValue(
195  $this->settings->get(
196  'cron_inactivated_user_delete_period',
197  (string) self::DEFAULT_INACTIVITY_PERIOD
198  )
199  );
200  $sub_text->setSize(4);
201  $sub_text->setMaxLength(4);
202  $sub_text->setRequired(true);
203  $a_form->addItem($sub_text);
204  }
205 
206  public function saveCustomSettings(ilPropertyFormGUI $a_form): bool
207  {
208  $roles = implode(',', $this->http->wrapper()->post()->retrieve(
209  'cron_inactivated_user_delete_include_roles',
210  $this->refinery->byTrying([
211  $this->refinery->kindlyTo()->listOf($this->refinery->kindlyTo()->int()),
212  $this->refinery->always([])
213  ])
214  ));
215 
216  $period = $this->http->wrapper()->post()->retrieve(
217  'cron_inactivated_user_delete_period',
218  $this->refinery->byTrying([
219  $this->refinery->kindlyTo()->int(),
220  $this->refinery->always(null)
221  ])
222  );
223 
224  $this->settings->set('cron_inactivated_user_delete_include_roles', $roles);
225  $this->settings->set('cron_inactivated_user_delete_period', (string) ($period ?? self::DEFAULT_INACTIVITY_PERIOD));
226 
227  return true;
228  }
229 }
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
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
allowDecimals(bool $a_value)
This class represents a multi selection list property in a property form.
static http()
Fetches the global http state from ILIAS.
This class represents a number property in a property form.
global $DIC
Definition: shib_login.php:22
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)